0

I split this question of from Fill dynamic array with values from an array. Below is the actual code I now have.

First the dynamic code, second the 3 example queries that I would like the dynamic code to output (but which it doesn't). I get an error Invalid column name "field" or, when I change field into #tmp_auditlog_fields.field, the error that the column "field" cannot be bound. The latter error is probably due to me not joining any tables but I have no clue on which field to join the tables since the #tmp_auditlogs_fields is nothing more than an array to create a bunch of queries, unrelated to any real data in the database.

How can I get the desired output? Any help is greatly appreciated :-)

-- dynamic code

create table #tmp_auditlog_fields (
  id int identity(1,1),
  field varchar(255),
  details varchar(255)
);

insert into #tmp_auditlog_fields (field, details) values
    ('a', 'foo'),
    ('b', 'bar'),
    ('c', 'baz'),
;

declare @sql nvarchar(max);
declare @cnt int, @i int = 1;

select @cnt = max(id) from #tmp_auditlog_fields;

while @i <= @cnt
    begin
        select @sql = 
            'select t.objectID, t.DocID as ' + field + 
            ' into #tmp_auditlog_' + field + 
            ' from #tmp_auditlog_subselection t where Details like ' + 
            details FROM  tmp_auditlog_fields WHERE id = @i
        exec sp_executesql @sql
        select @i = @i + 1
    end 
;

-- example queries

select t.ObjectID, t.DocID as a 
into #tmp_auditlog_a from #tmp_auditlog_subselection t
where Details like 'foo';

select t.ObjectID, t.DocID as b 
into #tmp_auditlog_b from #tmp_auditlog_subselection t
where Details like 'bar';

select t.ObjectID, t.DocID as c 
into #tmp_auditlog_c from #tmp_auditlog_subselection t
where Details like 'baz';

SQL Fiddle attempt:

declare @sql nvarchar(max);
declare @id int;

create table #tmp_auditlog_subselection (
  stuff varchar(255),
  other varchar(255)
);

insert into #tmp_auditlog_subselection (stuff, other) values
  ('foo', 'bar'),
  ('baz', 'bat'),
  ('lorem', 'ipsum')
;

create table #tmp_auditlog_fields (
  id int identity(1,1),
  field varchar(255),
  details varchar(255)
);

insert into #tmp_auditlog_fields (field, details) values
  ('a', 'foo'),
  ('b', 'bar'),
  ('c', 'baz')
;

while exists(select * from #tmp_auditlog_fields where id >= @id)
    begin
        select @sql = 
            'select t.objectID, t.DocID as ' + field + 
            ' into #tmp_auditlog_' + field + 
            ' from #tmp_auditlog_subselection t where Details like ' + 
            details FROM tmp_auditlog_fields WHERE id = @i;
        exec @sql
        select @id = min(id) from #tmp_auditlog_fields where id > @id;
    end 
;

select * from #tmp_auditlog_a;
1
  • 1
    Add a FROM #tmp_auditlog_fields after your select @sql = .... The current query doesn't know what table to pull your field and details columns from. Commented Jun 12, 2014 at 15:22

1 Answer 1

1

You're missing the from statement and where statement here:

while @i <= @cnt
    begin
        select @sql = 
            'select t.objectID, t.DocID as ' + field + 
            ' into #tmp_auditlog_' + field + 
            ' from #tmp_auditlog_subselection t where Details like ' + details FROM #tmp_auditlog_fields WHERE id = @i
        exec sp_executesql @sql
        select @i = @i + 1
  end
Sign up to request clarification or add additional context in comments.

10 Comments

They should be - you could try global temp tables instead: ##temp_auditlog_foo
When I try to run on my machine I get two errors: Extra comma after ('b', 'bar'), ('c', 'baz'), ; and also missing pound sign from here: details FROM tmp_auditlog_fields WHERE id = @i
Temp tables created within dynamic sql are scoped only within that dynamic sql. Temp tables created outside of the dynamic sql are scoped such that the current session as well as the dynamic session can access it. Global temp tables are an alternative, however, as Dave.Gugg suggested.
I have not used SQL Fiddle much. During my test attempts just now, it appears that using ; seems to be breaking up the batches issued and is causing the variables to fall out of scope.
Your fiddle link did not have any temp tables. I tried to recreate what I think you wanted here: sqlfiddle.com/#!3/dcae9/20
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.