Just a learner
Just a learner

Reputation: 28572

variables in sp_executesql

I have a piece of code:

DECLARE @v int;  
DECLARE @SQLString nvarchar(500);  
DECLARE @ParmDefinition nvarchar(500);  
DECLARE @max_title varchar(30);  

SET @IntVariable = 197;  
SET @SQLString = N'SELECT @max_titleOUT = max(JobTitle)   
   FROM some_table 
   WHERE name = @level';  
SET @ParmDefinition = N'@level tinyint, @max_titleOUT varchar(30) OUTPUT';  

EXECUTE sp_executesql @SQLString, @ParmDefinition, @level = @v, @max_titleOUT=@max_title OUTPUT;  
SELECT @max_title;  

I'm a little bit confused about this line:

       WHERE name = @level';

If @v is a string instead of an int variable, should I put quotes around @level like this?

       WHERE name = ''@level''';

When should I put quotes around a variable inside the @SQLString?

Upvotes: 1

Views: 344

Answers (1)

Backs
Backs

Reputation: 24903

No, @level and @v are variables. You shouldn't put quotes around.

When should I put quotes around a variable inside the @SQLString?

Nowhere

Upvotes: 2

Related Questions