Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / General DB Topics / DB Theory / February 2004

Tip: Looking for answers? Try searching our database.

SQL query

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
PRatcliff - 18 Feb 2004 23:56 GMT
Hello,

I am constructing a stored procedure to run based on user input. What
I WANT to do is basically this:

SELECT *
FROM   tableA A
join   tableB B
on     A.idfield = B.idfield
WHERE  CASE WHEN @userinput = 'A'
      THEN A.datefieldA BETWEEN @startdate AND @enddate
      ELSE B.datefieldB BETWEEN @startdate AND @enddate END

or this:

SELECT *
FROM   tableA A
join   tableB B
on     A.idfield = B.idfield
WHERE  if @userinput = A then A.datefieldA BETWEEN @startdate AND
@enddate
else   B.datefieldB BETWEEN @startdate AND @enddate

I know I can't use CASE in the above fashion, but I want to use a
single query to pull data where the 'where' field can change depending
upon the user's selection. How can I change the contstraint field in
the WHERE clause based on a variable value?

Thanks in advance & cheers,
PRatcliff
Wilhelm Hannemann - 19 Feb 2004 05:53 GMT
PRatcliff schrieb:
> Hello,
>
[quoted text clipped - 8 lines]
>        THEN A.datefieldA BETWEEN @startdate AND @enddate
>        ELSE B.datefieldB BETWEEN @startdate AND @enddate END

[...]

> I know I can't use CASE in the above fashion, but I want to use a
> single query to pull data where the 'where' field can change depending
[quoted text clipped - 3 lines]
> Thanks in advance & cheers,
> PRatcliff

SELECT *
FROM   tableA A
join   tableB B
on     A.idfield = B.idfield
WHERE  (@userinput =A AND A.datefieldA BETWEEN @startdate AND @enddate)
  OR   (@userinput!=A AND B.datefieldB BETWEEN @startdate AND @enddate)

HTH
Wilhelm
PRatcliff - 24 Feb 2004 18:14 GMT
Wilhelm,

All I can say is 'duh'. It seems so obvious now I see it. Another
gaping hole in my SQL knowledge is filled. Vielen Dank

PRatcliff

> PRatcliff schrieb:
> > Hello,
[quoted text clipped - 29 lines]
> HTH
> Wilhelm
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.