Results 1 to 4 of 4

Thread: [RESOLVED] could u tell me how to write a dynamic query which can hold a varchar type?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Resolved [RESOLVED] could u tell me how to write a dynamic query which can hold a varchar type?

    Hi All,
    This is regarding SQL server 2000.

    How can i write a Dynamic query so that i can hold a varchar type variable also?
    please see the below sample example what i have written..

    Declare @varSql varchar(2000)
    Declare @varName varchar(30)
    set @varName= 'Harvinder'
    set @varSql= 'select * from employee where empname = ' + ' ' varName ' '
    Execute (@varSql)

    I cam to know that i have to use two single quotes to represent a varchar type variable.. still i am getting error.
    could u please say how to resolve it?

    Thanks:
    regards:
    raghunadhs.v

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: could u tell me how to write a dynamic query which can hold a varchar type?

    Code:
    Declare @varSql varchar(2000)
    Declare @varName varchar(30)
    set @varName= 'Harvinder'
    set @varSql= 'select * from employee where empname = ' + '''' + @varName + ''''
    or to make life easier

    Code:
    Set Quoted_Identifier off
    
    Declare @varSql varchar(2000)
    Declare @varName varchar(30)
    set @varName= "Harvinder"
    
    set @varSql= "select * from Customers where CustomerId = '" + @varName + "'"
    
    exec (@varSQL)
    Last edited by brucevde; Oct 11th, 2007 at 10:08 AM.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    167

    Re: could u tell me how to write a dynamic query which can hold a varchar type?

    Hi brucevde,
    Thank you very much.it is fine and working well. could you pls explain what is the need of two single quotes and how it will be treated those two singel quotes.
    Thanks & Regards:
    raghunadhs.

    ]
    Code:
    Declare @varSql varchar(2000)
    Declare @varName varchar(30)
    set @varName= 'Harvinder'
    set @varSql= 'select * from employee where empname = ' + '''' + @varName + ''''
    or to make life easier

    Code:
    Set Quoted_Identifier off
    
    Declare @varSql varchar(2000)
    Declare @varName varchar(30)
    set @varName= "Harvinder"
    
    set @varSql= "select * from Customers where CustomerId = '" + @varName + "'"
    
    exec (@varSQL)
    [/QUOTE]

  4. #4
    Hyperactive Member Foxer's Avatar
    Join Date
    Oct 2001
    Location
    Australia
    Posts
    278

    Re: could u tell me how to write a dynamic query which can hold a varchar type?

    This statement

    Set Quoted_Identifier off

    Lets you use double quotes (") to mark the start and stop of your string and use single quotes (') to mark your variables.

    http://doc.ddart.net/mssql/sql70/set-set_30.htm

    It's really just a readability thing and in your case - readability can make a big difference.
    Rate my response if I helped

    Go Hard Or Go Home


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width