Need trigger for inserting data into table,after inserting data

Analyst_SQL 3,576 Reputation points
2022-03-28T11:05:15.5+00:00

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.

187359-image.png

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
SQL Server | Other
{count} votes

2 answers

Sort by: Most helpful
  1. Naomi Nosonovsky 8,881 Reputation points
    2022-03-29T04:06:15.267+00:00

    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.

    1 person found this answer helpful.

  2. 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


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.