|
-
Oct 11th, 2007, 08:35 AM
#1
Thread Starter
Addicted Member
[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
-
Oct 11th, 2007, 10:04 AM
#2
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.
-
Oct 14th, 2007, 01:15 AM
#3
Thread Starter
Addicted Member
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]
-
Oct 14th, 2007, 07:58 PM
#4
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|