enclose quotes in variable.

Rakesh Dhar 96 Reputation points

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
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

    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

    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

    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