Solved

multiple inserts in one query or procedure

Posted on 2011-09-29
11
269 Views
Last Modified: 2012-05-12
is there a way to do a multiple insert to a table in sql
0
Comment
Question by:singsunn
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 33

Accepted Solution

by:
paulmacd earned 500 total points
Comment Utility
In the same row?  Yes  
In multiple rows with a common criteria?  Yes.

http://msdn.microsoft.com/en-us/library/aa933206(v=sql.80).aspx
0
 

Author Comment

by:singsunn
Comment Utility
into multiple rows with one common criteria.
0
 
LVL 8

Expert Comment

by:Crashman
Comment Utility
USE YourDB
GO
INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5
GO

Open in new window

0
 
LVL 8

Expert Comment

by:Crashman
Comment Utility
or
 INSERT INTO [t]
        (
          [camp1],
          [camp2],
          [camp3]
        )
 VALUES ( 1, 2, 3 ),
        ( 3, 4, 5 ),
        ( 6, 7, 8 ),
        ( 9, 10, 11 ),
        ( 12, 13, 14 )

Open in new window

0
 
LVL 33

Expert Comment

by:paulmacd
Comment Utility
Does...
     INSERT INTO TableName (Column1, Column2) VALUES (Value1, Value2) WHERE Criteria=Value
...not work?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:singsunn
Comment Utility
values to be inserted are read from an xml file, so i don't know how many set of values there going to be.
do i need dynamic sql query or is it possible to do this in stored procedure?
0
 
LVL 33

Expert Comment

by:paulmacd
Comment Utility
I think you'd have to build the query dynamically.  You might be able to do this with a stored proc, but I'm not sure.
0
 
LVL 8

Expert Comment

by:Crashman
Comment Utility
read this

http://blog.sqlauthority.com/2009/02/13/sql-server-simple-example-of-reading-xml-file-using-t-sql/

and use the
INSERT INTO TableX
select  field1,field2
FROM OPENXML ('Your parameters' )

Open in new window

0
 
LVL 33

Expert Comment

by:paulmacd
Comment Utility
Nice.  Didn't know about that...
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>>values to be inserted are read from an xml file, so i don't know how many set of values there going to be. <<
Instead of hypotheicals, why don't you post the schema (CREATE TABLE) to your table, a sample Xml file and how they are mapped (if it is not obvious) and we can give you a complete example using either OPENXML() if you are still using SQL Server 2000 compatibility or the newer Xml Data Type Methods if you are using SQL Server 2005 or greater.
0
 

Author Closing Comment

by:singsunn
Comment Utility
none
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now