May 20, 2010 at 1:11 pm
declare @sql nvarchar(2000)
set @sql='select col1,' + ' col2,' + ' col3,' + 'col4 from table'
exec (@sql)
try that. don't forget your commas seperating your fields.
May 20, 2010 at 2:35 pm
hello calvo
using the below query will give individual columns.
declare @sql nvarchar(2000)
set @sql='select col1,' + ' col2,' + ' col3,' + 'col4 from table'
exec (@sql)
actually i want to contatenate all the fields into one field
ex:
declare @sql nvarchar(2000)
set @sql='select (col1' + ' '+' col2' +' '+ ' col3,'+' ' + 'col4) as ttcol from table'
exec (@sql)
i think you understood my requirement.
Thanks
Rock..
May 20, 2010 at 2:59 pm
You didn't really provide much helpful information about what exactly you're trying to do, but if it's a simple string concatenation with a space between each field then it would be like this:
declare @sql nvarchar(2000)
set @sql='select col1 + '' '' + col2 + '' '' + col3 + '' '' + col4 from table'
exec(@sql)
May 20, 2010 at 4:21 pm
declare @sql varchar(2000)
set @sql = 'select firstName' + ' + ' + 'lastName ' + 'from Person.Contact'
exec (@sql)
that will concatenate fields into one column. I used SQL Server 2008 and the adventureworks database.
You can debug our dynamic sql by adding simple print statements to see what you are trying to execute.
for example;
declare @sql varchar(2000)
set @sql = 'select firstName' + ' + ' + 'lastName ' + 'from Person.Contact'
print @sql
exec (@sql)
May 21, 2010 at 6:30 am
calvo,
i did little work on this. it works now.Thank you for the support
Regards,
Rock..
Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy