dbTalk Databases Forums  

INSERT - one record at a time

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss INSERT - one record at a time in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
eighthman11
 
Posts: n/a

Default INSERT - one record at a time - 05-02-2007 , 03:09 PM






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.


Reply With Quote
  #2  
Old   
Utahduck
 
Posts: n/a

Default Re: INSERT - one record at a time - 05-02-2007 , 03:21 PM






On May 2, 2:09 pm, eighthman11 <rdshu... (AT) nooter (DOT) com> wrote:
Quote:
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




Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: INSERT - one record at a time - 05-02-2007 , 04:36 PM



eighthman11 (rdshultz (AT) nooter (DOT) com) writes:
Quote:
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.

Quote:
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, esquel (AT) sommarskog (DOT) 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


Reply With Quote
  #4  
Old   
eighthman11
 
Posts: n/a

Default Re: INSERT - one record at a time - 05-03-2007 , 09:05 AM



On May 2, 4:36 pm, Erland Sommarskog <esq... (AT) sommarskog (DOT) se> wrote:
Quote:
eighthman11 (rdshu... (AT) nooter (DOT) 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

INSERTtargettable (...)
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... (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/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.




Reply With Quote
  #5  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: INSERT - one record at a time - 05-03-2007 , 09:19 AM



eighthman11 (rdshultz (AT) nooter (DOT) com) writes:
Quote:
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, esquel (AT) sommarskog (DOT) 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


Reply With Quote
  #6  
Old   
eighthman11
 
Posts: n/a

Default Re: INSERT - one record at a time - 05-03-2007 , 11:12 AM



On May 3, 9:19 am, Erland Sommarskog <esq... (AT) sommarskog (DOT) se> wrote:
Quote:
eighthman11 (rdshu... (AT) nooter (DOT) 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... (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/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.





Reply With Quote
  #7  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: INSERT - one record at a time - 05-03-2007 , 05:06 PM



eighthman11 (rdshultz (AT) nooter (DOT) com) writes:
Quote:
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, esquel (AT) sommarskog (DOT) 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


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.