Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SP takes longer than batch version
Message
 
To
27/01/2006 10:44:01
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Environment versions
SQL Server:
SQL Server 2000
Miscellaneous
Thread ID:
01090888
Message ID:
01090989
Views:
15
This is the batch (so you can see how the params are coming in too.) The SP has the first 6 declares changed to be params and the Sets of those variables are not there. (They are params after all) Otherwise the two are identical.
DECLARE 	@FiscalYear char(4)
declare	@Accounts varchar(600)
declare	@SWDNs varchar(1100)
declare	@PrimaryDocs varchar(700)
declare	@GLCs varchar(50)
declare 	@Sites varchar(200)

SET @FiscalYear = '2006'
SET @Accounts = '60101000326609107020004000000X60202261015609107020004000000X'
set @SWDNs ='H6000026419H6000026420H6000026421H6000026422H6000026424H6000026425H6000026426H6000026427H6000026428H6000026429H6000026430H6000026431H6000026432H6000026434H6000026436H6000026437H6000026438H6000026441H6000026442'
set @PrimaryDocs ='V000084V000085V000088V000089V000090V000091V000092V000093V000094V000095V000096V000097V000099V000100V000101V000102V000103V000104V000105V000106V000107V000109V000110V000111V000112V000113V000114V000115V000116V000117V000119V000121V000122V000123V000124V000126V000127VCJ6277'
SET @glcs = '71100'
SET @Sites = 'AC30002322'


-- Declare Table Variables
DECLARE @AccountTable TABLE (FY CHAR(4) , Account CHAR(29), CFI CHAR(1)) 
DECLARE @SWDNTable TABLE (SWDN CHAR(11))
DECLARE @PDocTable TABLE (PrimaryDoc CHAR(7))
DECLARE @GLTable TABLE (GLC CHAR(5))
DECLARE @SiteTable TABLE (Site char(2))

-- Loop Controls
DECLARE @lnLoop INT
DECLARE @loopcount INT

-- Create the Account Table
SET @lnLoop = (LEN(@Accounts) / 30)	-- Accounts are 30 characters long (Account + CFI (X if no CFI))
SET @LoopCount = 1

WHILE @LoopCount <= @lnLoop
BEGIN
	INSERT INTO @AccountTable(FY, Account, CFI) 
	VALUES (@FiscalYear, 
		LEFT(SUBSTRING(@Accounts, ((@LoopCount - 1) * 30) + 1, 30), 29),
		RIGHT(SUBSTRING(@Accounts, ((@Loopcount - 1) * 30) + 1, 30), 1))

	SET @LoopCount = @LoopCount + 1
END

-- Fix the X's back to ' '
UPDATE @AccountTable
SET CFI = ' '
WHERE CFI = 'X'


-- Create the SWDN table
SET @lnLoop = (LEN(@SWDNs) / 11)	-- Accounts are 11 characters long
SET @LoopCount = 1

WHILE @LoopCount <= @lnLoop
BEGIN
	INSERT INTO @swdNTable (SWDN) 
	VALUES (SUBSTRING(@SWDNs, ((@LoopCount - 1) * 11) + 1, 11))

	SET @LoopCount = @LoopCount + 1
END

-- Create the PrimaryDocs table
SET @lnLoop = (LEN(@PrimaryDocs) / 7)	-- Primary Documents are 7 chars long in Recon but 11 in FLAIRData!FLAIRTR2
SET @LoopCount = 1

WHILE @LoopCount <= @lnLoop
BEGIN
	INSERT INTO @PDocTable (PrimaryDoc) 
	VALUES (SUBSTRING(@PrimaryDocs, ((@LoopCount - 1) * 7) + 1, 7))

	SET @LoopCount = @LoopCount + 1
END

-- Create the GL code table
SET @lnLoop = (LEN(@GLCs) / 5)	-- GL codes are 5 chars long
SET @LoopCount = 1

-- This is inserted in case no GL value was specified in the transaction.
INSERT INTO  @GLTable (GLC) 
VALUES ('')

WHILE @LoopCount <= @lnLoop
BEGIN
	INSERT INTO  @GLTable (GLC) 
	VALUES (SUBSTRING(@GLCs, ((@LoopCount - 1) * 5) + 1, 5))

	SET @LoopCount = @LoopCount + 1
END

-- Create the Site code table
SET @lnLoop = (LEN(@Sites) / 2)	-- Site codes are 2 chars long
SET @LoopCount = 1

-- This is inserted in case no Site value was specified in the transaction.
INSERT INTO  @SiteTable (Site) 
VALUES ('')

WHILE @LoopCount <= @lnLoop
BEGIN
	INSERT INTO  @SiteTable (Site) 
	VALUES (SUBSTRING(@Sites, ((@LoopCount - 1) * 2) + 1, 2))

	SET @LoopCount = @LoopCount + 1
END

SELECT FY, Account, CFI, SWDN, PrimaryDoc, GLC, Site
INTO #Temp
FROM @AccountTable, @SWDNTable, @PDocTable, @SiteTable, @GLTable

-- Uses joins for speed boost and use of indices.
SELECT  FLAIRTR2.* 
FROM FLAIRData.dbo.FlairTR2 FLAIRTR2
INNER JOIN #Temp TMP
	ON FLAIRTR2.FiscalYear = TMP.FY 
		AND FLAIRTR2.Account = TMP.Account 
		AND FLAIRTR2.CFI = TMP.CFI
		AND FLAIRTR2.SWDN = TMP.SWDN
		AND FLAIRTR2.PrimaryDoc7 = TMP.PrimaryDoc
		AND FLAIRTR2.GLC = TMP.GLC
		AND FLAIRTR2.Site = TMP.Site
ORDER BY UniqueID


SELECT  CATR1.* 
FROM FLAIRData.dbo.CATR1 CATR1
INNER JOIN #Temp TMP
	ON CATR1.FiscalYear = TMP.FY 
		AND CATR1.Account = TMP.Account 
		AND CATR1.CFI = TMP.CFI
		AND CATR1.SWDN11 = TMP.SWDN
		AND CATR1.AgencyDocNumber = TMP.PrimaryDoc
		AND CATR1.Site = TMP.Site
Execution plans: How do I go about getting a text version of a plan?
State of Florida, DCF
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform