How to insert multiple rows - a loop needed?

2019-04-11 12:58发布

问题:

I have the following statement:

insert into forecast_entry.user_role_xref
        ( user_master_id ,
          role_id ,
          created_date ,
          created_by
        )
values
        ( 276 , -- user_master_id - int
          101 , -- role_id - int
          getdate() , -- created_date - datetime
          'MICHAELSK'  -- created_by - varchar(20)
        )

I need to generate a row for role_id 101-355 (so the same statement above, except repeated with the role_id incrementing). What would be the best way to do this? To get the job done I'm intending on writing a quick C# application that will have a loop but I'm sure this isn't the best way and hope to learn something here to avoid having to do that in future (as I'm sure this kind of scenario is common).

回答1:

You should make use of numbers table and if you don't have one you can use master..spt_values like this:

insert into forecast_entry.user_role_xref
        ( user_master_id ,
          role_id ,
          created_date ,
          created_by
        )
select 276, -- user_master_id - int
       number, -- role_id - int
       getdate() , -- created_date - datetime
       'MICHAELSK'  -- created_by - varchar(20)
from master..spt_values
where type = 'P' and
      number between 101 and 355


回答2:

Here is what I use, just modify as needed. Here, I add a bunch of sequence numbers to a table using a loop variable:

USE MyDB

GO

DECLARE @MyCounter as INT

SET @MyCounter = 1  -- to use this multiple times you can just

                    -- change the starting number and run again
                    -- if you do not want duplicate numbers

WHILE @MyCounter < 1000  -- any value you want

 BEGIN

    INSERT INTO [MyDB].[dbo].[MyTable]
        ([NumberField])
    VALUES
        (@MyCounter)  -- insert counter value into table

        set @MyCounter = @MyCounter + 1;  -- increment counter

END


回答3:

In my opinion, the best way is to create stored procedure. In stored procedure you should make a loop, which would insert data into table. From your C# application you open a connection to DB, call once a stored procedure and close a connection. On SQL you get best perfomance working with big amount of data.

Here is an example



回答4:

if you create a loop in c# it will send same query again and again to database which is not a good idea.you rather create sp and loop there. as suggested by sham



回答5:

Instead of looping query Create a DataTable and create a stored procedure with User Defined Table Type

CREATE TYPE dtl AS TABLE
( 
          user_master_id INT ,
          role_id INT,
          created_date DATETIME,
          created_by varchar(20)

)

And Stored procedure

CREATE PROCEDURE SPNAME 
@dtl dtl READONLY
AS
INSERT INTO forecast_entry.user_role_xref
        ( user_master_id ,
          role_id ,
          created_date ,
          created_by
        )
SELECT 
          user_master_id ,
          role_id ,
          created_date ,
          created_by
FROM @dtl

Pass DatatTable for @dtl parameter of stored procedure which contains the proper data between 101-255