SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Stored Procedure runs slower than SQL Query Why??


Stored Procedure runs slower than SQL Query Why??

Author
Message
david.ostrander
david.ostrander
SSC-Addicted
SSC-Addicted (410 reputation)SSC-Addicted (410 reputation)SSC-Addicted (410 reputation)SSC-Addicted (410 reputation)SSC-Addicted (410 reputation)SSC-Addicted (410 reputation)SSC-Addicted (410 reputation)SSC-Addicted (410 reputation)

Group: General Forum Members
Points: 410 Visits: 692
Hello -

I created a Store Procedure to have Table A sync with Table B by doing an insert of the data that it does not have.

The issue I'm running into is when I run the Stored Proc it's taking longer then just running the Insert itself. I want to put this into a Job to run at night but also to have it in a Transaction in case I need to rollback as well on an error. Can someone take a look to see if I have to much or not enough in my script?

USE [Dev_SageReporting]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[hgsp_backloglaborb_sync]

AS
DECLARE @intErrorCode INT

BEGIN TRAN

INSERT INTO BackLogLaborB
SELECT *
FROM BackLogLaborCurrent AS A
WHERE NOT EXISTS ( SELECT *
FROM BackLogLaborB AS B
WHERE A.PIT = B.PIT )

SET NOCOUNT ON;

SELECT @intErrorCode = @@ERROR
IF ( @intErrorCode <> 0 )
ROLLBACK TRANSACTION
ELSE
COMMIT TRANSACTION



Regards,
David
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12668 Visits: 5478
david.ostrander (3/13/2013)
Hello -

I created a Store Procedure to have Table A sync with Table B by doing an insert of the data that it does not have.

The issue I'm running into is when I run the Stored Proc it's taking longer then just running the Insert itself. I want to put this into a Job to run at night but also to have it in a Transaction in case I need to rollback as well on an error. Can someone take a look to see if I have to much or not enough in my script?

USE [Dev_SageReporting]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[hgsp_backloglaborb_sync]

AS
DECLARE @intErrorCode INT

BEGIN TRAN

INSERT INTO BackLogLaborB
SELECT *
FROM BackLogLaborCurrent AS A
WHERE NOT EXISTS ( SELECT *
FROM BackLogLaborB AS B
WHERE A.PIT = B.PIT )

SET NOCOUNT ON;

SELECT @intErrorCode = @@ERROR
IF ( @intErrorCode <> 0 )
ROLLBACK TRANSACTION
ELSE
COMMIT TRANSACTION



Regards,
David


For the start:
1. If you have single INSERT statement, you don't need to wrap it in TRANSACTION as it's atomic enough by itself.
2. The error you get is not from result of INSERT, but from SET NOCOUNT ON (which is unlikely to fail ;-));
3. I would suggest to learn using T-SQL TRY ... CATCH for error handling.
4. SET NOCOUNT ON; should be the first line in your stored procedure

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search