Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Insert multiple rows of data (at once) into existing Database Table

Status
Not open for further replies.

mhamlett

Programmer
Oct 20, 2014
10
0
0
US
Hello

I am looking to add multiple column entries to existing database table by way of script, i.e.;


Column1-- Column2-- Column3-- Column4

12345----- Testing---- Testing---- Testing


How can I repeat/input this exact data for 500 rows in a single script?

The End User will be using a form with 4 textboxes followed by a submit button. txtbox1 = Column1, and so on...
Lets say Column4 holds a value of qty. which will equal the total amount of rows I would like added to the table, any idea how i could accomplish this?

Feel free to ask any questions you may have

Thanks in advance!
 
Not sure I fully understand your requirement, but here is a quick shot at what I think you are asking.

Code:
SELECT TOP 500
	'12345' AS Column1
	,'Testing' AS Column2
	,'Testing' AS Column3
	,'Testing' AS Column4
FROM sys.all_objects

This gives me 500 rows of 12345, Testing, Testing, Testing

-Jim-
 
Jim

Thanks for your help with this! This script does create the columns just as I needed, but I cannot figure out how to run script on an existing table so that the data is inserted into the columns under specified table.

Thanks
 
Does this do what you want?

Code:
INSERT INTO ExistingTable (KeyCol, FirstCol, SecondCol, ThirdCol)
     SELECT TOP 500
	'12345'
	,'Testing'
	,'Testing'
	,'Testing'
     FROM sys.all_objects

 
That worked GREAT!

What if I need to have one of those columns 'auto-number' and I cannot use primary key for this because I will have several different INSERTS that will have replicated values?
 
Check out the CREATE SEQUENCE syntax. That might do what you want, but it won't work with TOP so you'll have to work around that. But some fiddling might yield the desired results.
 
I should have mentioned that SEQUENCE is only available on SQL Server 2012 and up. So you won't be able to use this on earlier versions. If that is the case, I don't know how to do this on an INSERT but it can be done in an UPDATE. If you were to insert these rows first with some unique tag or value in your counter column you could then go back and update that counter with a sequential number, something like:

Code:
DECLARE @Counter int = 0
UPDATE Tbl
SET @Counter = CounterCol = @Counter + 1
WHERE CounterCol = 0

..assuming you used a zero value in CounterCol during the insert to identify rows to be sequenced. You can set your initial count value to whatever you want (perhaps finding the current MAX value from earlier rows) and can increment the counter by values other than 1 if that is appropriate.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top