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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Insert records automatically into table

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_serial

4001 100 105
4002 110 112



Now I want to create a table which should contain data as below:

PO_no Serial

4001 100
4001 101
4001 102
4001 103
4001 104
4001 105

4002 110
4002 111
4002 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]

Go to Top of Page

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 answer
I want 2nd table for the existing data in 1st table also
Go to Top of Page

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, serial
from cte
order by po_no, serial



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

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 table1
and 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, serial
from cte



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

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, serial
from cte
order 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 2
The statement terminated. The maximum recursion 100 has been exhausted before statement completion.
Go to Top of Page

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]

Go to Top of Page

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 thousands
this query is not working if no. of serial > 100
whts the solution?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-07-14 : 04:48:44
add this to the end of query

option (MAXRECURSION 10000)


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

rohit04413
Yak Posting Veteran

72 Posts

Posted - 2011-07-14 : 05:32:28
quote:
Originally posted by khtan

add this to the end of query

option (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,result

from 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, serial
from cte
order by productiono, serial
option (maxrecursion 1000);



How to inner join these @ condition
query1.productiono = query2.productiono
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2011-07-14 : 06:19:13
quote:
Originally posted by khtan

add this to the end of query

option (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 @table
SELECT '4001', 100, 105 UNION ALL
SELECT '4002', 110, 112

SELECT
a.PO_no,
Serial = b.number
FROM @table a
INNER JOIN master..spt_values b
ON b.number BETWEEN a.First_serial AND a.Last_serial
WHERE b.type = 'p'


- Lumbago
My blog-> http://thefirstsql.com/2011/07/08/how-to-find-gaps-in-identity-columns-at-the-speed-of-light/
Go to Top of Page

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 query

option (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 @table
SELECT '4001', 100, 105 UNION ALL
SELECT '4002', 110, 112

SELECT
a.PO_no,
Serial = b.number
FROM @table a
INNER JOIN master..spt_values b
ON b.number BETWEEN a.First_serial AND a.Last_serial
WHERE b.type = 'p'


- Lumbago
My 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?
Go to Top of Page

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.

- Lumbago
My blog-> http://thefirstsql.com/2011/07/08/how-to-find-gaps-in-identity-columns-at-the-speed-of-light/
Go to Top of Page
   

- Advertisement -