Ad Hoc safe statements
Last updated:
Warning: Review and test in a non-production environment before running.
-- Adapted from demo scripts by Kimberly L. Tripp, SQLskills – https://www.sqlskills.com/blogs/kimberly/
SELECT [m].*
FROM [dbo].[member] AS [m]
WHERE [m].[member_no] = 258;
GO
SELECT [m].*
FROM [dbo].[member] AS [m]
WHERE [m].[member_no] = 34567;
GO
SELECT [m].*
FROM [dbo].[member] AS [m]
WHERE [m].[member_no] = 35;
GO
We can see that 3 statements was created for every query.
The literal value size is the reason for the 3 statements.
The implicit cast on the value was int, smallint and tinyint.
This statements are called "SAFE" becouse sql server replaces the value
with a parameter for all 3 statements.
relevant statement will be executed without creating a new one.