i need a solution for the following query. declare @r1 as varchar (5) set @r1='Title' select name as @r1 from customer
i wanted the solution to be as Title Samp1 Samp2 Samp3
But when io execute the above statement it displays with @r1 Samp1 Samp2 Samp3
The above solution needs to be made in an union all query with a diffrent sql statement.Please let me know how to retrieve the header value from variable and display it with the other column.Hope its clear or else please let know.I can explain in more depth. Thanks in advance.
declare @sql varchar(8000) select @sql = 'select name as [' + @r1 + '] from customer'
exec (@sql)
This has security implications though.
========================================== Cursors are useful if you don't know sql. SSIS can be used in a similar way. Beer is not cold and it isn't fizzy.
i need a solution for the following query. declare @r1 as varchar (5) set @r1='Title' select name as @r1 from customer
i wanted the solution to be as Title Samp1 Samp2 Samp3
But when io execute the above statement it displays with @r1 Samp1 Samp2 Samp3
The above solution needs to be made in an union all query with a diffrent sql statement.Please let me know how to retrieve the header value from variable and display it with the other column.Hope its clear or else please let know.I can explain in more depth. Thanks in advance.
The above code is not valid statement becuase you cannot use variable as alias name. Noty sure why you did not get any error. Also make sure to read this www.sommarskog.se/dynamic_sql.html