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. -
INSERT INTO
-
UserInfo(InfoName, InfoValue)
-
VALUES
-
('Name1', 'value1'),
-
('Name2', 'value2')
-
/* And so on... */
-
I tried this, by the way, and it did not work with MSSQL 2005.
I'd be gratefull for any suggestions.
-Atli
3 28074 Atli 5,058
Recognized Expert Expert
The second I submitted the thread I figured this out :D -
INSERT INTO [UserInfo] (InfoName, InfoValue)
-
SELECT 'Name1', 'Value1'
-
UNION ALL
-
SELECT 'Name2', 'Value2'
-
UNION ALL
-
SELECT 'Name3', 'Value3'
-
Still, if you have a better idea I'd like to hear it.
-Atli
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): - declare @UserInfo table (InfoName nvarchar(100), InfoValue nvarchar(100))
-
-
declare @xml xml
-
set @xml = '
-
<root>
-
<user Name="User1" Value="Value1" />
-
<user Name="User2" Value="Value2" />
-
<user Name="User3" Value="Value3" />
-
<user Name="User4" Value="Value4" />
-
</root>'
-
-
insert @UserInfo (InfoName, InfoValue)
-
select UserData.value('@Name', 'nvarchar(100)'), UserData.value('@Value', 'nvarchar(100)')
-
from @xml.nodes('/root/user') as Data(UserData)
-
-
select * from @UserInfo
Atli 5,058
Recognized Expert Expert
Nice one.
Makes me wander how much I don't know about MSSQL :P
Sign in to post your reply or Sign up for a free account.
Similar topics |
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,...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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....
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |