June 26, 2009 at 7:01 am
i have a err_dtl with columns, clm_id,busn.
clm_id busn
------ ----
111 b01
112 b02
113 b03
.... ....
I want to display the values in columnwise rathere than row-wise like this.
clm_id busn clm_id busn clm_id busn
----- ----- ------ ----- ----- -----
111 b01 112 b02 113 b03
I used the following code:
----------------------------
Declare @t table
(clm_id numeric(18,0),
BUSN varchar(20)
)
insert into @t (clm_id,BUSN)
select clm_id,BUSN from err_dtl
Select clm_id,BUSN from @t
Declare @s-2 varchar(8000)
declare @n int
select @n = 1
select @s-2 = COALESCE(@s + ',', '') + cast(clm_id as varchar(10)) + 'clm_id'+cast(@n as varchar(100)),
@n = @n + 1
from @t
print @s-2
Exec('Select '+@s)
declare @p int
select @p = 1
declare @b-2 varchar(8000)
select @b-2 = COALESCE(@b + ',', '') + cast(BUSN as varchar(10)) + 'BUSN'+cast(@p as varchar(100)),
@p = @p + 1
from @t
print @b-2
Exec('Select '+@b)
I got the following error:
Msg 207, Level 16, State 3, Line 1
Invalid column name 'B01BUSN1'.
Msg 207, Level 16, State 3, Line 1
Invalid column name 'B01BUSN2'.
Msg 207, Level 16, State 3, Line 1
Invalid column name 'B03BUSN3'.
Why this error occurs?
July 8, 2009 at 3:05 am
Sorry! I tried a different idea and it worked:-)
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply