dbTalk Databases Forums  

Dynamic where clause in stored procedure

sybase.public.sqlanywhere.general sybase.public.sqlanywhere.general


Discuss Dynamic where clause in stored procedure in the sybase.public.sqlanywhere.general forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
janis
 
Posts: n/a

Default Dynamic where clause in stored procedure - 11-07-2009 , 08:08 PM






Hi, I'm working in Sybase 9.0.2.2451
I'm new in Sybase and I'm trying to create a dynamic where clause in a
stored procedure.
This is my procedure:

ALTER PROCEDURE "DBA"."Testing"( in iwhere VarChar(10) )

BEGIN
Select * from tableX
where columnname = 1
and columnname3 = 100
and columnname5 = 100
END

If parameter iwhere = 'Y' I want to make the last two sentences
starting with 'and' in my where clause invisible, But how do i do
that ?

I realy hope someone copuld help me.

Reply With Quote
  #2  
Old   
Chris Keating (Sybase iAnywhere)
 
Posts: n/a

Default Re: Dynamic where clause in stored procedure - 11-07-2009 , 08:40 PM






You need to build the statement as a string and use EXECUTE IMMEDIATE.

-chris

janis wrote:
Quote:
Hi, I'm working in Sybase 9.0.2.2451
I'm new in Sybase and I'm trying to create a dynamic where clause in a
stored procedure.
This is my procedure:

ALTER PROCEDURE "DBA"."Testing"( in iwhere VarChar(10) )

BEGIN
Select * from tableX
where columnname = 1
and columnname3 = 100
and columnname5 = 100
END

If parameter iwhere = 'Y' I want to make the last two sentences
starting with 'and' in my where clause invisible, But how do i do
that ?

I realy hope someone copuld help me.

Reply With Quote
  #3  
Old   
Krisztián Pintér
 
Posts: n/a

Default Re: Dynamic where clause in stored procedure - 11-09-2009 , 04:47 AM



On Sun, 08 Nov 2009 03:08:17 +0100, janis <janimie (AT) gmail (DOT) com> wrote:

Quote:
ALTER PROCEDURE "DBA"."Testing"( in iwhere VarChar(10) )

BEGIN
Select * from tableX
where columnname = 1
and columnname3 = 100
and columnname5 = 100
END

If parameter iwhere = 'Y' I want to make the last two sentences
starting with 'and' in my where clause invisible, But how do i do
that ?

you don't need dynamic where for that. consider:


Select * from tableX
where columnname = 1
and ( (columnname3 = 100 and columnname5 = 100) or iwhere = 'Y')


the expression in the 3rd row evaluates to true if the parameter is Y,
regardless of the values of column 3 and 5. if the parameter is not Y,
the filters are in effect.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.