INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

(OP)
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!

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

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-

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

(OP)
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

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

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 

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

(OP)
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?

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

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.

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

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close