• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

multiple inserts in one query or procedure

is there a way to do a multiple insert to a table in sql
0
singsunn
Asked:
singsunn
  • 4
  • 3
  • 3
  • +1
1 Solution
 
Paul MacDonaldDirector, Information SystemsCommented:
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
 
singsunnAuthor Commented:
into multiple rows with one common criteria.
0
 
CrashmanCommented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CrashmanCommented:
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
 
Paul MacDonaldDirector, Information SystemsCommented:
Does...
     INSERT INTO TableName (Column1, Column2) VALUES (Value1, Value2) WHERE Criteria=Value
...not work?
0
 
singsunnAuthor Commented:
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
 
Paul MacDonaldDirector, Information SystemsCommented:
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
 
CrashmanCommented:
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
 
Paul MacDonaldDirector, Information SystemsCommented:
Nice.  Didn't know about that...
0
 
Anthony PerkinsCommented:
>>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
 
singsunnAuthor Commented:
none
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now