Wednesday, March 28, 2012
problem with large number of inserts
ODBC and sending a large number of individual insert statements. Apparently
this is causing problems with the transaction filling up on the sql database
the inserts are being sent to. To solve the problem, we set the transaction
log to be able to grow to 2GB. It quickly filled that space. Then we set it
to unlimited growth. Over time it grew to 12GB which began to cause other
problems. We backed up and ran DBCC Shrinkfile to bring it back under 1GB but
the remote app is not able to complete the inserts at that size. Truncate log
on checkpoint is selected in the database properties.
I found some KB articles which referred to problems doing large updates but
they had to do with using the 'where' statement and we are doing inserts with
no 'where' statement so it didn't seem to apply. Any ideas on how to solve
this problem?
Neither the Event Viewer nor the SQL Logs are offering much insight. No
errors are logged when the remote insert fails. However, the same database
appears to be failing to backup. During the weekly backup routine, we get
this in the Event Viewer:
"Could not clear 'DIFF' bitmap in database 'specialneeds' due to error 3617.
A subsequent backup operation may be slower/larger than normal."
We also get several variations of this message for both the Log and Database:
3041 :
BACKUP failed to complete the command BACKUP LOG [specialneeds] TO DISK =
N'E:\SQL Backup\specialneeds\specialneeds_db_200506190202.B AK', DISK =
N'E:\SQL Backup\specialneeds\specialneeds_Tlog.bak' WITH INIT , NOUNLOAD ,
NAME = N'specialneeds backup', NOSKIP , STATS = 10, NOFORMAT
We have 30 or so other databases, some larger some smaller, running on the
same SQL Server and none of them are having problems backing up. Thanks for
your help!
Hi
Run a DBCC CHECKDB on this database and see what it reports.
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"myaffee" wrote:
> We have an application that is connecting to our SQL 2k server remotely via
> ODBC and sending a large number of individual insert statements. Apparently
> this is causing problems with the transaction filling up on the sql database
> the inserts are being sent to. To solve the problem, we set the transaction
> log to be able to grow to 2GB. It quickly filled that space. Then we set it
> to unlimited growth. Over time it grew to 12GB which began to cause other
> problems. We backed up and ran DBCC Shrinkfile to bring it back under 1GB but
> the remote app is not able to complete the inserts at that size. Truncate log
> on checkpoint is selected in the database properties.
> I found some KB articles which referred to problems doing large updates but
> they had to do with using the 'where' statement and we are doing inserts with
> no 'where' statement so it didn't seem to apply. Any ideas on how to solve
> this problem?
> Neither the Event Viewer nor the SQL Logs are offering much insight. No
> errors are logged when the remote insert fails. However, the same database
> appears to be failing to backup. During the weekly backup routine, we get
> this in the Event Viewer:
> "Could not clear 'DIFF' bitmap in database 'specialneeds' due to error 3617.
> A subsequent backup operation may be slower/larger than normal."
> We also get several variations of this message for both the Log and Database:
> 3041 :
> BACKUP failed to complete the command BACKUP LOG [specialneeds] TO DISK =
> N'E:\SQL Backup\specialneeds\specialneeds_db_200506190202.B AK', DISK =
> N'E:\SQL Backup\specialneeds\specialneeds_Tlog.bak' WITH INIT , NOUNLOAD ,
> NAME = N'specialneeds backup', NOSKIP , STATS = 10, NOFORMAT
> We have 30 or so other databases, some larger some smaller, running on the
> same SQL Server and none of them are having problems backing up. Thanks for
> your help!
>
|||Mike,
Here's what it reported:
DBCC results for 'specialneeds'.
DBCC results for 'sysobjects'.
There are 245 rows in 6 pages for object 'sysobjects'.
DBCC results for 'sysindexes'.
There are 283 rows in 12 pages for object 'sysindexes'.
DBCC results for 'syscolumns'.
There are 1167 rows in 25 pages for object 'syscolumns'.
DBCC results for 'systypes'.
There are 26 rows in 1 pages for object 'systypes'.
DBCC results for 'syscomments'.
There are 220 rows in 19 pages for object 'syscomments'.
DBCC results for 'sysfiles1'.
There are 2 rows in 1 pages for object 'sysfiles1'.
DBCC results for 'syspermissions'.
There are 18 rows in 1 pages for object 'syspermissions'.
DBCC results for 'sysusers'.
There are 13 rows in 1 pages for object 'sysusers'.
DBCC results for 'sysproperties'.
There are 0 rows in 0 pages for object 'sysproperties'.
DBCC results for 'sysdepends'.
There are 571 rows in 4 pages for object 'sysdepends'.
DBCC results for 'sysreferences'.
There are 2 rows in 1 pages for object 'sysreferences'.
DBCC results for 'sysfulltextcatalogs'.
There are 0 rows in 1 pages for object 'sysfulltextcatalogs'.
DBCC results for 'sysfulltextnotify'.
There are 0 rows in 0 pages for object 'sysfulltextnotify'.
DBCC results for 'sysfilegroups'.
There are 1 rows in 1 pages for object 'sysfilegroups'.
DBCC results for 'leAuditCollectConfigVars'.
There are 3 rows in 1 pages for object 'leAuditCollectConfigVars'.
DBCC results for 'leAuditCollectDatabases'.
There are 59 rows in 1 pages for object 'leAuditCollectDatabases'.
DBCC results for 'leAuditCollectNotification'.
There are 1 rows in 1 pages for object 'leAuditCollectNotification'.
DBCC results for 'leAuditCollectAlerts'.
There are 5 rows in 1 pages for object 'leAuditCollectAlerts'.
DBCC results for 'affiliatelog'.
There are 0 rows in 1 pages for object 'affiliatelog'.
DBCC results for 'billing'.
There are 0 rows in 1 pages for object 'billing'.
DBCC results for 'configuration'.
There are 355 rows in 3 pages for object 'configuration'.
DBCC results for 'configuration1'.
There are 0 rows in 1 pages for object 'configuration1'.
DBCC results for 'coupons'.
There are 9 rows in 1 pages for object 'coupons'.
DBCC results for 'customerprices'.
There are 0 rows in 1 pages for object 'customerprices'.
DBCC results for 'customers'.
There are 1083 rows in 34 pages for object 'customers'.
DBCC results for 'downloads'.
There are 0 rows in 1 pages for object 'downloads'.
DBCC results for 'gifts'.
There are 0 rows in 1 pages for object 'gifts'.
DBCC results for 'hackers'.
There are 1 rows in 1 pages for object 'hackers'.
DBCC results for 'languages'.
There are 671 rows in 6 pages for object 'languages'.
DBCC results for 'mycompany'.
There are 0 rows in 1 pages for object 'mycompany'.
DBCC results for 'ordertracking'.
There are 0 rows in 1 pages for object 'ordertracking'.
DBCC results for 'pinnumbers'.
There are 0 rows in 1 pages for object 'pinnumbers'.
DBCC results for 'prodcategories'.
There are 2891 rows in 8 pages for object 'prodcategories'.
DBCC results for 'dtproperties'.
There are 0 rows in 1 pages for object 'dtproperties'.
DBCC results for 'prodfeatures'.
There are 0 rows in 1 pages for object 'prodfeatures'.
DBCC results for 'projects'.
There are 0 rows in 1 pages for object 'projects'.
DBCC results for 'quantitydiscounts'.
There are 0 rows in 1 pages for object 'quantitydiscounts'.
DBCC results for 'registrant'.
There are 0 rows in 1 pages for object 'registrant'.
DBCC results for 'registryitems'.
There are 0 rows in 1 pages for object 'registryitems'.
DBCC results for 'reviews'.
There are 0 rows in 1 pages for object 'reviews'.
DBCC results for 'savedcarts'.
There are 3 rows in 1 pages for object 'savedcarts'.
DBCC results for 'searchresults'.
There are 0 rows in 1 pages for object 'searchresults'.
DBCC results for 'shipmethods'.
There are 1 rows in 1 pages for object 'shipmethods'.
DBCC results for 'suppliers'.
There are 0 rows in 1 pages for object 'suppliers'.
DBCC results for 'tblaccess'.
There are 40 rows in 2 pages for object 'tblaccess'.
DBCC results for 'tbllog'.
There are 682 rows in 11 pages for object 'tbllog'.
DBCC results for 'orders'.
There are 997 rows in 71 pages for object 'orders'.
DBCC results for 'tbluser'.
There are 2 rows in 1 pages for object 'tbluser'.
DBCC results for 'templates'.
There are 0 rows in 1 pages for object 'templates'.
DBCC results for 'oitems'.
There are 2093 rows in 42 pages for object 'oitems'.
DBCC results for 'ipaddress'.
There are 8 rows in 1 pages for object 'ipaddress'.
DBCC results for 'privlabelproducts'.
There are 0 rows in 1 pages for object 'privlabelproducts'.
DBCC results for 'privatelabels'.
There are 1 rows in 1 pages for object 'privatelabels'.
DBCC results for 'privlabelproducts1'.
There are 32 rows in 3 pages for object 'privlabelproducts1'.
DBCC results for 'privatelabels1'.
There are 1 rows in 1 pages for object 'privatelabels1'.
DBCC results for 'privlabelproducts3'.
There are 0 rows in 1 pages for object 'privlabelproducts3'.
DBCC results for 'privatelabels3'.
There are 1 rows in 1 pages for object 'privatelabels3'.
DBCC results for 'categories'.
There are 39 rows in 1 pages for object 'categories'.
DBCC results for 'specialneeds.DIY_TEMPCOMMAND_TABLE'.
There are 0 rows in 0 pages for object 'specialneeds.DIY_TEMPCOMMAND_TABLE'.
DBCC results for 'spneeds_calendar'.
There are 10 rows in 1 pages for object 'spneeds_calendar'.
DBCC results for 'calendar'.
There are 13 rows in 1 pages for object 'calendar'.
DBCC results for 'spneeds_level'.
There are 12 rows in 1 pages for object 'spneeds_level'.
DBCC results for 'spneeds_author'.
There are 2892 rows in 18 pages for object 'spneeds_author'.
DBCC results for 'spneeds_subject'.
There are 39 rows in 1 pages for object 'spneeds_subject'.
DBCC results for 'spneeds_product'.
There are 2890 rows in 69 pages for object 'spneeds_product'.
DBCC results for 'disability_link_categories'.
There are 6 rows in 1 pages for object 'disability_link_categories'.
DBCC results for 'disability_link_images'.
There are 3 rows in 1 pages for object 'disability_link_images'.
DBCC results for 'disability_links'.
There are 16 rows in 1 pages for object 'disability_links'.
DBCC results for 'discount_codes'.
There are 1 rows in 1 pages for object 'discount_codes'.
DBCC results for 'spneeds_messages'.
There are 1 rows in 1 pages for object 'spneeds_messages'.
DBCC results for 'EmailContest'.
There are 434 rows in 8 pages for object 'EmailContest'.
DBCC results for 'products3'.
There are 3251 rows in 144 pages for object 'products3'.
DBCC results for 'products1'.
There are 3258 rows in 145 pages for object 'products1'.
DBCC results for 'products'.
There are 3258 rows in 151 pages for object 'products'.
DBCC results for 'affiliates'.
There are 1 rows in 1 pages for object 'affiliates'.
CHECKDB found 0 allocation errors and 0 consistency errors in database
'specialneeds'.
DBCC execution completed. If DBCC printed error messages, contact your
system administrator.
"Mike Epprecht (SQL MVP)" wrote:
[vbcol=seagreen]
> Hi
> Run a DBCC CHECKDB on this database and see what it reports.
> --
> --
> Mike Epprecht, Microsoft SQL Server MVP
> Zurich, Switzerland
> MVP Program: http://www.microsoft.com/mvp
> Blog: http://www.msmvps.com/epprecht/
>
> "myaffee" wrote:
problem with large number of inserts
ODBC and sending a large number of individual insert statements. Apparently
this is causing problems with the transaction filling up on the sql database
the inserts are being sent to. To solve the problem, we set the transaction
log to be able to grow to 2GB. It quickly filled that space. Then we set it
to unlimited growth. Over time it grew to 12GB which began to cause other
problems. We backed up and ran DBCC Shrinkfile to bring it back under 1GB bu
t
the remote app is not able to complete the inserts at that size. Truncate lo
g
on checkpoint is selected in the database properties.
I found some KB articles which referred to problems doing large updates but
they had to do with using the 'where' statement and we are doing inserts wit
h
no 'where' statement so it didn't seem to apply. Any ideas on how to solve
this problem?
Neither the Event Viewer nor the SQL Logs are offering much insight. No
errors are logged when the remote insert fails. However, the same database
appears to be failing to backup. During the weekly backup routine, we get
this in the Event Viewer:
"Could not clear 'DIFF' bitmap in database 'specialneeds' due to error 3617.
A subsequent backup operation may be slower/larger than normal."
We also get several variations of this message for both the Log and Database
:
3041 :
BACKUP failed to complete the command BACKUP LOG [specialneeds] TO DISK
=
N'E:\SQL Backup\specialneeds\specialneeds_db_2005
06190202.BAK', DISK =
N'E:\SQL Backup\specialneeds\specialneeds_Tlog.bak' WITH INIT , NOUNLOAD ,
NAME = N'specialneeds backup', NOSKIP , STATS = 10, NOFORMAT
We have 30 or so other databases, some larger some smaller, running on the
same SQL Server and none of them are having problems backing up. Thanks for
your help!Hi
Run a DBCC CHECKDB on this database and see what it reports.
--
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"myaffee" wrote:
> We have an application that is connecting to our SQL 2k server remotely vi
a
> ODBC and sending a large number of individual insert statements. Apparentl
y
> this is causing problems with the transaction filling up on the sql databa
se
> the inserts are being sent to. To solve the problem, we set the transactio
n
> log to be able to grow to 2GB. It quickly filled that space. Then we set i
t
> to unlimited growth. Over time it grew to 12GB which began to cause other
> problems. We backed up and ran DBCC Shrinkfile to bring it back under 1GB
but
> the remote app is not able to complete the inserts at that size. Truncate
log
> on checkpoint is selected in the database properties.
> I found some KB articles which referred to problems doing large updates bu
t
> they had to do with using the 'where' statement and we are doing inserts w
ith
> no 'where' statement so it didn't seem to apply. Any ideas on how to solve
> this problem?
> Neither the Event Viewer nor the SQL Logs are offering much insight. No
> errors are logged when the remote insert fails. However, the same database
> appears to be failing to backup. During the weekly backup routine, we get
> this in the Event Viewer:
> "Could not clear 'DIFF' bitmap in database 'specialneeds' due to error 361
7.
> A subsequent backup operation may be slower/larger than normal."
> We also get several variations of this message for both the Log and Databa
se:
> 3041 :
> BACKUP failed to complete the command BACKUP LOG [specialneeds] TO DI
SK =
> N'E:\SQL Backup\specialneeds\specialneeds_db_2005
06190202.BAK', DISK =
> N'E:\SQL Backup\specialneeds\specialneeds_Tlog.bak' WITH INIT , NOUNLOAD
,
> NAME = N'specialneeds backup', NOSKIP , STATS = 10, NOFORMAT
> We have 30 or so other databases, some larger some smaller, running on the
> same SQL Server and none of them are having problems backing up. Thanks fo
r
> your help!
>|||Mike,
Here's what it reported:
DBCC results for 'specialneeds'.
DBCC results for 'sysobjects'.
There are 245 rows in 6 pages for object 'sysobjects'.
DBCC results for 'sysindexes'.
There are 283 rows in 12 pages for object 'sysindexes'.
DBCC results for 'syscolumns'.
There are 1167 rows in 25 pages for object 'syscolumns'.
DBCC results for 'systypes'.
There are 26 rows in 1 pages for object 'systypes'.
DBCC results for 'syscomments'.
There are 220 rows in 19 pages for object 'syscomments'.
DBCC results for 'sysfiles1'.
There are 2 rows in 1 pages for object 'sysfiles1'.
DBCC results for 'syspermissions'.
There are 18 rows in 1 pages for object 'syspermissions'.
DBCC results for 'sysusers'.
There are 13 rows in 1 pages for object 'sysusers'.
DBCC results for 'sysproperties'.
There are 0 rows in 0 pages for object 'sysproperties'.
DBCC results for 'sysdepends'.
There are 571 rows in 4 pages for object 'sysdepends'.
DBCC results for 'sysreferences'.
There are 2 rows in 1 pages for object 'sysreferences'.
DBCC results for 'sysfulltextcatalogs'.
There are 0 rows in 1 pages for object 'sysfulltextcatalogs'.
DBCC results for 'sysfulltextnotify'.
There are 0 rows in 0 pages for object 'sysfulltextnotify'.
DBCC results for 'sysfilegroups'.
There are 1 rows in 1 pages for object 'sysfilegroups'.
DBCC results for 'leAuditCollectConfigVars'.
There are 3 rows in 1 pages for object 'leAuditCollectConfigVars'.
DBCC results for 'leAuditCollectDatabases'.
There are 59 rows in 1 pages for object 'leAuditCollectDatabases'.
DBCC results for 'leAuditCollectNotification'.
There are 1 rows in 1 pages for object 'leAuditCollectNotification'.
DBCC results for 'leAuditCollectAlerts'.
There are 5 rows in 1 pages for object 'leAuditCollectAlerts'.
DBCC results for 'affiliatelog'.
There are 0 rows in 1 pages for object 'affiliatelog'.
DBCC results for 'billing'.
There are 0 rows in 1 pages for object 'billing'.
DBCC results for 'configuration'.
There are 355 rows in 3 pages for object 'configuration'.
DBCC results for 'configuration1'.
There are 0 rows in 1 pages for object 'configuration1'.
DBCC results for 'coupons'.
There are 9 rows in 1 pages for object 'coupons'.
DBCC results for 'customerprices'.
There are 0 rows in 1 pages for object 'customerprices'.
DBCC results for 'customers'.
There are 1083 rows in 34 pages for object 'customers'.
DBCC results for 'downloads'.
There are 0 rows in 1 pages for object 'downloads'.
DBCC results for 'gifts'.
There are 0 rows in 1 pages for object 'gifts'.
DBCC results for 'hackers'.
There are 1 rows in 1 pages for object 'hackers'.
DBCC results for 'languages'.
There are 671 rows in 6 pages for object 'languages'.
DBCC results for 'mycompany'.
There are 0 rows in 1 pages for object 'mycompany'.
DBCC results for 'ordertracking'.
There are 0 rows in 1 pages for object 'ordertracking'.
DBCC results for 'pinnumbers'.
There are 0 rows in 1 pages for object 'pinnumbers'.
DBCC results for 'prodcategories'.
There are 2891 rows in 8 pages for object 'prodcategories'.
DBCC results for 'dtproperties'.
There are 0 rows in 1 pages for object 'dtproperties'.
DBCC results for 'prodfeatures'.
There are 0 rows in 1 pages for object 'prodfeatures'.
DBCC results for 'projects'.
There are 0 rows in 1 pages for object 'projects'.
DBCC results for 'quantitydiscounts'.
There are 0 rows in 1 pages for object 'quantitydiscounts'.
DBCC results for 'registrant'.
There are 0 rows in 1 pages for object 'registrant'.
DBCC results for 'registryitems'.
There are 0 rows in 1 pages for object 'registryitems'.
DBCC results for 'reviews'.
There are 0 rows in 1 pages for object 'reviews'.
DBCC results for 'savedcarts'.
There are 3 rows in 1 pages for object 'savedcarts'.
DBCC results for 'searchresults'.
There are 0 rows in 1 pages for object 'searchresults'.
DBCC results for 'shipmethods'.
There are 1 rows in 1 pages for object 'shipmethods'.
DBCC results for 'suppliers'.
There are 0 rows in 1 pages for object 'suppliers'.
DBCC results for 'tblaccess'.
There are 40 rows in 2 pages for object 'tblaccess'.
DBCC results for 'tbllog'.
There are 682 rows in 11 pages for object 'tbllog'.
DBCC results for 'orders'.
There are 997 rows in 71 pages for object 'orders'.
DBCC results for 'tbluser'.
There are 2 rows in 1 pages for object 'tbluser'.
DBCC results for 'templates'.
There are 0 rows in 1 pages for object 'templates'.
DBCC results for 'oitems'.
There are 2093 rows in 42 pages for object 'oitems'.
DBCC results for 'ipaddress'.
There are 8 rows in 1 pages for object 'ipaddress'.
DBCC results for 'privlabelproducts'.
There are 0 rows in 1 pages for object 'privlabelproducts'.
DBCC results for 'privatelabels'.
There are 1 rows in 1 pages for object 'privatelabels'.
DBCC results for 'privlabelproducts1'.
There are 32 rows in 3 pages for object 'privlabelproducts1'.
DBCC results for 'privatelabels1'.
There are 1 rows in 1 pages for object 'privatelabels1'.
DBCC results for 'privlabelproducts3'.
There are 0 rows in 1 pages for object 'privlabelproducts3'.
DBCC results for 'privatelabels3'.
There are 1 rows in 1 pages for object 'privatelabels3'.
DBCC results for 'categories'.
There are 39 rows in 1 pages for object 'categories'.
DBCC results for 'specialneeds.DIY_TEMPCOMMAND_TABLE'.
There are 0 rows in 0 pages for object 'specialneeds.DIY_TEMPCOMMAND_TABLE'.
DBCC results for 'spneeds_calendar'.
There are 10 rows in 1 pages for object 'spneeds_calendar'.
DBCC results for 'calendar'.
There are 13 rows in 1 pages for object 'calendar'.
DBCC results for 'spneeds_level'.
There are 12 rows in 1 pages for object 'spneeds_level'.
DBCC results for 'spneeds_author'.
There are 2892 rows in 18 pages for object 'spneeds_author'.
DBCC results for 'spneeds_subject'.
There are 39 rows in 1 pages for object 'spneeds_subject'.
DBCC results for 'spneeds_product'.
There are 2890 rows in 69 pages for object 'spneeds_product'.
DBCC results for 'disability_link_categories'.
There are 6 rows in 1 pages for object 'disability_link_categories'.
DBCC results for 'disability_link_images'.
There are 3 rows in 1 pages for object 'disability_link_images'.
DBCC results for 'disability_links'.
There are 16 rows in 1 pages for object 'disability_links'.
DBCC results for 'discount_codes'.
There are 1 rows in 1 pages for object 'discount_codes'.
DBCC results for 'spneeds_messages'.
There are 1 rows in 1 pages for object 'spneeds_messages'.
DBCC results for 'EmailContest'.
There are 434 rows in 8 pages for object 'EmailContest'.
DBCC results for 'products3'.
There are 3251 rows in 144 pages for object 'products3'.
DBCC results for 'products1'.
There are 3258 rows in 145 pages for object 'products1'.
DBCC results for 'products'.
There are 3258 rows in 151 pages for object 'products'.
DBCC results for 'affiliates'.
There are 1 rows in 1 pages for object 'affiliates'.
CHECKDB found 0 allocation errors and 0 consistency errors in database
'specialneeds'.
DBCC execution completed. If DBCC printed error messages, contact your
system administrator.
"Mike Epprecht (SQL MVP)" wrote:
[vbcol=seagreen]
> Hi
> Run a DBCC CHECKDB on this database and see what it reports.
> --
> --
> Mike Epprecht, Microsoft SQL Server MVP
> Zurich, Switzerland
> MVP Program: http://www.microsoft.com/mvp
> Blog: http://www.msmvps.com/epprecht/
>
> "myaffee" wrote:
>
Monday, March 26, 2012
Problem with Insert trigger
fired via a dts process that runs every 10 minutes which inserts data into a
ForecastTonnageChanges table. Based on changes to this
ForecastTonnageChanges, I want to update the live Forecast table. The
trigger fires this event. My basic logic in the trigger is:
Delete from Forecast
where exists (select *
from inserted
where <joining key columns> )
Insert into Forecast
select ...
from inserted
When this doesn't work, I am ending up with extra records in my forecast
table. I'm wondering if another insert has happened and between the delete
and the insert in the trigger, an additional row is in the inserted table.
Is this possible? I would think implied locking via the trigger would
prevent this. But, I'm grasping for straws here.
If you have ideas on a more reliable way to implement this... please share.Hi
A trigger is fired for each statement, therefore you will not get extra rows
in the inserted/deleted tables.
As you don't give DDL and all the trigger code it is hard to say where you
are going wrong, but you may want to use profiler to see what is happening.
You should also implement error handling to make sure that the statement
succeeds and all the relivent statements in the transaction are rolled back
if a failure occurs.
John
"Erin" wrote:
> I have an insert trigger that works 99.999% of the time. The trigger is
> fired via a dts process that runs every 10 minutes which inserts data into
a
> ForecastTonnageChanges table. Based on changes to this
> ForecastTonnageChanges, I want to update the live Forecast table. The
> trigger fires this event. My basic logic in the trigger is:
> Delete from Forecast
> where exists (select *
> from inserted
> where <joining key columns> )
> Insert into Forecast
> select ...
> from inserted
> When this doesn't work, I am ending up with extra records in my forecast
> table. I'm wondering if another insert has happened and between the delet
e
> and the insert in the trigger, an additional row is in the inserted table.
> Is this possible? I would think implied locking via the trigger would
> prevent this. But, I'm grasping for straws here.
> If you have ideas on a more reliable way to implement this... please share.[/color
]|||Without any more information, could you have a forecast VIEW of the
most current rows in the ForecastTonnageChanges table instead of
physically shuffling all this data around? You obviously have a time
stamp on the new data, so that should be easy enough.
Monday, March 12, 2012
Problem with FETCH LAST
I have a SQL stored procedure that inserts a record into a table,
creates a cursor to fetch the last record that was added to get the
unique key that was created and then writes that and other info to a
separate table. This procedure was working fine at our ISP under NT 4
and SQL 7.
We recently moved to another ISP on servers that are windows 2000 and
SQL 2000. Now this code is going kerplooey. It actually worked fine
in the staging area but now that it was moved into production, it is
not working. also wanted to mention that the production database was
restored from a backup. below is the code.
the first time this is run it is ok, for example the transaction
number is 1. the next time it is run, a new record is created in the
sweep results with a transaction number of 2. but for some reason,
when i declare the cursor to fetch the last record, it goes back to
the transaction number 1 record. so the counts from transaction 1
don't match counts from transaction 2 and the next step has an error
condition and doesn't work.
thanks in advance for any help you can provide
Ann Williams
-- update the sweep results table
INSERT tbl_sweepresults (del_wrkfeedback_count,
updnull_feedback_count, swp_feedback_count,
swp_count_error, del_error, updnull_error, swp_error, init_error,
sweep_date)
VALUES (@.var_del_wrkfeedback_count, @.var_updnull_feedback_count,
@.var_swp_feedback_count,
@.var_swp_count_error, @.var_del_error, @.var_updnull_error,
@.var_swp_error, @.var_init_error, GETDATE())
-- create cursor
DECLARE tbl_sweepresults_cursor SCROLL CURSOR FOR
SELECT transaction_no, sweep_date, init_error, updnull_feedback_count
FROM tbl_sweepresults
OPEN tbl_sweepresults_cursor
-- get transaction number, sweep date, init error, feedback sweep
count and pass to tbl_currentTrans for OPAL comparison
FETCH LAST FROM tbl_sweepresults_cursor INTO @.var_transaction_no,
@.var_sweep_date, @.var_init_error, @.var_swp_countzero
DELETE tbl_currentTrans
INSERT tbl_currentTrans (current_transaction_no, current_sweep_date,
current_init_error, current_swp_countzero)
VALUES (@.var_transaction_no, @.var_sweep_date, @.var_init_error,
@.var_swp_countzero)
-- close the cursor
CLOSE tbl_sweepresults_cursor
DEALLOCATE tbl_sweepresults_cursorIs the value you are trying to retrieve an IDENTITY column? If so,
SCOPE_IDENTITY() is what you need. It returns the last inserted identity
value.
Since your cursor declaration doesn't include an ORDER BY clause you've been
lucky that it ever gave a meaningful result. FETCH LAST will just return an
indeterminate row from the table. Moving to another system (perhaps one with
more read-ahead cacheing) has shown up this defficiency which relied on the
engine always returning the last-inserted row.
Q. Why insert the row you've just added into another table
(tbl_currentTrans)? After all you already have it in a table and you know
the primary key.
--
David Portas
----
Please reply only to the newsgroup
--