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 |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2006-01-11 : 08:17:40
|
| Ink writes "I want my query to pull data like this:1 123A 11 123A 21 123A 31 123A 41 123A 51 123A 61 123A 71 123A 81 123A 91 123A 101 123A 111 123A 122 123A 12 123A 22 123A 32 123A 42 123A 52 123A 62 123A 72 123A 82 123A 92 123A 102 123A 112 123A 12...and so forth...but it's not doing it. It's only pulling the first record like below.1 123A 11 123A 21 123A 31 123A 41 123A 51 123A 61 123A 71 123A 81 123A 91 123A 101 123A 111 123A 12What must I add to pull data like my example above? Here's my t-sql.create table #tmpTbl (AcctID int, AcctNum varchar(20))insert into #tmpTbl values (1,'123A');insert into #tmpTbl values (2,'123B');insert into #tmpTbl values (3,'123C');insert into #tmpTbl values (4,'123D');insert into #tmpTbl values (5,'123E');DECLARE @acctid intDECLARE @acctnum varchar(10)DECLARE @counter intSET @counter=0DECLARE abc CURSOR FORSELECT * FROM #tmpTblOPEN abcFETCH NEXT FROM abc into @acctid, @acctnumWHILE (@@FETCH_STATUS = 0)BEGIN WHILE @counter < 12 BEGIN SET @Counter=@Counter+1 print cast(@acctid as varchar(2))+ ' ' + @acctnum + ' ' + cast(@counter as varchar(2)) END FETCH NEXT FROM abc INTO @acctid, @acctnumENDCLOSE abcDEALLOCATE abcGOdrop table #tmpTblThanks," |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-11 : 08:39:39
|
| place SET @counter = 0 before the WHILE @counter < 12.Why are you using cursor ? This can be achieve without using cursor.-----------------'KH'Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. |
 |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2006-01-11 : 09:02:04
|
| You've shown us your expected output and your actual output...BUT NOT your input data!!!! |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-11 : 20:18:41
|
Non cursor and while loop solutioncreate table #tmpTbl (AcctID int, AcctNum varchar(20))insert into #tmpTbl values (1,'123A');insert into #tmpTbl values (2,'123B');insert into #tmpTbl values (3,'123C');insert into #tmpTbl values (4,'123D');insert into #tmpTbl values (5,'123E');select * from #tmpTblcross join( select 1 as Num union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9 union all select 10 union all select 11 union all select 12) as norder by AcctID, AcctNum, Num -----------------'KH'Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. |
 |
|
|
|
|
|
|
|