| Author |
Topic |
|
qman
Constraint Violating Yak Guru
442 Posts |
Posted - 2011-09-23 : 12:22:38
|
| I have a cursor which when run displays the following:NULL NULL NULL NULLLEVEL0 LEVEL0 LEVEL0 LEVEL0 LEVEL0 LEVEL0 LEVEL1 aa aa aa LEVEL1 c c c LEVEL1 b b b LEVEL1 a a a LEVEL1 cc cc cc LEVEL1 bb bb bb I am trying get it to display one level0 per multiple level1's as shown below. I think I am close, and am hoping someone can tweak the cursor to get it to behave like I want. Code also shown below.Thanks in advance!LEVEL0 LEVEL1 a a aLEVEL1 aa aa aaaLEVEL0 LEVEL1 b b bLEVEL1 bb bb bbLEVEL0 LEVEL1 c c cLEVEL1 cc cc ccdeclare @qValue nvarchar(60), @a varchar(3), @b varchar(3), @c varchar(3)-- create temp table 1create table #aTABLE (a char(3), b char(3), c char(3) )-- populate temp tableinsert into #aTABLEselect 'a', 'a', 'a'union allselect 'b', 'b', 'b'union allselect 'c', 'c', 'c'union allselect 'aa', 'aa', 'aa'union allselect 'bb', 'bb', 'bb'union allselect 'cc', 'cc', 'cc'-- create temp table 2select 'cola' = @qValue, 'colb' = @qValue, 'colc' = @qValue, 'cold' = @qValue into #aTABLE2-- create cursor for displaydeclare TEST cursor forselect a, b, c from #aTABLEopen TESTfetch next from TEST into @a, @b, @cwhile @@fetch_status = 0begin insert #aTABLE2(cola, colb, colc, cold) select 'LEVEL0', '', '', '' insert #aTABLE2(cola, colb, colc, cold) select 'LEVEL1', @a, @b, @c fetch next from TEST into @a, @b, @cendclose TESTdeallocate TESTselect * from #aTABLE2 order by cola |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2011-09-23 : 13:37:28
|
forget the cursor - how's this:with cte (cola, colb, colc, cold, grp) as ( select 'LEVEL1' ,a ,b ,c ,left(a,1) from #aTable union all select distinct 'LEVEL0' ,' ' ,' ' ,' ' ,left(a, 1) from #aTable)select cola, colb, colc, coldfrom cteorder by grp, colbOUTPUT:cola colb colc cold------ ---- ---- ----LEVEL0 LEVEL1 a a a LEVEL1 aa aa aa LEVEL0 LEVEL1 b b b LEVEL1 bb bb bb LEVEL0 LEVEL1 c c c LEVEL1 cc cc cc Be One with the OptimizerTG |
 |
|
|
qman
Constraint Violating Yak Guru
442 Posts |
Posted - 2011-09-23 : 13:51:10
|
| Thanks TG!!!!!!!!The CTE looks good. Let me apply it to my real tables and see how the data looks.. |
 |
|
|
qman
Constraint Violating Yak Guru
442 Posts |
Posted - 2011-09-23 : 14:38:07
|
| Any chances you can help me with the cursor route?It would be nice to see how each one compares... |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2011-09-26 : 10:55:08
|
Ok - please report back any performance differences as well as how many rows in your tables. This output is identical with the cte solution based on the data in your original post.set nocount ondeclare @qValue nvarchar(60), @a varchar(3), @b varchar(3), @c varchar(3)-- create temp table 1create table #aTABLE (a char(3), b char(3), c char(3) )-- populate temp tableinsert into #aTABLEselect 'a', 'a', 'a'union allselect 'b', 'b', 'b'union allselect 'c', 'c', 'c'union allselect 'aa', 'aa', 'aa'union allselect 'bb', 'bb', 'bb'union allselect 'cc', 'cc', 'cc'-- create temp table 2select 'cola' = @qValue, 'colb' = @qValue, 'colc' = @qValue, 'cold' = @qValue, 'grp' as grp into #aTABLE2 where 1=2-- create cursor for displaydeclare TEST cursor forselect a, b, c from #aTABLEopen TESTfetch next from TEST into @a, @b, @cwhile @@fetch_status = 0beginif left(@a,1) = @abegin insert #aTABLE2(cola, colb, colc, cold, grp) select 'LEVEL0', '', '', '', @aendinsert #aTABLE2(cola, colb, colc, cold, grp)select 'LEVEL1', @a, @b, @c, left(@a,1)fetch next from TEST into @a, @b, @cendclose TESTdeallocate TESTselect cola, colb, colc, cold from #aTABLE2 order by grp, colb Be One with the OptimizerTG |
 |
|
|
|
|
|