This one is new for me. It might be something you have to consider all the time. I do not.
Stored procedures in SQL may have defaults for certain options set only at creation. So you can't override them or even alter them. You have to drop and recreate them if those options have an affect on your query.
This probably doesn't affect 99% of your queries. It doesn't affect mine too much. Today I had to so a bit of XML in my stored procedure though. It worked in dev fine because of course my options are set differently than in another server. When I gave the script to someone else to try... whammo!
SELECT failed because the following SET options have incorrect settings: 'QUOTED_IDENTIFIER'
What the hell does that mean? Is this Office Space? PC LOAD LETTER!
No it's not a movie. This is real life and SQL has this cool table called sys.sql_modules. It can store options that you have set when you created that bloody stored procedure that isn't working now.
SELECT name = OBJECTNAME([objectid]), usesquotedidentifier
WHERE OBJECTNAME([object_id]) IN (N'sproc1', N'spoc2');
In this example our stored procedure is called sproc1. The columns we can look for are:
For this example our results prove that we do have a problem.
So how do we fix this. We can't do an alter. We have to recreate the stored procedure. Yep, what a pain in the butt. It's because of the darn XML and STUFF() that we are doing to put some rows all together into a delimited string. Please don't focus on that.. it's the QUOTED_IDENTIFIER ON that is the bug.
--drop the sp and recreate with the correct set options.
IF EXISTS (SELECT * FROM sys.objects WHERE objectid = OBJECTID(N'[dbo].[sproc1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sproc1]
SET ANSI_NULLS ON
-- important for xml queries
SET QUOTED_IDENTIFIER ON
CREATE PROCEDURE [dbo].[sproc1]
-- important for mapping in Entity Framework SET FMTONLY OFF
-- SELECT an existing row from the table.
CREATE TABLE #Things
( [Id] INT, [Description] VARCHAR (200), [Date] DATETIME NOT NULL )
INSERT INTO #Things(Id, Description, Date)
SELECT ID, Description, Date
INNER JOIN TableB ON TableB.ID = TableA.ID
WHERE Description IS NOT NULL
SELECT ', ' + b.Description
FROM #TableA b
WHERE (b.Id = a.Id)
FOR XML PATH(''),TYPE).value('(./text())','VARCHAR(MAX)')
,1,2,'') AS Things FROM #Things a
GROUP BY Id
DROP TABLE #Things