Solved

Dynamic where clause

Posted on 2010-09-08
3
811 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Copy a TadoDataSet to another and apply changes to database. 11 1,252
SQL Time Syntax 4 291
Sybase SQL Syntax 2 275
How do I get Sybase ASE to throw an error that Java can catch? 4 511
Note: This is the third blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   We’ve been talki…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

930 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

10 Experts available now in Live!

Get 1:1 Help Now