piektdiena, 2018. gada 6. jūlijs

Quoted identifier- don't touch it

Have you ever paid attention to QUOTED_IDENTIFIER settings? It can get very "interesting" if you create procedure with this setting "OFF".

SELECT OBJECT_NAME(object_id), *
FROM sys.sql_modules WHERE uses_quoted_identifier = 0

SELECT OBJECT_NAME(object_id), *
FROM sys.sql_modules WHERE uses_quoted_identifier = 1

This is just interesting (why its better to not touch this setting- ON is default and recommended)

SET QUOTED_IDENTIFIER ON
CREATE TABLE #test ("1" INT)
INSERT INTO #test VALUES (1), (2), (3)
GO
SET QUOTED_IDENTIFIER OFF
SELECT * FROM #test WHERE "1" = 1 -- works, 3 rows returned
GO
SET QUOTED_IDENTIFIER ON
SELECT * FROM #test WHERE "1" = 1 -- works, 1 row returned

DROP TABLE #test
And this shows what

SET QUOTED_IDENTIFIER ON
CREATE TABLE #test (i INt)
CREATE INDEX test ON #test (i) WHERE [i] = 1

GO
SET QUOTED_IDENTIFIER OFF
SELECT * FROM #test WHERE i = 1 -- filtered index ignored
--INSERT INTO #test VALUES (1) -- will fail

GO
SET QUOTED_IDENTIFIER ON
SELECT * FROM #test WHERE i = 1 -- filtered index used
--INSERT INTO #test VALUES (2) -- will work

DROP TABLE #test

And this will work just fine (one "GO" missing, also inserts is not commented out. Put "GO" back and first insert will fail)


SET QUOTED_IDENTIFIER ON
CREATE TABLE #test (i INt)
CREATE INDEX test ON #test (i) WHERE [i] = 1

GO
SET QUOTED_IDENTIFIER OFF
SELECT * FROM #test WHERE i = 1 -- filtered index ignored
INSERT INTO #test VALUES (1) -- will work

SET QUOTED_IDENTIFIER ON
SELECT * FROM #test WHERE i = 1 -- filtered index used
INSERT INTO #test VALUES (2) -- will work 

DROP TABLE #test

Nav komentāru:

Ierakstīt komentāru