Stored Procedure runs fast after recompile - stored-procedures
I have a very weird issue with a stored procedure on SQL Server 2008 R2. Sometimes, about once every month, I have one procedure that becomes very slow, takes about 6sec to run instead of a few milliseconds. But if I simply recompile it, without changing anything, it runs fast again.
It does not happen on all stored procedure, only one (there are a few hundreds on the server).
My guess is when the sp is compiled, it is cached and this cache is reused every time I call it, and this cached version gets corrupted for some reason.
I hoped maybe some people already faced this kind of issue, or could at least point me in the right direction, like what configuration of SQL Server or IIS could affect the stored procedure cache ?
Here's the code:
USE [MyBaseName]
GO
/****** Object: StoredProcedure [dbo].[Publication_getByCriteria] Script Date: 05/29/2013 12:11:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Publication_getByCriteria]
#id_sousTheme As int = null,
#id_theme As int = null,
#nbPubli As int = 1000000,
#bActuSite As bit = null,
#bActuPerso As bit = null,
#bActuNewsletter As bit = null,
#bActuChronique As bit = null,
#bActuVideo As bit = null,
#bActuVideoBuzz As bit = null,
#bActuOpportunite As bit = null,
#id_contact As int = null,
#bOnlyPublished As bit = 0,
#bOnlyForHomePage as bit = 0,
#id_contactForTheme As int = null,
#id_newsletter As int = null,
#ID_ActuChronique As int = null,
#sMotClef As varchar(500) = null,
#sMotClefForFullText as varchar(500) = '""',
#dtPublication As datetime = null,
#bParlonsFinance As bit = null,
#bPartenaires as bit = null,
#bUne As bit = null,
#bEditoParlonsFinance As bit = null,
#bEditoQuestionFonds as bit = null,
#dtDebPublication As datetime = null,
#dtFinPublication As datetime = null,
#bOnlyActuWithDroitReponse As bit = 0,
#bActuDroitReponse As bit = null
AS
BEGIN
SET NOCOUNT ON;
DECLARE #dtNow As datetime
SET #dtNow = GETDATE()
SELECT TOP (#nbPubli) p.id_publication, p.sTitre, p.sTexte, p.sTexteHTML, p.dtPublication, p.id_linkedDroitReponse,
si.id_actusite, pe.id_actuPerso, ne.id_actuNewsletter, ac.id_actuChronique, av.id_actuVideo, ap.id_actuOpportunite, ad.id_actuDroitReponse,
c.ID_Contact, c.sPhotoCarre, NULL As sTypePubli, n.id_newsletter,
dbo.Publication_get1Theme(p.id_publication) As theme,
CAST(CASE WHEN ad.id_actuDroitReponse IS NULL THEN 0 ELSE 1 END As bit) As bIsDroitReponse,
coalesce(Personne.sNom, Societe.sNom) as sNom, Personne.sPrenom
FROM Publication p
LEFT OUTER JOIN ActuSite si ON p.id_publication = si.id_publication
LEFT OUTER JOIN ActuPerso pe ON p.id_publication = pe.id_publication
LEFT OUTER JOIN ActuNewsletter ne ON p.id_publication = ne.id_publication
LEFT OUTER JOIN ActuChronique ac ON p.id_publication = ac.id_publication
LEFT OUTER JOIN ActuVideo av ON p.id_publication = av.id_publication
LEFT OUTER JOIN ActuOpportunite ap ON p.id_publication = ap.id_publication
LEFT OUTER JOIN ActuDroitReponse ad ON p.id_publication = ad.id_publication
LEFT OUTER JOIN Contact c ON p.id_contact = c.ID_Contact
LEFT OUTER JOIN Personne ON Personne.id_contact = c.id_contact
LEFT OUTER JOIN Societe ON Societe.id_contact = c.id_contact
LEFT OUTER JOIN Newsletter n ON ne.id_actuNewsletter = n.id_actuNewsletter
WHERE p.bSupp = 0
AND (#bOnlyPublished = 0 Or (#bOnlyPublished = 1 AND p.dtPublication IS NOT NULL AND p.dtPublication < #dtNow))
AND (#id_sousTheme IS NULL Or p.id_publication IN(SELECT id_publication FROM PubliSousTheme WHERE id_soustheme = #id_sousTheme))
AND (#id_theme IS NULL Or p.id_publication IN(SELECT id_publication FROM PubliTheme WHERE id_theme = #id_theme))
AND ((#bActuSite = 1 AND si.id_actusite IS NOT NULL)
OR (#bActuPerso = 1 AND pe.id_actuPerso IS NOT NULL)
OR (#bActuNewsletter = 1 AND ne.id_actuNewsletter IS NOT NULL)
OR (#bActuChronique = 1 AND ac.id_actuChronique IS NOT NULL)
OR (#bActuVideo = 1 AND av.id_actuVideo IS NOT NULL)
OR (#bActuVideoBuzz = 1 AND av.id_actuVideo IS NOT NULL and coalesce(av.sBuzz, '') <> '' )
OR (#bActuOpportunite = 1 AND ap.id_actuOpportunite IS NOT NULL)
OR (#bActuDroitReponse = 1 AND ad.id_actuDroitReponse IS NOT NULL))
AND (#id_contact IS NULL Or p.id_contact = #id_contact)
AND (#id_contactForTheme IS NULL Or
(p.id_publication IN(SELECT id_publication FROM PubliSousTheme
WHERE id_soustheme IN(SELECT id_soustheme FROM ContactSousTheme WHERE id_contact = #id_contactForTheme)))
Or (p.id_publication IN(SELECT id_publication FROM PubliTheme
WHERE id_theme IN(SELECT id_theme FROM ContactTheme WHERE id_contact = #id_contactForTheme)))
)
AND (#ID_ActuChronique is NULL or id_actuChronique = #ID_ActuChronique)
AND (#id_newsletter IS NULL Or p.id_publication IN(SELECT id_publication FROM ListActuNewsletter WHERE id_newsletter = #id_newsletter))
AND (#sMotClef IS NULL
or contains((p.sTexte, p.sTitre), #sMotClefForFullText)
Or Personne.sNom LIKE '%' + #sMotClef + '%' COLLATE Latin1_General_CI_AI
Or Personne.sPrenom LIKE '%' + #sMotClef + '%' COLLATE Latin1_General_CI_AI
Or Societe.sNom LIKE '%' + #sMotClef + '%' COLLATE Latin1_General_CI_AI
)
AND (#dtPublication IS NULL Or p.dtPublication >= #dtPublication)
AND (
#bParlonsFinance IS NULL Or
(#bParlonsFinance = 0 AND p.id_publication NOT IN(SELECT id_publication FROM PubliTheme
WHERE id_theme IN(SELECT id_theme FROM Theme WHERE bParlonsFinance = 1)))
Or (#bParlonsFinance = 1 AND p.id_publication IN(SELECT id_publication FROM PubliTheme
WHERE id_theme IN(SELECT id_theme FROM Theme WHERE bParlonsFinance = 1))))
AND (
#bPartenaires IS NULL Or
(#bPartenaires = 0 AND p.id_publication NOT IN(SELECT id_publication FROM PubliTheme
WHERE id_theme IN(SELECT id_theme FROM Theme WHERE 0 = 1)))
Or (#bPartenaires = 1 AND p.id_publication IN(SELECT id_publication FROM PubliTheme
WHERE id_theme IN(SELECT id_theme FROM Theme WHERE 0 = 1))))
AND (
#bUne IS NULL
Or p.bUne = #bUne)
AND (#bEditoParlonsFinance IS NULL
Or p.bEditoParlonsFinance = #bEditoParlonsFinance)
AND (#bEditoQuestionFonds IS NULL
Or p.bEditoQuestionFonds = #bEditoQuestionFonds)
AND (#dtDebPublication IS NULL Or p.dtPublication >= #dtDebPublication)
AND (#dtFinPublication IS NULL Or p.dtPublication <= #dtFinPublication)
AND (#bOnlyActuWithDroitReponse = 0 Or (#bOnlyActuWithDroitReponse = 1 AND p.id_linkedDroitReponse IS NOT NULL))
and (#bOnlyForHomePage = 0 or (#bOnlyForHomePage = 1 and ac.bHomePage = 1))
ORDER BY coalesce(p.dtPublication, p.dtCreate) DESC, p.id_publication DESC
END
When you first compile a stored procedure, its execution plan gets cached.
If the sproc has parameters whose definitions can significantly change the contained query's execution plan (e.g. index scans vs seeks), the stored procedure's cached plan may not work best for all parameter definitions.
One way to avoid this is to include a RECOMPILE clause with the CREATE PROCEDURE statement.
Example:
CREATE PROCEDURE dbo.mySpro
#myParam
WITH RECOMPILE
AS
BEGIN
-- INSERT WORKLOAD HERE
END
GO
By doing this, a new plan will be generated each time the procedure is called. If recompile time < time lost by its using the wrong cached plan, this is worth using WITH RECOMPILE. In your case, it will also save you the time/planning required to manually recompile this procedure every time you notice it is executing slowly.
For a stored procedure with that many parameters, it would be wise to add OPTION(OPTIMIZE FOR UNKNOWN) at the end of the query to tell the compiler not to optimize the execution plan for specific parameters.
What SQL Server does the first time it runs a stored procedure is optimize the execution plan(s) for the parameters that were passed to it. This is done in a process that is called Parameter Sniffing.
In general, execution plans are cached by SQL Server so that SQL Server doesn't have to recompile each time for the same query. The next time the procedure is run, SQL Server will re-use the execution plan(s) for the queries in it... However, the execution plan(s) might be totally inefficient if you call the stored procedure with different parameters.
Adding the option I mentioned will tell to the SQL compiler that the execution plan should not be optimized for specific parameters, but rather for any parameter that is passed to the stored procedure. From the documentation:
OPTIMIZE FOR UNKNOWN
Instructs the query optimizer to use statistical data instead of the initial values for all local variables when the query is compiled and optimized, including parameters created with forced parameterization.
The answer by #sion_corn recommends adding WITH RECOMPILE to the stored procedure definition, however this forces a recompile of the whole statement each time the stored procedure is executed. This might incur an unacceptable overhead if the procedure is called very often.
Related
How do I modify a Stored Procedure to Join an additional table?
I have a fairly complex Stored Procedure that is joining several tables together, but I need yet another column called in from a table that is yet to be joined. Here's the Stored Procedure as it stands: CREATE PROCEDURE [rpt].[PlannerShipToLocations_ds1] AS BEGIN SET NOCOUNT ON; DECLARE #sql nvarchar(4000) ,#DateStart nvarchar(10) = '2015-01-01' DECLARE #t TABLE ( [PCN] int ,[Part_Key] int ,[Customer_Address_No] int PRIMARY KEY CLUSTERED ( [Part_Key] ,[Customer_Address_No] ,[PCN] ) ) SET #sql = 'SELECT * FROM OPENQUERY (PLEXRPTSVR, ''SELECT DISTINCT s.[PCN] ,sl.[Part_Key] ,s.[Customer_Address_No] FROM [Sales_v_Shipper_e] AS s INNER JOIN [Sales_v_Shipper_Status_e] AS ss ON s.[PCN] = ss.[PCN] AND s.[Shipper_Status_Key] = ss.[Shipper_Status_Key] INNER JOIN [Sales_v_Shipper_Line_e] AS sl ON s.[PCN] = sl.[PCN] AND s.[Shipper_Key] = sl.[Shipper_Key] WHERE 1 = 1 AND ss.[Shipped] = 1 AND s.[Ship_Date] >= ''''' + #DateStart + ''''' ;'')' INSERT INTO #t EXECUTE sp_executesql #sql ;WITH base AS ( SELECT DISTINCT u.[Last_Name] + ', ' + u.[First_Name] AS [Planner] ,c.[Customer_Code] AS [Customer Code] ,c.[Name] AS [Customer Name] ,a.[Customer_Address_Code] AS [Customer Address Code] **/* xxx.[Country] AS [Country] */** FROM [plx].[Part_v_Customer_Part_e] cp INNER JOIN [plx].[Part_v_Part_e] p ON cp.[Plexus_Customer_No] = p.[Plexus_Customer_No] AND cp.[Part_Key] = p.[Part_Key] INNER JOIN [plx].[Common_v_Customer_e] c ON cp.[Plexus_Customer_No] = c.[Plexus_Customer_No] AND cp.[Customer_No] = c.[Customer_No] INNER JOIN [plx].[Plexus_Control_v_Plexus_User_e] u ON p.[Plexus_Customer_No] = u.[Plexus_Customer_No] AND p.[Planner] = u.[Plexus_User_No] OUTER APPLY ( SELECT [Customer_Address_Code], **/*[Country]*/** FROM [plx].[Common_v_Customer_Address_e] a INNER JOIN #t t ON a.[Plexus_Customer_No] = t.[PCN] AND a.[Customer_Address_No] = t.[Customer_Address_No] **/* INNER JOIN [plx].[Common_v_Country] xxx ON a.[Country_Key] = xxx.[Country_Key] */** WHERE a.[Plexus_Customer_No] = p.[Plexus_Customer_No] AND a.[Customer_No] = c.[Customer_No] AND t.[Part_Key] = p.[Part_Key] AND a.[Ship_To] = 1 ) a ** /* INNER JOIN [plx].[Common_v_Country] xxx ON a.[Country_Key] = xxx.[Country_Key] */ /* OUTER APPLY ( SELECT [Country] FROM [plx].[Common_v_Country] xxx INNER JOIN #t t ON a.[Country_Key] = xxx.[Country_Key] WHERE a.[Plexus_Customer_No] = p.[Plexus_Customer_No] AND a.[Customer_No] = c.[Customer_No] AND t.[Part_Key] = p.[Part_Key] AND a.[Ship_To] = 1 ) xxx */** WHERE 1 = 1 AND u.[Lockout] = 0 AND p.[Part_Status] IN ('Production', 'Production - Near EOP', 'Pre-Production') AND cp.[Active] = 1 ) SELECT t1.[Planner] ,t1.[Customer Code] ,t1.[Customer Name] ,STUFF( (SELECT ' | ' + t2.[Customer Address Code] FROM base t2 WHERE t1.[Planner] = t2.[Planner] and t1.[Customer Code] = t2.[Customer Code] ORDER BY t2.[Customer Address Code] FOR XML PATH(''), TYPE ).value('.','varchar(max)') ,1,3,'') AS [Customer Address Code(s)] FROM base t1 GROUP BY t1.[Planner] ,t1.[Customer Code] ,t1.[Customer Name] ORDER BY [Customer Code] ,[Planner] ,[Customer Address Code(s)] END GO I've bolded the sections that are my best guesses about how to go about joining this additional table, I recognize that I wouldn't use all of them but I wanted to show my thoughts. To break it down: 1.) [plx].[Common_v_Customer_Address_e] a AND [plx].[Common_v_Country] xxx are the two tables I need in order to call [Country] out by name. I essentially need to add this as a column displayed on the report and eventually will need to sort on it as well (I'll probably add it to ORDER BY at the end). 2.) I'm not sure if I need to be joining [plx].[Common_v_Country] xxx within the OUTER APPLY or if it needs its own separate INNER JOIN or what. I've illustrated and commented out both here. So far everything I've tried results in "The multi-part identifier 'xxx.Country' could not be bound." Thanks for the help. I've tried modifying the OUTER APPLY to include the new table. I've tried creating my own new INNER JOIN. I've tried creating my own new OUTER APPLY.
Dynamic Creation of External Tables in Synapse
I have a json string which is the result of Get Metadata activity in data factory in the following format: {"structure": [{"name": "Id","type": "String"},{"name": "IsDeleted","type": "Boolean"},{"name": "RowVersion","type": "Byte[]"}, {"name": "ModelId","type": "String"}] This json is showing the schema of parquet file. I pass this json along with the parquet filename and uctnow() to a stored procedure to create a table of schema for that file with the following code CREATE OR ALTER PROCEDURE [CreateExternalTables] ( #schema NVARCHAR (MAX), #tableName NVARCHAR(MAX), #ExecTime NVARCHAR(MAX) ) AS BEGIN IF OBJECT_ID('tempdb..#tables_to_create', 'U') IS NOT NULL DROP TABLE #tables_to_create CREATE TABLE #tables_to_create ( tableName NVARCHAR (MAX), fieldOrder NVARCHAR (MAX), fieldName NVARCHAR (MAX), fieldType NVARCHAR (MAX), translatedType NVARCHAR (MAX), executeTime NVARCHAR (MAX) ) BEGIN WITH Fields (fieldOrder, fieldName, fieldType) AS ( SELECT [key] AS fieldOrder, JSON_VALUE([value], '$.name') AS fieldName, JSON_VALUE([value], '$.type') AS fieldType FROM OPENJSON(#schema) ) INSERT INTO #tables_to_create( tableName, fieldOrder, fieldName, fieldType, translatedType, executeTime ) SELECT #tableName, fieldOrder, fieldName, fieldType, CASE WHEN fieldType = 'Single' THEN 'real' WHEN fieldType = 'Boolean' THEN 'bit' WHEN fieldType = 'Double' THEN 'float' WHEN fieldType = 'Int64' THEN 'bigint' ELSE NULL END AS translatedType, #ExecTime FROM Fields END END; The overall setup is like below: But I receive the following error which I'm not sure why: What I would like to do is to create this temo table in stored procedure in order to create external tables in a automatic way. To be precise I'm following this link to do it: create-external-dataset Any help is highly appreciated. UPDATE After solving the first problem with #wBob 's help I follow the rest of the link to create the external tables with the following which I inserted into my stored procedure: FROM Fields END Declare #sqlCommand nvarchar(max); Declare #folderPath nvarchar(max); SET #sqlCommand = 'IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''[dbo].[' + #tableName + ']'') AND type in (N''U'')) CREATE EXTERNAL TABLE [dbo].[' + #tableName + '] (' WHILE((SELECT COUNT(*) FROM #tables_to_create) > 0) BEGIN DECLARE #key int SELECT #key = MIN(fieldOrder) FROM #tables_to_create WHERE executeTime = #ExecTime DECLARE #fieldName VARCHAR(50) DECLARE #translatedType VARCHAR(50) SELECT #fieldName = fieldName, #translatedType = translatedType FROM #tables_to_create WHERE fieldOrder = #key AND executeTime = #ExecTime SET #sqlCommand = #sqlCommand + ' [' + #fieldName + '] [' + #translatedType + '] NULL' DELETE FROM #tables_to_create WHERE fieldOrder = #key AND executeTime = #ExecTime IF((SELECT COUNT(*) FROM #tables_to_create WHERE executeTime = #ExecTime) > 0) SET #sqlCommand = #sqlCommand + ', ' END SET #sqlCommand = #sqlCommand + ' ) WITH ( LOCATION = ''/' + /main/json/ + ''', DATA_SOURCE = DataLakeStaged, FILE_FORMAT = StagedParquet )' EXEC(#sqlCommand) END; However I receive the following error:
As per the error message, queries are not supported in the conditions of WHILE statement in Azure Synapse Analytics. Dedicated SQL pools are MPP systems that have some slight differences from box-product SQL Server and Azure SQL DB and more generally, loops are a pattern that don't translate that well. You should consider creating a stored procedure that does not loop and leave any looping required to the For Each activity in Azure Data Factory (ADF) / Synapse Pipelines which can loop in parallel. If you do require a loop, there are multiple examples of doing loops in dedicated SQL pools online, but there is a good example in the official documentation, reproduced here in the event the link moves: -- First, create a temporary table containing a unique row number used to identify the individual statements: CREATE TABLE #tbl WITH ( DISTRIBUTION = ROUND_ROBIN ) AS SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS Sequence , [name] , 'UPDATE STATISTICS '+QUOTENAME([name]) AS sql_code FROM sys.tables ; -- Second, initialize the variables required to perform the loop: DECLARE #nbr_statements INT = (SELECT COUNT(*) FROM #tbl) , #i INT = 1 ; -- Now loop over statements executing them one at a time: WHILE #i <= #nbr_statements BEGIN DECLARE #sql_code NVARCHAR(4000) = (SELECT sql_code FROM #tbl WHERE Sequence = #i); EXEC sp_executesql #sql_code; SET #i +=1; END
JOIN in UPDATE sql for DB2
Is there a way to use joins in update statements for DB2? Google has really let me down. This is roughly what I'm trying to achieve (... except obviously working ....) Update gk.WR_VEHICLE_WARRANTY w join gk.VGARANT_FRIST_ZUWEIS z on z.PK_GARANT_FRIST_ZUWEIS = w.FK_GARANT_FRIST_ZUWEIS set CURRENT = '1' where z.GW = '1' and z.FK_GBE is null and z.INTERN = '0';
I solved the problem , just took out the join and did a inner select Update gk.WR_VEHICLE_WARRANTY set CURRENT = '1' Where FK_GARANT_FRIST_ZUWEIS in (select PK_GARANT_FRIST_ZUWEIS from gk.VGARANT_FRIST_ZUWEIS z where z.GW = '1' and z.FK_GBE is null and z.INTERN = '0' )
Stored Procedure not giving the correct results in MVC Project
The SP that I use works as it should in SQL Management studio but this is the only query that gets me the wrong results in my project. The SP query is: SELECT DISTINCT Vehicle_Model_Range.ModelRange_ID, Users.MapLat, Users.MapLong, Users.User_ID, Users.Status_ID, Vehicle_Details.Approved, Vehicle_Details.Vehicle_Type FROM Users INNER JOIN Vehicle_Details ON Users.User_ID = Vehicle_Details.User_ID INNER JOIN Vehicle_Make ON Vehicle_Details.Make_ID = Vehicle_Make.Make_ID INNER JOIN Vehicle_Model ON Vehicle_Details.Model_ID = Vehicle_Model.Model_ID INNER JOIN Vehicle_Model_Range ON Vehicle_Model.ModelRange_Id = Vehicle_Model_Range.ModelRange_ID WHERE (Vehicle_Details.Approved = 1) AND (Users.Status_ID = 1) AND (Vehicle_Details.Vehicle_Type = 'V') AND (Users.MapLat IS NOT NULL) AND (Users.MapLong IS NOT NULL) AND (Vehicle_Model_Range.ModelRange_ID IS NOT NULL) Here's the code in VS that uses it: public ActionResult Search(string Location) { GoogleMapEntities GE = new GoogleMapEntities(); var GetVeh = db.GetMapSearch().Where(x => (x.ModelRange_ID.ToString().StartsWith(Location)) && (x.Vehicle_Type == "V") && (x.Status_ID == 1) && (x.Approved == true)).ToList(); return Json(GetVeh, JsonRequestBehavior.AllowGet); } Basically, what happens is that it plots all the points onto the map and does not follow the where statement that says that it should only plot for the following where conditions: WHERE (Vehicle_Details.Approved = 1) AND (Users.Status_ID = 1) AND (Vehicle_Details.Vehicle_Type = 'V') AND (Users.MapLat IS NOT NULL) AND (Users.MapLong IS NOT NULL) AND (Vehicle_Model_Range.ModelRange_ID IS NOT NULL) I have updated the model a few times and the SP does not work. EDIT: I have just added those where conditions to the linq query but still does not work. Any ideas? EDIT: Full Stored Proc USE [X_DB] GO /****** Object: StoredProcedure [dbo].[sp_App_MapSearch] Script Date: 2017/06/26 9:16:10 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROC [dbo].[sp_App_MapSearch] AS BEGIN SELECT DISTINCT Vehicle_Model_Range.ModelRange_ID, Users.MapLat, Users.MapLong, Users.User_ID, Users.Status_ID, Vehicle_Details.Approved, Vehicle_Details.Vehicle_Type FROM Users INNER JOIN Vehicle_Details ON Users.User_ID = Vehicle_Details.User_ID INNER JOIN Vehicle_Make ON Vehicle_Details.Make_ID = Vehicle_Make.Make_ID INNER JOIN Vehicle_Model ON Vehicle_Details.Model_ID = Vehicle_Model.Model_ID INNER JOIN Vehicle_Model_Range ON Vehicle_Model.ModelRange_Id = Vehicle_Model_Range.ModelRange_ID WHERE (Vehicle_Details.Approved = 1) AND (Users.Status_ID = 1) AND (Vehicle_Details.Vehicle_Type = 'V') AND (Users.MapLat IS NOT NULL) AND (Users.MapLong IS NOT NULL) AND (Vehicle_Model_Range.ModelRange_ID IS NOT NULL) END
error line 86 for stored procedure
I am trying to get this stored procedure to work. I have been working for a month straight, was up till 3 last night, and am approaching burnout. I really want to get this to work as we have an ad campaign that we spent hundreds on sending traffic to. USE [redhotkitties2005db] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[proc_rhkprod_lock_items_for_paypal] #cfuseridid bigint ,#transaction_guid uniqueidentifier = NULL AS BEGIN SET NOCOUNT ON; declare #tblcartID bigint ,#cfuserid bigint ,#tblproductsid bigint ,#quantity bigint ,#localInventoryID varchar(100) ,#taxologykey bigint ,#name varchar(1000) ,#shortDescription varchar(8000) ,#productDescription varchar(8000) ,#UorLorUL varchar(2) ,#unitPrice money ,#shippingWeight numeric(6, 2) ,#overSize tinyint ,#smallPic1 nvarchar(100) ,#largePic1 nvarchar(100) ,#smallPic2 nvarchar(100) ,#largePic2 nvarchar(100) ,#smallPic3 nvarchar(100) ,#largePic3 nvarchar(100) SET #transaction_guid = coalesce(#transaction_guid,newid()) declare c1 cursor forward_only for select rhkProd_tblCart.tablePK AS tblcartID ,rhkProd_tblProducts.productID as tblproductsid ,rhkProd_tblCart.quantity ,rhkProd_tblProducts.localInventoryID ,rhkProd_tblProducts.name ,rhkProd_tblProducts.taxologyKey ,rhkProd_tblProducts.shortDescription ,rhkProd_tblProducts.productDescription ,rhkProd_tblProducts.UorLorUL ,rhkProd_tblProducts.unitPrice ,rhkProd_tblProducts.shippingWeight ,rhkProd_tblProducts.overSize ,rhkProd_tblProducts.smallPic1 ,rhkProd_tblProducts.largePic1 ,rhkProd_tblProducts.smallPic2 ,rhkProd_tblProducts.largePic2 ,rhkProd_tblProducts.smallPic3 ,rhkProd_tblProducts.largePic3 from rhkProd_tblCart INNER JOIN rhkProd_tblProducts on rhkProd_tblCart.tblProductsFK = rhkProd_tblProducts.productID where rhkProd_tblCart = #cfuserid open c1 fetch next from c1 into #tblcartID ,#cfuserid ,#tblproductsid ,#quantity ,#localinventoryID ,#name ,#taxologykey ,#shortdescription ,#productdescription ,#UorLorUL ,#unitprice ,#shippingweight ,#oversize ,#smallpic1 ,#largepic1 ,#smallpic2 ,#largepic2 ,#smallpic3 ,#largepic3 begin while ##fetch_status = 0 insert into rhkprod_tblpaypallock ( ,[cfuserid] ,[transaction_guid] ,[timestamp] ,[tblproductsFK] ,[quantity] ,[localInventoryID] ,[name] ,[taxologykey] ,[shortdescription] ,[productdescription] ,[uorlorul] ,[unitprice] ,[shippingweight] ,[oversize] ,[smallpic1] ,[largepic1] ,[smallpic2] ,[largepic2] ,[smallpic3] ,[largepic3] ) values ( ,#cfuserid ,#transaction_guid ,getdate() ,#tblproductsid ,#quantity ,#localinventoryID ,#name ,#taxologykey ,#shortdescription ,#productdescription ,#UorLorUL ,#unitprice ,#shippingweight ,#oversize ,#smallpic1 ,#largepic1 ,#smallpic2 ,#largepic2 ,#smallpic3 ,#largepic3 ) update rhkprod_tblproducts set quantity = quantity - #quantity where productid = #tblproductsid and UorLorUL in ('U','L') fetch next from c1 into #tblcartID ,#cfuserid ,#tblproductsid ,#quantity ,#localinventoryID ,#name ,#taxologykey ,#shortdescription ,#productdescription ,#UorLorUL ,#unitprice ,#shippingweight ,#oversize ,#smallpic1 ,#largepic1 ,#smallpic2 ,#largepic2 ,#smallpic3 ,#largepic3 close c1 deallocate c1 end END
Your INSERT statement has a stray comma at the start: insert into rhkprod_tblpaypallock ( ,[cfuserid] Should be insert into rhkprod_tblpaypallock ( [cfuserid] Your VALUES clause also has a stray comma: values ( ,#cfuserid Should be values ( #cfuserid