473,789 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

INSERT - one record at a time

Hi everyone:

Using Sql Server SQL 8
I'm trying to INSERT records into a "can software package" batch
table. I have a work-table that mimics the batch table. After
manipulating the records in the work-file I want to INSERT them into
the batch table.

The problem is the batch table in the can software has a trigger on
the batch table which is going to force me to INSERT one record at a
time. I've always been able to do an INSERT with no problem.

The batch table has pretty basic columns:
BatchID
BatchDate
SeqNumber
These three fields are the key and then just some miscellaneous
columns. Any easy way to loop thru my work-file to insert these
records. Never done a loop in SQL so an example would be really
really appreciated. I have a sequence number so I was hoping to do a
While loop but I really don't know enough about creating a loop to
make that call. Thanks in advance for any help.

May 2 '07 #1
6 10969
On May 2, 2:09 pm, eighthman11 <rdshu...@noote r.comwrote:
Hi everyone:

Using Sql Server SQL 8
I'm trying to INSERT records into a "can software package" batch
table. I have a work-table that mimics the batch table. After
manipulating the records in the work-file I want to INSERT them into
the batch table.

The problem is the batch table in the can software has a trigger on
the batch table which is going to force me to INSERT one record at a
time. I've always been able to do an INSERT with no problem.

The batch table has pretty basic columns:
BatchID
BatchDate
SeqNumber
These three fields are the key and then just some miscellaneous
columns. Any easy way to loop thru my work-file to insert these
records. Never done a loop in SQL so an example would be really
really appreciated. I have a sequence number so I was hoping to do a
While loop but I really don't know enough about creating a loop to
make that call. Thanks in advance for any help.
Google "SQL Cursors". Personally I hate cursors and use a WHILE loop:

DECLARE @ValueDataType

WHILE (SELECT COUNT(*) FROM Table) 0
BEGIN

SET @Value = distinct value from Table

INSERT INTO Table2
SELECT *
FROM Table
WHERE Value = @Value

DELETE Table WHERE Value = @Value

END

Somebody can probably produce the same code using a cursor. I just
choose to ignore them. heh.

-Utah
May 2 '07 #2
eighthman11 (rd******@noote r.com) writes:
Using Sql Server SQL 8
I'm trying to INSERT records into a "can software package" batch
table. I have a work-table that mimics the batch table. After
manipulating the records in the work-file I want to INSERT them into
the batch table.

The problem is the batch table in the can software has a trigger on
the batch table which is going to force me to INSERT one record at a
time. I've always been able to do an INSERT with no problem.
Ouch! Apparently someone did not know how to write a set-based trigger.
The batch table has pretty basic columns:
BatchID
BatchDate
SeqNumber
These three fields are the key and then just some miscellaneous
columns. Any easy way to loop thru my work-file to insert these
records. Never done a loop in SQL so an example would be really
really appreciated. I have a sequence number so I was hoping to do a
While loop but I really don't know enough about creating a loop to
make that call. Thanks in advance for any help.
They say cursors are evil, and that's true: iterative solutions are
almost always magnitudes slower than set-based. But when you need to
iterate, cursors is usually the best solution.

DECLARE @batchid int,
@batchdate datetime,
@seqnumber int,
....

DECLARE cur CURSOR STATIC LOCAL FOR
SELECT batchid, batchdate, seqnumer, ....
FROM yourworktable
-- ORDER BY if you like

OPEN cur

WHILE 1 = 1
BEGIN
FETCH cur INTO @batchid, @batchdate, @seqnumer, ...
IF @@fetch_status <0
BREAK

INSERT targettable (...)
VALUES (....)
END

DEALLOCATE cur

Notes:

DECLARE CURSOR - creates the cursor.
STATIC - the result set for the cursor is defined once for all into
tempdb.
LOCAL - Cursor is visible in current scope only.

OPEN - Opens the cursor.

FETCH - get next from the cursor.

@@fetch_status - 0 as long as there are more rows in the pipeline.

DEALLOCATE - deletes the cursor.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
May 2 '07 #3
On May 2, 4:36 pm, Erland Sommarskog <esq...@sommars kog.sewrote:
eighthman11 (rdshu...@noote r.com) writes:
Using Sql Server SQL 8
I'm trying toINSERTrecords into a "can software package" batch
table. I have a work-table that mimics the batch table. After
manipulating the records in the work-file I want toINSERTthem into
the batch table.
The problem is the batch table in the can software has a trigger on
the batch table which is going to force me toINSERTone record at a
time. I've always been able to do anINSERTwith no problem.

Ouch! Apparently someone did not know how to write a set-based trigger.
The batch table has pretty basic columns:
BatchID
BatchDate
SeqNumber
These three fields are the key and then just some miscellaneous
columns. Any easy way to loop thru my work-file toinsertthese
records. Never done a loop in SQL so an example would be really
really appreciated. I have a sequence number so I was hoping to do a
While loop but I really don't know enough about creating a loop to
make that call. Thanks in advance for any help.

They say cursors are evil, and that's true: iterative solutions are
almost always magnitudes slower than set-based. But when you need to
iterate, cursors is usually the best solution.

DECLARE @batchid int,
@batchdate datetime,
@seqnumber int,
....

DECLARE cur CURSOR STATIC LOCAL FOR
SELECT batchid, batchdate, seqnumer, ....
FROM yourworktable
-- ORDER BY if you like

OPEN cur

WHILE 1 = 1
BEGIN
FETCH cur INTO @batchid, @batchdate, @seqnumer, ...
IF @@fetch_status <0
BREAK

INSERTtargettab le (...)
VALUES (....)
END

DEALLOCATE cur

Notes:

DECLARE CURSOR - creates the cursor.
STATIC - the result set for the cursor is defined once for all into
tempdb.
LOCAL - Cursor is visible in current scope only.

OPEN - Opens the cursor.

FETCH - get next from the cursor.

@@fetch_status - 0 as long as there are more rows in the pipeline.

DEALLOCATE - deletes the cursor.

--
Erland Sommarskog, SQL Server MVP, esq...@sommarsk og.se

Books Online for SQL Server 2005 athttp://www.microsoft.c om/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.c om/sql/prodinfo/previousversion s/books.mspx


I would like to thank everyone for their response. I tried both
examples I was given and both worked great. Right now I am in testing
and I am only using a handful of records. Later on when I am
inserting hundreds or thousands of records I will try both ways again
to see which processes faster. Once again I really appreciate the
help.
May 3 '07 #4
eighthman11 (rd******@noote r.com) writes:
I would like to thank everyone for their response. I tried both
examples I was given and both worked great. Right now I am in testing
and I am only using a handful of records. Later on when I am
inserting hundreds or thousands of records I will try both ways again
to see which processes faster. Once again I really appreciate the
help.
Hundreds of thousands? I'm afraid that you will find both methods painfully
slow. With that size I would be prepared to look into do modify the trigger,
despite that it would void any warranties.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
May 3 '07 #5
On May 3, 9:19 am, Erland Sommarskog <esq...@sommars kog.sewrote:
eighthman11 (rdshu...@noote r.com) writes:
I would like to thank everyone for their response. I tried both
examples I was given and both worked great. Right now I am in testing
and I am only using a handful of records. Later on when I am
inserting hundreds or thousands of records I will try both ways again
to see which processes faster. Once again I really appreciate the
help.

Hundreds of thousands? I'm afraid that you will find both methods painfully
slow. With that size I would be prepared to look into do modify the trigger,
despite that it would void any warranties.

--
Erland Sommarskog, SQL Server MVP, esq...@sommarsk og.se

Books Online for SQL Server 2005 athttp://www.microsoft.c om/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.c om/sql/prodinfo/previousversion s/books.mspx
Sorry for the confustion. It isn't hundreds of thousands. It would
be hundreds OR thousands of records. In actuality I can't imagine
there ever being more than 3 thousand records.

May 3 '07 #6
eighthman11 (rd******@noote r.com) writes:
Sorry for the confustion. It isn't hundreds of thousands. It would
be hundreds OR thousands of records. In actuality I can't imagine
there ever being more than 3 thousand records.
OK, then you *may* be able to sustain the performance. But with 3000
rows to insert, you will need to have some patience.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
May 3 '07 #7

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

Similar topics

2
13399
by: george | last post by:
This is like the bug from hell. It is kind of hard to explain, so please bear with me. Background Info: SQL Server 7.0, on an NT box, Active Server pages with Javascript, using ADO objects. I'm inserting simple records into a table. But one insert command is placing 2 or 3 records into the table. The 'extra' records, have the same data as the previous insert incident, (except for the timestamp).
16
17020
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums must be UPDATED, if not, they must be INSERTED. Logically then, I would like to SELECT * FROM <TABLE> WHERE ....<Values entered here>, and then IF FOUND UPDATE <TABLE> SET .... <Values entered here> ELSE INSERT INTO <TABLE> VALUES <Values...
1
1837
by: ms | last post by:
I am running an insert statement from a dbf file and there is one record causing the insert to fail. A msg. is returned stating it is due to an invalid datatype. There are 2 text fields, 2 numeric(double), and one date/time. I assume it is one of the numeric or the date/time field. How can I determine the culprit record? There is one key field which is one of the text fields. I am trying to write a JOIN query but have not been...
8
9245
by: Bri | last post by:
Greetings, I'm having a very strange problem in an AC97 MDB with ODBC Linked tables to SQL Server 7. The table has an Identity field and a Timestamp field. The problem is that when a new record is entered, either from a form or from the table view of the table, when the record gets saved it immediately displays #DELETED# in all of the fields. However, if I close the form or table view and reopen the record has in fact been inserted. The...
8
6298
by: Carl | last post by:
Hi, I hope someone can share some of their professional advice and help me out with my embarissing problem concerning an Access INSERT query. I have never attempted to create a table with one-to-one relationship but on this occasion I must keep username/password details within a seperate table. Here's the basic specs and database schema: -------------------------------------------
6
2362
by: SandySears | last post by:
I am trying to use a stored procedure to insert a record using VS 2005, VB and SQL Server Express. The code runs without errors or exceptions, and returns the new identifer in the output parameters. It returns my error text message in another output parameter as "ok", which is the value that is set in the stored procedure prior to doing the insert. It returns my var for @@rowcount as 1. However, the record does not get into the table. ...
6
3471
by: rn5a | last post by:
During registration, users are supposed to enter the following details: First Name, Last Name, EMail, UserName, Password, Confirm Password, Address, City, State, Country, Zip & Phone Number. I am using MS-Access 2000 database table for this app. Note that the datatype of all the fields mentioned above are Text. Apart from the above columns, there's another column in the DB table named 'RegDateTime' whose datatype is Date/Time which is...
5
4594
by: djsdaddy | last post by:
Good Day All, I have some EEO data in an old dBase4 database that I have converted to an Access table. Since dBase was not a relational database, I didn't create any key fields. I linked all of the tables together on Employees' SSN; therefore, in order to use this data with the current Access database, I need to create a relationship between the old dBase4 EEO data and the Access employee data. The Access employee data was also an old dBase...
10
12717
by: MLH | last post by:
Suppose, in a multi-user environment, you have append query SQL in a VBA procedure that looks like INSERT INTO MyTable... and the next line reads MyVar=DMax("","MyTable... You can never be certain that MyVar will be set to the key-field value that was created when the Append query ran. Now, there are other ways to do it - I know - that will ensure you 'nab' the correct record. But I was wondering
0
2292
by: magnolia | last post by:
i created a trigger that will record the changes made to a table .everything works fine except the insert query.whenerever i try to insert a record it fires insert and update triger at the same time which means i hav 2 record for every insert operation. any help appreciated. thank u herez teh code i tried. ALTER TRIGGER trg_ContactAddress_Audit ON Address FOR DELETE, INSERT, UPDATE AS
0
9499
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10124
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8998
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4078
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.