Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sachingovekar
Posting Yak Master
101 Posts |
Posted - 2009-10-08 : 01:26:35
|
Hi, want to display column. How will the below workdeclare @string varchar(2000)set @string = 'username'select 'richard' as ' '+@string+' ' |
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-10-08 : 01:35:49
|
Use Dynamic SQLdeclare @string varchar(2000)set @string = 'username'exec('select' +'''richard'''+ 'as ' +@string)Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-10-08 : 02:43:47
|
quote: Originally posted by senthil_nagore Use Dynamic SQLdeclare @string varchar(2000)set @string = 'username'exec('select' +'''richard'''+ 'as ' +@string)Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/
You dont need first two +sdeclare @string varchar(2000)set @string = 'username'exec('select ''richard'' as ' +@string)MadhivananFailing to plan is Planning to fail |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-10-08 : 02:44:09
|
quote: Originally posted by sachingovekar Hi, want to display column. How will the below workdeclare @string varchar(2000)set @string = 'username'select 'richard' as ' '+@string+' '
Why are you passing column name as parameter?MadhivananFailing to plan is Planning to fail |
|
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2009-10-08 : 02:49:14
|
quote: Originally posted by madhivanan
quote: Originally posted by senthil_nagore Use Dynamic SQLdeclare @string varchar(2000)set @string = 'username'exec('select' +'''richard'''+ 'as ' +@string)Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/
You dont need first two +sdeclare @string varchar(2000)set @string = 'username'exec('select ''richard'' as ' +@string)MadhivananFailing to plan is Planning to fail
Ok Madhi, Thanks Senthil.C------------------------------------------------------[Microsoft][ODBC SQL Server Driver]Operation canceledhttp://senthilnagore.blogspot.com/ |
|
|
sachingovekar
Posting Yak Master
101 Posts |
Posted - 2009-10-08 : 03:30:47
|
want to display the dates as column names which will have quantitycreate table #temp1(application varchar(100),insertdate varchar(20),quantity int)insert into #temp1 values ('adobe 1.1','2009-09-02',34)insert into #temp1 values ('adobe 1.2','2009-09-02',45)insert into #temp1 values ('adobe 1.1','2009-09-09',100)insert into #temp1 values ('adobe 1.2','2009-09-09',78)insert into #temp1 values ('adobe 1.1','2009-09-16',900)insert into #temp1 values ('adobe 1.2','2009-09-16',58)declare @devicename varchar(1000)DECLARE device_cursor CURSOR FORSELECT distinct insertdate from #temp1Order By insertdateopen device_cursorFETCH NEXT FROM device_cursorINTO @devicename -- Check @@FETCH_STATUS to see if there are any more rows to fetch.WHILE @@FETCH_STATUS = 0BEGIN Select application, max(case when insertdate = @devicename then quantity else 0 end) as +''+@devicename+''+from #temp1group by application -- This is executed as long as the previous fetch succeeds. FETCH NEXT FROM device_cursor INTO @devicename ENDCLOSE device_cursorDEALLOCATE device_cursorGO |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-10-08 : 03:59:44
|
Read about Cross-tab reports in sql server help fileMadhivananFailing to plan is Planning to fail |
|
|
|
|
|
|
|