Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Dynamic SQL

Author  Topic 

gk70
Starting Member

11 Posts

Posted - 2004-10-07 : 09:19:10
Great Article on Dynamic SQL! But my problem is I have an SQL Query which has in 'where clause' comditions like sex='M' etc(this can vary depending on query passed)I need to execute this query within an SP.. so how to use delimiters so as to escape sequence 'm' or whatever it is , in exec statement

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-10-07 : 09:33:02
Select 'This is how you include the ('').'

Corey
Go to Top of Page

ehorn
Master Smack Fu Yak Hacker

1632 Posts

Posted - 2004-10-07 : 09:35:35
[code]declare @sql varchar(8000)
declare @var char(1); set @var = 'M'

set @sql = 'SELECT <something> '
set @sql = @sql + 'FROM <sometable> '
set @sql = @sql + 'WHERE <somefield> = ''' + @var + ''''

select @sql
--exec (@sql)
[/code]You may not need dynamic SQL here though. Can you give a more specific description of your problem.
Go to Top of Page

gk70
Starting Member

11 Posts

Posted - 2004-10-07 : 09:51:01
Well ehorn.
Iam generating an SQL Query depending on user conditions to create member groups.It can include one or more conditions including comparisions.I need to distinguish the result set so produced which would enable me to display such records in seperate areas,So to distinguish the result set I need to concatenate the SQL query passed with another condition.Then i can use sp_executesql or execute,But how do I escape sequence conditions like mem_sex='M' and mem_status='ALIVE' etc(this can change depending on user selection)

For eg: exec('select * from memberdetails where meme_Sex='M'') will genereate an error .in nutshell how do I escape sequence ''?
Go to Top of Page

Amethystium
Aged Yak Warrior

701 Posts

Posted - 2004-10-07 : 10:00:48
How about :


exec ('''' + replace(@ssql ,'''', '''''') + '''')




------------->>> BREAKING NEWS!!! <<<-------------
Saddam Hussien has weapons of mass destrcution
Go to Top of Page

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2004-10-07 : 10:10:47
pass the criteria in a variable like what ehorn suggested?
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-10-07 : 10:37:00
Declare @sqlStr nvarchar(1000)

Set @sqlStr = 'select * from memberdetails where meme_Sex=''M'''
exec(@sqlStr)


Corey
Go to Top of Page
   

- Advertisement -