Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Stored Procedure runs slower than SQL Query Why??
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ostinoh
Yak Posting Veteran

66 Posts

Posted - 03/13/2013 :  11:14:29  Show Profile  Reply with Quote
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

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

Posted - 03/13/2013 :  11:17:41  Show Profile  Reply with Quote
might be due to a bad plan in cache. try flushing out the procedure cache and then executing it

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000