Insert multiple row using store Procedure in table

Analyst_SQL 3,576 Reputation points
2021-12-31T12:28:38.877+00:00

below is store procedure

USE [SilverRags]
GO
/****** Object:  StoredProcedure [dbo].[SP_insert_Bale]    Script Date: 12/31/2021 5:02:50 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Proc [dbo].[SP_insert_Bale_M]


@Bale_ID varchar(12)=null,
@item_code int=null,
@Bale_Weight decimal(10,2)=null,
@Bale_Qty int=null,
@Bale_Date date=null,
@Bale_Time time(7)=null,
@IPAddress varchar(50)=null,
@Bale_type int=null,
@Empid int=null,
@Machineno int=null

as begin


Insert into tbl_Bale_prd(bale_id,item_code,bale_weight,Bale_qty,Bale_Date,Bale_time,IpAddress,empid,Bale_Type,Machine_no) values

(@bale_id,@item_code,@bale_weight,@Bale_qty,@Bale_Date,@Bale_time,@IpAddress,@empid,@Bale_Type,@Machineno)


end

IF i mentioned Bale_qty 10 ,then 10 row insert into table.

Developer technologies Transact-SQL
SQL Server Other
{count} votes

Accepted answer
  1. LiHong-MSFT 10,056 Reputation points
    2022-01-03T03:04:15.563+00:00

    Hi,@Analyst_SQL
    About this sentence “IF i mentioned Bale_qty 10 ,then 10 row insert into table.”
    Do you mean that when the @bale _qty is 10, insert 10 rows of the same data, when the @bale _qty is 5, insert 5 rows of the same data.
    If so,please check this:

    ALTER PROC dbo.SP_insert_Bale_M     
     ...  
    AS   
    BEGIN  
    INSERT INTO #tbl_Bale_prd  
    SELECT TOP (@Bale_qty) @bale_id,@item_code,@bale_weight,@Bale_qty,@Bale_Date,@Bale_time,@IpAddress,@empid,@Bale_Type,@Machineno  
    FROM master.dbo.syscolumns  
    END  
    EXEC dbo.SP_insert_Bale_M  @Bale_Qty =10  
    

    If not, please share us your detailed result.

    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.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Ronen Ariely 15,206 Reputation points
    2021-12-31T13:11:59.83+00:00

    Good day and happy new year akhter < @Analyst_SQL >

    Your description is not clear but I will make a guess :-)

    If this not fit your needs then please elaborate more as provide exact sample of what you want to get

    So... my guess is that you want to have input whi8ch include multiple values for multiple rows which you want t insert the table. In other words you want to send multiple rows of data to the stored procedure as input.

    If this is the case then you should simply use Table-Valued Parameter as input for the SP. This way you can INSERT all the rows in the Table-Valued Parameter to your table.

    You can read more about Table-Valued Parameter and how to use it in the documentation:

    https://learn.microsoft.com/en-us/sql/relational-databases/tables/use-table-valued-parameters-database-engine

    Instead of declaring multiple constant parameters like you have

     @Bale_ID varchar(12)=null,  
     @item_code int=null,  
     @Bale_Weight decimal(10,2)=null,  
     @Bale_Qty int=null,  
     @Bale_Date date=null,  
     @Bale_Time time(7)=null,  
     @IPAddress varchar(50)=null,  
     @Bale_type int=null,  
     @Empid int=null,  
     @Machineno int=null  
    

    You simply use a single Table-Valued Parameter

    Check the link above. You have a full example there

    0 comments No comments

Your answer

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