You are right, the (@be int) shown applies to parameterized queries. Applications often parameterize the queries using sp_executesql
, and then sent them to the sql server.
The query will be cached as (variables)QueryText
. And ofcourse, the values won't be cached in the text, since the query is parameterized.
Parameterized query example
Test Data
CREATE SCHEMA PERSON;
CREATE TABLE Person.Person( BusinessEntityID int );
INSERT INTO Person.Person(BusinessEntityID)
VALUES(1),(2),(3);
Query
exec sp_executesql N'SELECT * FROM Person.Person WHERE BusinessEntityID = @be',N'@be int',@be=2
See the result in the cache with this query
select text from sys.dm_exec_query_stats
cross apply sys.dm_exec_sql_text(sql_handle)
where text like '%Person%';
or
(@be int)SELECT * FROM Person.Person WHERE BusinessEntityID = @be
With a procedure the caching will be different
Create the procedure
use test
go
create procedure dbo.myproc @dbname varchar(255)
as
select * from sys.databases where name = @dbname
Run the proc
exec dbo.myproc @dbname= 'master';
Result in cache
select text from sys.dm_exec_query_stats
cross apply sys.dm_exec_sql_text(sql_handle)
where text like '%myproc%';
or
create procedure dbo.myproc @dbname varchar(255) as select * from sys.databases where name = @dbname
This can also happen when forced parameterization is enabled
Enable forced parameterization
ALTER DATABASE test SET PARAMETERIZATION FORCED
Same query, without parameters
SELECT *
FROM Person.Person
WHERE BusinessEntityID = 5
Result in cache
select text from sys.dm_exec_query_stats
cross apply sys.dm_exec_sql_text(sql_handle)
where text like '%Person%'
or
(@0 int)select * from Person . Person where BusinessEntityID = @0