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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 display column name

Author  Topic 

sachingovekar
Posting Yak Master

101 Posts

Posted - 2009-10-08 : 01:26:35
Hi,
want to display column. How will the below work


declare @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 SQL

declare @string varchar(2000)
set @string = 'username'
exec('select' +'''richard'''+ 'as ' +@string)

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-10-08 : 02:43:47
quote:
Originally posted by senthil_nagore

Use Dynamic SQL

declare @string varchar(2000)
set @string = 'username'
exec('select' +'''richard'''+ 'as ' +@string)

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/



You dont need first two +s


declare @string varchar(2000)
set @string = 'username'
exec('select ''richard'' as ' +@string)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 work


declare @string varchar(2000)
set @string = 'username'
select 'richard' as ' '+@string+' '


Why are you passing column name as parameter?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 SQL

declare @string varchar(2000)
set @string = 'username'
exec('select' +'''richard'''+ 'as ' +@string)

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/



You dont need first two +s


declare @string varchar(2000)
set @string = 'username'
exec('select ''richard'' as ' +@string)

Madhivanan

Failing to plan is Planning to fail



Ok Madhi, Thanks

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page

sachingovekar
Posting Yak Master

101 Posts

Posted - 2009-10-08 : 03:30:47
want to display the dates as column names which will have quantity

create 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 FOR
SELECT distinct insertdate from #temp1
Order By insertdate
open device_cursor
FETCH NEXT FROM device_cursor
INTO @devicename

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
Select application,
max(case when insertdate = @devicename then quantity else 0 end) as +''+@devicename+''+
from #temp1
group by application

-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM device_cursor
INTO @devicename
END

CLOSE device_cursor
DEALLOCATE device_cursor
GO
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-10-08 : 03:59:44
Read about Cross-tab reports in sql server help file

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -