473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Inserting multiple rows with a single INSERT INTO

Hi,

I have an application running on a wireless device and being wireless I
want it to use bandwidth as efficiently as possible. Therefore, I want
the SQL statement that it uploads to the SQL Server to be as efficient
as possible. In one instance, I give it four records to upload, which
currently I have as four seperate SQL statements seperated by a ";".
However, all the INSERT INTO... information is the same each time, the
only that changes is the VALUES portion of each command. Also, I have
to have the name of each column to receive the data (believe it or not,
these columns are only a small subset of the columns in the table).

Here is my current SQL statement:

INSERT INTO tblInvTransLog ( intType, strScreen, strMachine, strUser,
dteDate, intSteelRecID, intReleaseReceiptID, strReleaseNo, intQty,
dblDiameter, strGrade, HeatID, strHeatNum, strHeatCode, lngfkCompanyID)

VALUES (1, 'Raw Material Receiving', '[MachineNo]', '[CurrentUser]',
'3/21/2005', 888, 779, '2', 5, 0.016, '1018', 18, '610T142', 'K8',
520);
INSERT INTO tblInvTransLog ( intType, strScreen, strMachine, strUser,
dteDate, intSteelRecID, intReleaseReceiptID, strReleaseNo, intQty,
dblDiameter, strGrade, HeatID, strHeatNum, strHeatCode, lngfkCompanyID)

VALUES (1, 'Raw Material Receiving', '[MachineNo]', '[CurrentUser]',
'3/21/2005', 888, 779, '2', 9, 0.016, '1018', 30, '14841', 'B9', 344);
Since the SQL statement INSERT INTO portion remains the same every
time, it would be good if I could have the INSERT INTO portion only
once and then any number of VALUES sections, something like this:

INSERT INTO tblInvTransLog (intType, strScreen, strMachine, strUser,
dteDate, intSteelRecID, intReleaseReceiptID, strReleaseNo, intQty,
dblDiameter, strGrade, HeatID, strHeatNum, strHeatCode, lngfkCompanyID)
VALUES (1, 'Raw Material Receiving', '[MachineNo]',
'[CurrentUser]', '3/21/2005', 888, 779, '2', 5, 0.016, '1018', 18,
'610T142', 'K8', 520)
VALUES (1, 'Raw Material Receiving', '[MachineNo]',
'[CurrentUser]', '3/21/2005', 888, 779, '2', 9, 0.016, '1018', 30,
'14841', 'B9', 344);

But this is not a valid SQL statement. But perhaps someone with a more
comprehensive knowledge of SQL knows of way. Maybe there is a way to
store a string at the header of the command then use the string name in
each seperate command(??)

Jul 23 '05 #1
2 2252
Use UNION :

INSERT INTO tblInvTransLog (...)
SELECT ... UNION ALL
SELECT ... UNION ALL
... etc

Avoid using ambiguous local formats for date literals. The formatted
date strings in your code will cause an error under certain server or
client settings. As a bonus, the more reliable ISO format 'YYYYMMDD'
will save you an additional 2 characters per row!

--
David Portas
SQL Server MVP
--

Jul 23 '05 #2
A quick and easy way to cut down on the number of characters sent
across the line would be to turn this into a stored procedure instead
of using a direct insert. This is generally recommended anyway for
performance and security reasons. You would still have a few extra
characters with each call, but the number would be reduced. For
example, you could have:

EXEC tblInvTransLog_Insert 1, 'Raw Material Receiving', '[MachineNo]',
'[CurrentUser]', '3/21/2005', 888, 779, '2', 5, 0.016, '1018', 18,
'610T142', 'K8', 520;
EXEC tblInvTransLog_Insert 1, 'Raw Material Receiving', '[MachineNo]',
'[CurrentUser]', '3/21/2005', 888, 779, '2', 9, 0.016, '1018', 30,
'14841', 'B9', 344

You might want to use a more descriptive stored procedure name, but
hopefully you get the idea here.

HTH,
-Tom.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
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
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
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
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...
0
by: a | last post by:
I've read and split a delimited text file into a dataset. It looks fine in a datagrid (5 columns and 5,000 rows), but I've been trying, without success, to then insert the resulting dataset called...
2
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
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...
20
by: talktozee | last post by:
Hey, everyone! Basically, I need to insert *multiple rows* into table A from table B based upon some criteria, and I need to insert some static values along with each row from table A. For...
3
Atli
by: Atli | last post by:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.