473,512 Members | 14,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inserting multiple rows with a single INSERT query

Atli
5,058 Recognized Expert Expert
Hi.

I've been trying to insert multiple rows into a table using a single INSERT statement in MSSQL / SQL Server 2005.
I could of course cheat and have my C# code insert each row using some sort of a loop, but where is the fun in that :-)

If you'r familliar with MySQL, this MySQL query would do what I am trying to do with MSSQL / SQL Server 2005.
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO 
  2.   UserInfo(InfoName, InfoValue)
  3. VALUES
  4.   ('Name1', 'value1'),
  5.   ('Name2', 'value2')
  6.   /* And so on... */
  7.  
I tried this, by the way, and it did not work with MSSQL 2005.

I'd be gratefull for any suggestions.

-Atli
May 22 '07 #1
3 28074
Atli
5,058 Recognized Expert Expert
The second I submitted the thread I figured this out :D

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO [UserInfo] (InfoName, InfoValue)
  2.     SELECT 'Name1', 'Value1'
  3. UNION ALL 
  4.     SELECT 'Name2', 'Value2'
  5. UNION ALL 
  6.     SELECT 'Name3', 'Value3'
  7.  
Still, if you have a better idea I'd like to hear it.

-Atli
May 22 '07 #2
almaz
168 Recognized Expert New Member
The second I submitted the thread I figured this out :D
Yes, this is one of possible solutions. Another one is to use XML (sample works on MSSQL 2005):
Expand|Select|Wrap|Line Numbers
  1. declare @UserInfo table (InfoName nvarchar(100), InfoValue  nvarchar(100))
  2.  
  3. declare @xml xml
  4. set @xml = '
  5. <root>
  6.     <user Name="User1" Value="Value1" />
  7.     <user Name="User2" Value="Value2" />
  8.     <user Name="User3" Value="Value3" />
  9.     <user Name="User4" Value="Value4" />
  10. </root>'
  11.  
  12. insert @UserInfo (InfoName, InfoValue)
  13. select UserData.value('@Name', 'nvarchar(100)'), UserData.value('@Value', 'nvarchar(100)')
  14. from @xml.nodes('/root/user') as Data(UserData) 
  15.  
  16. select * from @UserInfo
May 22 '07 #3
Atli
5,058 Recognized Expert Expert
Nice one.
Makes me wander how much I don't know about MSSQL :P
May 22 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
5502
by: Sans Spam | last post by:
Greetings! I have a table that contains all of the function permissions within a given application. These functions are different sections of a site and each has its own permissions (READ, WRITE,...
4
75830
by: Raj Kotaru | last post by:
Hi, In sqlplus, I can insert a single row using: insert into employee (name, salary, hiredate) values ('xyz', '86378', sysdate); Is there a modification of the insert command that will...
3
16307
by: JB | last post by:
To anyone that is able to help.... What I am trying to do is this. I have two tables (Orders, and OrderDetails), and my question is on the order details. I would like to set up a stored...
3
2530
by: gregory.sharrow | last post by:
I need to secure a datawarehouse table at the row level based on 1 to many keys on that table. A user should only see the rows they have access to. I need to be able to figure out which rows they...
1
4485
by: Scott Chapman | last post by:
I am working with Python (psycopg). I have HTML with embedded Python that I'm inserting into a database and it could contain any character. Single quotes, at least, must be escaped (to two...
2
4011
by: a | last post by:
NEW Post Here's my best guess at how to insert this dataset.... the code runs, but no new records are added to the sql table. I've read and split a delimited text file into a dataset. It...
5
6154
by: Arsen V. | last post by:
Hello, What is the optimal way to insert multiple rows (around 1000) from a web application into a table? The user enters multiple lines into a text box (up to 10,000). The ASP.NET...
5
3747
by: Robert Brown | last post by:
Hi All. I have a routine that checks a SQL Table for all records 3 months prior to a predetermined date, then I insert them into an Archive DB then delete those records from the original table....
7
3371
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.