Reputation: 189
Hi I have a little problem. I must exec query in that style. In the example, something like that
declare @name varchar(max)
set @name = 'ColumnID'
select @name from Account
that return a lot of 'ColumnID' but I will have a result column ColumnID in Account table
Upvotes: 1
Views: 154
Reputation: 4683
try using sp_executesql stored procedure
declare @name nvarchar(20);
set @name = 'ColumnID';
declare @sql nvarchar(max)
set @sql='select ' + @name + ' from Account'
exec sp_executesql @sql
Upvotes: 0
Reputation: 30498
You'll be wanting to execute a dynamic SQL Statement:
exec('select ' + @name + ' from Account');
Be wary of over-liberal use of these, as they can come with pretty hefty baggage:
Advantages
Disadvantages
Upvotes: 3
Reputation: 351516
Try this:
declare @name varchar(max);
set @name = 'ColumnID';
exec('select ' + @name + ' from Account');
Upvotes: 1