sql - How to pass Ordinal_Position values dynamically to a query? -
i trying run below query dynamically fetch column name depending on ordinal_position.
declare @data_col nvarchar(max), @id int set @id=1 print @id set @data_col='select column_name information_schema.columns table_name=''source_table'' , ordinal_position='+@id+'' exec sp_executesql @data_col
this doesn't seems work. there alternate method??
you using sp_executesql
. use parameters!
declare @data_col nvarchar(max), @id int; set @id = 1; print @id; set @data_col = ' select column_name information_schema.columns table_name = ''source_table'' , ordinal_position = @id '; exec sp_executesql @data_col, n'@id integer', @id = @id;
i think pass table in parameter, there no harm in hard coding it.
Comments
Post a Comment