June 3, 2008 at 12:57 pm
HI
can somebody help me with this query
i have table which has records like below
A 1
B 2
C 1
D 3
. .
. .
. .
. .
AND WOULD LIKE TO SOME THEM AS
A B C D . . . . .
1 2 3 4 . . . . .
TODAY IT IS ONLY A B C D COLUMNS
BUT TOMMORROW IT MIGH TBE A B C D E ... COLUMNS
IS THERE ANY DYNAMIC WAY OF DOING THIS
can somebody please help me on this
June 4, 2008 at 2:22 am
Hi,
i believe u need to use crosstab queries. Here is 2 link which will be useful for you.
http://www.sqlmag.com/Articles/Index.cfm?ArticleID=15608&DisplayTab=Article
http://www.simple-talk.com/sql/t-sql-programming/creating-cross-tab-queries-and-pivot-tables-in-sql/
regards,
vijay
June 4, 2008 at 10:21 pm
As far as I understood your problem...
CREATE TABLE [dbo].[tblABC](
[C1] [varchar](50) NULL,
[V1] [varchar](50) NULL
)
Declare @sSqlC varchar(1000)
select @sSqlC = COALESCE(@sSqlC + ',','') + ' ''' + cast(tblABC.[V1] as varchar(20)) + ''' As ' + C1 from tblABC
set @sSqlC = 'Select ' + @sSqlC
Exec(@sSqlC)
I hope it will help...
Atif Sheikh
June 6, 2008 at 6:52 am
If you'd like to avoid the unpleasantries of dynamic sql for crosstab queries check out the RAC utility. Coding dynamic crosstabs of complexity is like watching sausage made. It can take the appetite away from a normal human being. Save the prozac for something really important :). Check out RAC @
For developers who have smarts and common sense visit:
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply