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 |
|
nhess80
Yak Posting Veteran
83 Posts |
Posted - 2010-10-07 : 13:17:31
|
| Below I have code that goes through a table and where it finds that there is a number with 6 digits in length its adds a 0,1,2,3.... to the endEXAMPLE217728Since this is 6 digits long it’s going to take that and loop this2177281217728221772832177284217728521772862177287217728821772892177280Which is what I want to do but the way I have it setup it creates these in 10 different result sets.Is there a way so that it loops through and creates these in one result set??declare @counter varchar(2)set @counter = 0while @counter < 10 begin Select CONVERT(varchar(7),Victory_GlobalX_npanxx+''+@counter),Victory_GlobalX_inter,Victory_GlobalX_intraFROM Victory_GlobalXWhere LEN(Victory_GlobalX_npanxx) = 6 set @counter = @counter + 1endThanks for your help,Nick |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2010-10-07 : 13:39:27
|
Such as:;WITH Tally(n) AS (SELECT 0 UNION ALL SELECT n+1 FROM Tally WHERE n<9)Select Victory_GlobalX_npanxx+CAST(n as varchar), Victory_GlobalX_inter, Victory_GlobalX_intraFROM Victory_GlobalXCROSS JOIN TallyWhere LEN(Victory_GlobalX_npanxx) = 6 |
 |
|
|
nhess80
Yak Posting Veteran
83 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2010-10-07 : 14:15:01
|
Awwww, CTEs are da bomb diggety. I'm actually having trouble NOT writing them these days. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-10-08 : 04:40:15
|
quote: Originally posted by robvolk Such as:;WITH Tally(n) AS (SELECT 0 UNION ALL SELECT n+1 FROM Tally WHERE n<9)Select Victory_GlobalX_npanxx+CAST(n as varchar), Victory_GlobalX_inter, Victory_GlobalX_intraFROM Victory_GlobalXCROSS JOIN TallyWhere LEN(Victory_GlobalX_npanxx) = 6
Man that had me scratching my head for a while. I was wondering why you needed to use a recursive call to populate the CTE when a straight select from tally would do the job. It took me a while to work out that this;WITH Tally AS (SELECT [n]-1 AS [n] FROM tally WHERE n <= 10)SELECT * FROM Tally Which results in:Msg 252, Level 16, State 1, Line 2Recursive common table expression 'Tally' does not contain a top-level UNION ALL operator. Needed to be this;WITH Tally AS (SELECT [n]-1 AS [n] FROM dbo.tally WHERE n <= 10)SELECT * FROM Tally Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
|
|
|
|
|