What is the parent and what is the child in your tables? It looks to me (I may be wrong) that tbl_BalPackM is the parent to other tables. If this is the case, then we need to insert information into this table first and then the rest of the tables.
Need trigger for inserting data into table,after inserting data
Below is table
CREATE TABLE #Probale (prdno INT,orderno int,CodeItem int,Weigth int,prdqty int,EntryDate date,DelID int,PID int)
Create table #tbl_BalPacM (PID int ,Orderno int,del int)
Create table #tbl_PckDetail (DID int,PID int, prdno int,Qty int ,delid int)
I want, when i insert data into #Probale table ,when trigger get fire and insert data into #Tbl_BalPacm and #tbl_PckDetail table.
INSERT INTO #Probale VALUES(10000,15,1,270,1,'2020-10-21',null,111)
INSERT INTO #Probale VALUES(10001,15,2,270,1,'2020-10-21',null,111)
I am generating PID using Store procedure
Create Proc [dbo].[SP_Generate_Pack_ID]
as begin
;with cte as (select 10001 as PID
UNION ALL
select top (1) PID + 1 as PID
from tbl_BalPacM t order by t.PID DESC)
select top 1 PID
from cte ORDER BY PID DESC
output.
Developer technologies | Transact-SQL
SQL Server | Other
2 answers
Sort by: Most helpful
-
-
Analyst_SQL 3,576 Reputation points
2022-03-29T08:07:10.17+00:00 First table is below,
CREATE TABLE #Probale (prdno INT,orderno int,CodeItem int,Weigth int,prdqty int,EntryDate date,DelID int,PID int)when data get inserted into #Probale table, then trigger will fire and data get insert into
table (#tbl_BalPacM ) it is Parent table of #tbl_PckDetail,
Means PID will insert in tbl_BalPacM and tbl_PckDetail as well,then Prdno from #probale table will get insert simultaneously,
Note: I am using Store Procedure for generating PID