enclose quotes in variable.

Rakesh Dhar 96 Reputation points
2021-10-08T09:15:15.24+00:00

declare @qry varchar(500),@Shawn Branch _id varchar(100),@Paramjeet Dahiya varchar(900)
set @Shawn Branch _id=0912
select @qry='select * from account_charge_tbl where Branch_id=' + ''+@Shawn Branch _id ''
select @qry

Please friends help here ......... getting error in this
i need to enclose the Branch_id in single quotes .

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,322 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,597 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rakesh Dhar 96 Reputation points
    2021-10-08T12:09:27.057+00:00

    to all friends ......thank you to all of you ......... within out talks i got the solution

    'select * from account_charge_tbl where Branch_id='+''''+@Shawn Branch _id+''''

    this line got executed and i got my result ...........

    4 single codes ..i added

    thanks all

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Olaf Helper 43,161 Reputation points
    2021-10-08T09:46:17.057+00:00

    Simple quote them with an addition apostrophe =>

    declare @qry varchar(500),@Branch_id varchar(100),@param varchar(900)
    set @Branch_id=0912
    select @qry='select * from account_charge_tbl where Branch_id= ''' + @Branch_id + '''';
    select @qry
    

    =>

    select * from account_charge_tbl where Branch_id= '912'
    

  2. Yogesh Bhadauriya 26 Reputation points
    2021-10-08T09:47:33.447+00:00

    To correct your query, you will need to use below suggestion
    SELECT @qry='select * from account_charge_tbl where Branch_id='''+@Shawn Branch _id+''''

    I am recommending you to use parameterized query to pass parameter. Evaluate below query which will do job for you.

    DECLARE @qry NVARCHAR(500),@Branch_id varchar(100),@param NVARCHAR(900)  
      
    SET @Branch_id=0912  
      
    SELECT @qry='select * from account_charge_tbl where Branch_id=@Branch_id'  
      
    SET @param='@Branch_id varchar(100)'  
      
    EXEC SP_EXECUTESQL @qry,@param,@Branch_id