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 |
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-07-14 : 03:05:26
|
Dear All,I have a table as below fields:1. PO_no (varchar)2. First_serial (int)3. Last_serial (int)Sample Data as below:PO_no First_serial Last_serial4001 100 1054002 110 112 Now I want to create a table which should contain data as below:PO_no Serial4001 1004001 1014001 1024001 1034001 1044001 1054002 1104002 1114002 112 How can I do that automatically? |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-07-14 : 03:16:04
|
MCQ :1. you want to create a new table automatically ?2. you want to automatic create records in the 2nd table when records are inserted into 1st table ?3. none of the above ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-07-14 : 03:24:00
|
quote: Originally posted by khtan MCQ :1. you want to create a new table automatically ?2. you want to automatic create records in the 2nd table when records are inserted into 1st table ?3. none of the above ? KH[spoiler]Time is always against us[/spoiler]
I think 2. is the answerI want 2nd table for the existing data in 1st table also |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-07-14 : 03:30:06
|
the following query will create the records into 2nd table; with cte as( select po_no, serial = first_serial, last_serial from tbl1 union all select po_no, serial = serial + 1, last_serial from cte where serial + 1 <= last_serial)select po_no, serialfrom cteorder by po_no, serial KH[spoiler]Time is always against us[/spoiler] |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-07-14 : 03:32:11
|
and to make it a auto, create an insert trigger on table1and with the following trigger code; with cte as( select po_no, serial = first_serial, last_serial from inserted union all select po_no, serial = serial + 1, last_serial from cte where serial + 1 <= last_serial)insert into tbl2 (po_no, serial)select po_no, serialfrom cte KH[spoiler]Time is always against us[/spoiler] |
 |
|
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-07-14 : 04:12:35
|
quote: Originally posted by khtan the following query will create the records into 2nd table; with cte as( select po_no, serial = first_serial, last_serial from tbl1 union all select po_no, serial = serial + 1, last_serial from cte where serial + 1 <= last_serial)select po_no, serialfrom cteorder by po_no, serial KH[spoiler]Time is always against us[/spoiler]
Dear K H Tan,I am getting following error:Msg 530, Level 16, State 1, Line 2The statement terminated. The maximum recursion 100 has been exhausted before statement completion. |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-07-14 : 04:16:58
|
what is the max no of serial you might have ?do you have bad data in there ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-07-14 : 04:24:11
|
quote: Originally posted by khtan what is the max no of serial you might have ?do you have bad data in there ? KH[spoiler]Time is always against us[/spoiler]
Wht do u mean by bad data?Well, serial runs into thousandsthis query is not working if no. of serial > 100whts the solution? |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-07-14 : 04:48:44
|
add this to the end of queryoption (MAXRECURSION 10000) KH[spoiler]Time is always against us[/spoiler] |
 |
|
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-07-14 : 05:32:28
|
quote: Originally posted by khtan add this to the end of queryoption (MAXRECURSION 10000) KH[spoiler]Time is always against us[/spoiler]
Thanks Sir.Just a little more.I have 2 queries. and i want to inner join the result of those 2 queries to form a single table. Plz tell me how can i do that?Query 1; with tblASmax as( select serialnumber, runno,process,side, result from ( select serialnumber,runno,process,result,side, row_no = row_number() over (partition by process,side,serialnumber order by runno desc) from tblAS union all select serialnumber,runlocal,process,result,NULL, row_no = row_number() over (partition by process,serialnumber order by runlocal desc) from tblFA ) m where m.row_no = 1 and process = '12')select k.productiono,k.qty,serialnumber,result from(select po.productiono,qty,partcode,serialnumber,resultfrom tblPO po left join tblASmax rs on po.firstsr <= rs.serialnumber and po.lastsr >= rs.serialnumber group by po.productiono,qty,partcode,serialnumber,result)k where k.productiono='5009073' Query 2; with cte as( select productiono, serial = firstsr, lastsr from tblpo where productiono = '5009073' union all select productiono, serial = serial + 1, lastsr from cte where serial + 1 <= lastsr)select productiono, serialfrom cte order by productiono, serialoption (maxrecursion 1000); How to inner join these @ condition query1.productiono = query2.productiono |
 |
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2011-07-14 : 06:19:13
|
quote: Originally posted by khtan add this to the end of queryoption (MAXRECURSION 10000)
khtan: this is a horrible advice! Especially when recursion is completely unnecessary. -> DECLARE @table table ( PO_no varchar(50), First_serial int, Last_serial int) INSERT INTO @tableSELECT '4001', 100, 105 UNION ALLSELECT '4002', 110, 112SELECT a.PO_no, Serial = b.numberFROM @table a INNER JOIN master..spt_values b ON b.number BETWEEN a.First_serial AND a.Last_serialWHERE b.type = 'p' - LumbagoMy blog-> http://thefirstsql.com/2011/07/08/how-to-find-gaps-in-identity-columns-at-the-speed-of-light/ |
 |
|
rohit04413
Yak Posting Veteran
72 Posts |
Posted - 2011-07-14 : 06:22:54
|
quote: Originally posted by Lumbago
quote: Originally posted by khtan add this to the end of queryoption (MAXRECURSION 10000)
khtan: this is a horrible advice! Especially when recursion is completely unnecessary. -> DECLARE @table table ( PO_no varchar(50), First_serial int, Last_serial int) INSERT INTO @tableSELECT '4001', 100, 105 UNION ALLSELECT '4002', 110, 112SELECT a.PO_no, Serial = b.numberFROM @table a INNER JOIN master..spt_values b ON b.number BETWEEN a.First_serial AND a.Last_serialWHERE b.type = 'p' - LumbagoMy blog-> http://thefirstsql.com/2011/07/08/how-to-find-gaps-in-identity-columns-at-the-speed-of-light/
Thanks Lumbago,Could you please tell me how to inner join 2 queries shown above? |
 |
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2011-07-14 : 06:39:49
|
If you can post the original queries you want to combine without all the recursion stuff I'll give it a go.- LumbagoMy blog-> http://thefirstsql.com/2011/07/08/how-to-find-gaps-in-identity-columns-at-the-speed-of-light/ |
 |
|
|
|
|
|
|