How to insert multiple row into a table?

rabi shakeel 1 Reputation point
2022-04-10T17:43:53.523+00:00

I am inserting data into Probale, Then I want to insert 10 rows (depend on Qty) get insert into #tbl_PckDetail,Prdno value will be same

CREATE TABLE #Probale (prdno INT,orderno int,CodeItem int,Weigth int,prdqty int,EntryDate date,DelID int,PID int)

   INSERT INTO #Probale VALUES(10000,15,10,270,1,'2020-10-21',null,111)   
 Create table #tbl_PckDetail (DID int, prdno int,Qty int ,delid int)  

191557-190242-image.png

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,708 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
{count} votes

1 answer

Sort by: Most helpful
  1. LiHong-MSFT 10,046 Reputation points
    2022-04-11T02:28:36.24+00:00

    Hi @rabi shakeel
    Check this:

    CREATE TABLE #Probale (prdno INT,orderno int,CodeItem int,Weigth int,prdqty int,EntryDate date,DelID int,PID int)   
    INSERT INTO #Probale VALUES(10000,15,10,270,1,'2020-10-21',null,111)   
    Create table #tbl_PckDetail (DID int, prdno int,Qty int ,delid int)  
      
    ;WITH CTE AS  
    (  
     SELECT 1 AS DID,Prdno,prdqty AS QTY,DelID  
     FROM #Probale  
     WHERE prdqty =1  
     UNION ALL  
     SELECT DID+1 AS DID,Prdno,QTY,DelID  
     FROM CTE  
     WHERE DID<10  
    )  
    INSERT INTO #tbl_PckDetail   
    SELECT * FROM CTE  
      
    SELECT * FROM #tbl_PckDetail  
    

    You can also find solutions from other experts in this thread:Insert Multiple Row into Table

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments