Unfamiliar syntax - Query with Parameters in Braces at the beggining
I have run sp_WhoIsActive on one of our servers using the following syntax:
sp_whoisactive @get_plans = 1, @show_sleeping_spids = 0, @get_outer_command = 1, @get_locks = 1
and found a spid with the sql_command (the column shown when @get_outer_command
is set to 1) as follows
(@p1 int,@p2 int)
Exec MyDatabase.MyProc @p1 @p2
When I try to run a query with this syntax on my test Adventureworks database:
(@be int)
SELECT *
FROM Person.Person
WHERE BusinessEntityID = @be
I get the error
Msg 1050, Level 15, State 1, Line 1
This syntax is only allowed for parameterized queries.
Msg 137, Level 15, State 2, Line 4
Must declare the scalar variable "@FN".
so it seems to be something to do with parameterized queries. This makes sense as the variable @be is never set to a value
What is happening here?
sql-server-2016 syntax parameter sp-whoisactive
add a comment |
I have run sp_WhoIsActive on one of our servers using the following syntax:
sp_whoisactive @get_plans = 1, @show_sleeping_spids = 0, @get_outer_command = 1, @get_locks = 1
and found a spid with the sql_command (the column shown when @get_outer_command
is set to 1) as follows
(@p1 int,@p2 int)
Exec MyDatabase.MyProc @p1 @p2
When I try to run a query with this syntax on my test Adventureworks database:
(@be int)
SELECT *
FROM Person.Person
WHERE BusinessEntityID = @be
I get the error
Msg 1050, Level 15, State 1, Line 1
This syntax is only allowed for parameterized queries.
Msg 137, Level 15, State 2, Line 4
Must declare the scalar variable "@FN".
so it seems to be something to do with parameterized queries. This makes sense as the variable @be is never set to a value
What is happening here?
sql-server-2016 syntax parameter sp-whoisactive
add a comment |
I have run sp_WhoIsActive on one of our servers using the following syntax:
sp_whoisactive @get_plans = 1, @show_sleeping_spids = 0, @get_outer_command = 1, @get_locks = 1
and found a spid with the sql_command (the column shown when @get_outer_command
is set to 1) as follows
(@p1 int,@p2 int)
Exec MyDatabase.MyProc @p1 @p2
When I try to run a query with this syntax on my test Adventureworks database:
(@be int)
SELECT *
FROM Person.Person
WHERE BusinessEntityID = @be
I get the error
Msg 1050, Level 15, State 1, Line 1
This syntax is only allowed for parameterized queries.
Msg 137, Level 15, State 2, Line 4
Must declare the scalar variable "@FN".
so it seems to be something to do with parameterized queries. This makes sense as the variable @be is never set to a value
What is happening here?
sql-server-2016 syntax parameter sp-whoisactive
I have run sp_WhoIsActive on one of our servers using the following syntax:
sp_whoisactive @get_plans = 1, @show_sleeping_spids = 0, @get_outer_command = 1, @get_locks = 1
and found a spid with the sql_command (the column shown when @get_outer_command
is set to 1) as follows
(@p1 int,@p2 int)
Exec MyDatabase.MyProc @p1 @p2
When I try to run a query with this syntax on my test Adventureworks database:
(@be int)
SELECT *
FROM Person.Person
WHERE BusinessEntityID = @be
I get the error
Msg 1050, Level 15, State 1, Line 1
This syntax is only allowed for parameterized queries.
Msg 137, Level 15, State 2, Line 4
Must declare the scalar variable "@FN".
so it seems to be something to do with parameterized queries. This makes sense as the variable @be is never set to a value
What is happening here?
sql-server-2016 syntax parameter sp-whoisactive
sql-server-2016 syntax parameter sp-whoisactive
asked Dec 28 '18 at 9:14
SEarle1986
373213
373213
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f225905%2funfamiliar-syntax-query-with-parameters-in-braces-at-the-beggining%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
edited Dec 28 '18 at 10:07
answered Dec 28 '18 at 9:30
Randi Vertongen
1,203112
1,203112
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f225905%2funfamiliar-syntax-query-with-parameters-in-braces-at-the-beggining%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown