Solved

Dynamic where clause

Posted on 2010-09-08
3
812 Views
Last Modified: 2012-05-10
Can someone show how to create a dynamic where clause in Sybase?

Eg.
I have a select statement with a where clause.

But  based on the count of rows returned from a table I want to add one more condition....
if the count is 0 then no additional condition needs to be added to the where clause
and if the count > 1, then I want to add the values returned by another select query  in the where clause  condition.

Thanks.
0
Comment
Question by:4Learning
3 Comments
 
LVL 14

Expert Comment

by:Jan_Franek
ID: 33634224
You can dynamicaly create your select command, store it into varchar variable and then execute it using dynamic exec - see http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.commands/html/commands/commands56.htm
0
 
LVL 2

Accepted Solution

by:
Rajesh_Anandappan earned 500 total points
ID: 33795793
example:

declare @sql_string varchar(255)
         ,@val varchar(10)

select @sql_string = 'select age from <table>'
      ,@val = 'joe'

IF exists (select 1 from <table>) -- if rows are there in this table
   BEGIN
      select @sql_string = @sql_string + ' where name = '+@val
      exec(@sql_string)
   END
ELSE
   BEGIN
      select 'No rows in table'
   END
0
 

Author Closing Comment

by:4Learning
ID: 33851616
Thankyou for the example. I tried the variable for the select clause, the from clause and the where clause and added them all up in one string and then passed that string as a parameter to the exec command as you listed above.

Thanks.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Troubleshooting common task sequence error codes
One of the biggest threats in the cyber realm pertains to advanced persistent threats (APTs). This paper is a compare and contrast of Russian and Chinese APT's.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now