June 7, 2009 at 1:13 am
I have a Column with some values as shown below
Table A
C1
3
4
5
6
I have to generate total 18records with some autogenerated ID column in table B
As the frist value of C is 3 i have to generate 3 records like wise for all the values i have to generate that many number of records in table B
Plz suggest a solution for the ablve scenarioo with out using cursors
Thanks
Priya
June 7, 2009 at 2:17 am
Hi Priya,
would you please show us what you've tried so far so we can help you to make your code performing better.
I think it's a better idea than just throw your request in and wait for the coding result from us...
What you're looking for seems like a job for the Tally table. You might want to search for it in this forum or start with http://www.sqlservercentral.com/articles/T-SQL/62867/.
Please read the mentioned article and try to apply it to your situation. If you're struggling with it, get back here and we'll help you to understand it and make it work.
June 7, 2009 at 3:54 am
I have gone through the example
But my concern is how can we loop through all the values of a column with out having cursor in the above example?
Thanks,
Priya.
June 7, 2009 at 11:03 am
Indu (6/7/2009)
I have gone through the exampleBut my concern is how can we loop through all the values of a column with out having cursor in the above example?
Thanks,
Priya.
As Lutz indicated, a tally table is probably what you need. My problem is I'm not sure what you are expecting as output based on the data you have provided.
If you could show us the expected results based on the data in your original post that would be more helpful. Please realize, some of us need a visual clue as to what you are attempting. Kust giving a description isn't always enough information.
June 7, 2009 at 1:23 pm
Since you "investigated" what a tally table is, the following code should be self explaining. It will return 18 lines as requested.
-- build sample table and insert data
DECLARE @a TABLE (C1 INT)
INSERT INTO @a
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6
-- select the requested 18 rows (3*'3' + 4*'4' + 5*'5' + 6+'6')
SELECT a.C1 FROM @a a
INNER JOIN tally ON a.c1 >= tally.n
June 7, 2009 at 3:02 pm
Indu (6/7/2009)
I have gone through the example
Heh... is it just me or does that really sound pompous and arrogant? 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
June 7, 2009 at 3:07 pm
Jeff Moden (6/7/2009)
Indu (6/7/2009)
I have gone through the exampleHeh... is it just me or does that really sound pompous and arrogant? 😉
Nope, it isn't just you. 😉
June 7, 2009 at 3:14 pm
Jeff Moden (6/7/2009)
Indu (6/7/2009)
I have gone through the exampleHeh... is it just me or does that really sound pompous and arrogant? 😉
No, i wouldn't say that it was pompous or arrogant (I've used this phrase before, so I hope it isn't). Of course, I wouldn't say that it sounds credible either.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
June 7, 2009 at 3:34 pm
I was wondering about his reply too, since the two statements (regarding the article and the correlation to the original subject) within one post don't seem to match up very well...
I'm also a little confused what kind of business case would require a coding like it's been asked for. I'm under the impression that the "business case" is a little more theoretical than real life.
But other than the OP no one knows so far, or do you have had any need in the past to solve an issue like this? Just being curious...
June 7, 2009 at 6:53 pm
lmu92 (6/7/2009)
I'm also a little confused what kind of business case would require a coding like it's been asked for. I'm under the impression that the "business case" is a little more theoretical than real life.
Well, homework is one possibility.
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
June 7, 2009 at 8:43 pm
RBarryYoung (6/7/2009)
Jeff Moden (6/7/2009)
Indu (6/7/2009)
I have gone through the exampleHeh... is it just me or does that really sound pompous and arrogant? 😉
No, i wouldn't say that it was pompous or arrogant (I've used this phrase before, so I hope it isn't). Of course, I wouldn't say that it sounds credible either.
Put the words back into the context they were offered. OP is basically saying "I already told ya sucker... now fix my problem."
--Jeff Moden
Change is inevitable... Change for the better is not.
June 7, 2009 at 9:01 pm
lmu92 (6/7/2009)
I was wondering about his reply too, since the two statements (regarding the article and the correlation to the original subject) within one post don't seem to match up very well...I'm also a little confused what kind of business case would require a coding like it's been asked for. I'm under the impression that the "business case" is a little more theoretical than real life.
But other than the OP no one knows so far, or do you have had any need in the past to solve an issue like this? Just being curious...
I seen this type of problem many times. Normally, it comes with a start and end date and someone wants to generate a row for each date in the range.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 8, 2009 at 12:26 pm
Jeff Moden (6/7/2009)
I seen this type of problem many times. Normally, it comes with a start and end date and someone wants to generate a row for each date in the range.
Well, in that case we'd talk about two columns (start date and end date) which would be almost a "standard case".
But here the request is based on a given number in a single column. This almost looks like a trial of reverse engineering of a grouped-by result table. I think Barry's guess is a lot closer... 😉
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply