Skip to content Skip to sidebar Skip to footer

How Can I Add "x" Rows To A Table In Sql?

If I have a table, let's say Customers and I want to receive from the user a number (Form) and create X rows in the Customer table. Let's say the customer put the number 4 I want 4

Solution 1:

You could use a stored procedure and then pass the number of customers you want added. Something like this...

createprocedure AddNewCustomers 
@NumberOfCustToAddintasdeclare@counterintset@counter=0 

while @counter<@NumberOfCustToAddbegin//put your insert statement here
    set@counter=@counter+1end

go

Then, call the procedure passing the number of customers that the user requested...

exec AddNewCustomers @NumberOfCustomersToCreate

Solution 2:

Is this what you want -

INSERT INTO Customers(Name)
VALUES(NULL)
GO 10

This will insert 10 rows which you can update later.

Solution 3:

Here you go, this will generate 4 identical rows from passed in variables. This will max out at 100 rows.

Un-comment and correct insert statement as you see fit.

DECLARE@IINT, @NAME NVARCHAR(10)
SET@NAME='HELEN'SET@I=4

;WITH MYCTE
AS
(
SELECT@NAMEAS NAME, X=1UNIONALLSELECT NAME, X+1FROM MYCTE
WHERE X<@I
)
-- INSERT INTO...SELECT*FROM MYCTE OPTION(MAXRECURSION 100)

Post a Comment for "How Can I Add "x" Rows To A Table In Sql?"