Solved

How can I construct a sql statement using variables to change the where clause with several parameters

Posted on 2011-03-18
5
184 Views
Last Modified: 2012-05-11
I am trying to build a sql statement using a variable.

SELECT * FROM Mytable
WHERE
@MyVariaable = (                  
CASE @MyVariaable                              
WHEN 'A' THEN
ColumnA > Null OR ColumnAB > 0 OR ColumnAC > 0
WHEN 'E' THEN
ColumnE > Null OR ColumnEB > 0 OR ColumnEC > 0
END

CASE is very good at returning 1 column or variable, but not multiples.
0
Comment
Question by:Internet_Engineer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35165468
you have to do a OR:
SELECT * FROM Mytable
WHERE ( ( @MyVariable = 'A' AND ColumnA > Null OR ColumnAB > 0 OR ColumnAC > 0 )
      OR( @MyVariable = 'E' AND ColumnE > Null OR ColumnEB > 0 OR ColumnEC > 0 )
      )

Open in new window

0
 
LVL 6

Expert Comment

by:divyeshhdoshi
ID: 35165700
DECLARE @SQL1 varchar(200)
DECLARE @WHERE varchar(500)
SET @SQL1='SELECT * FROM btLocations WHERE'

SET @WHERE=' WHERE 1=1 '

SET @Where = @Where + ' AND <Column1>=<Value1>'
SET @Where = @Where + ' AND <Column2>=<Value2>'
SET @Where = @Where + ' AND <Column3>=<Value3>'
SET @Where = @Where + ' AND <Column4>=<Value4>'

EXEC (@SQL1 + @Where );  

or you can use procedure like this


CREATE PROCEDURE [dbo].<procname>  
 @StartRowIndex int,  
 @MaximumRows int,  
 @SortCondition nvarchar(250),  
 @WhereCondition nvarchar(250)  
AS  
BEGIN  
 
 SET NOCOUNT ON  
 
 Declare @SQL1 Varchar(MAX);  
 Declare @SQL2 Varchar(MAX);  
 Declare @SQL3 Varchar(MAX);  
 Declare @SQL4 Varchar(MAX);  
 
 Set @SQL1='WITH TempTable AS (  
  SELECT ROW_NUMBER() OVER (Order By ';  
 Set @SQL2=' ) AS Row,  
      *  
  FROM [btLocations]  
  Where ';  
 Set @SQL3=' )';  
 
 Set @SQL4='SELECT  
 *  
 FROM TempTable  
 WHERE Row between '+Cast(@StartRowIndex As Varchar(3))+' AND '+Cast(@StartRowIndex+@MaximumRows As Varchar(3))+';';  
   
 
 Exec (@SQL1 + @SortCondition + @SQL2 + @WhereCondition + @SQL3 + @SQL4);  
 
 SET ROWCOUNT 0  
END  
0
 

Expert Comment

by:BlakeRogers
ID: 35165720
You can use the case statement but you have to be clear about what you want. Greater than null isn't really a good thing. Null means nothing, it doesn't mean zero. So you want to make sure it's is not null but using
IS NOT NULL

Open in new window


You also didn't end the parenthesis at the end of your where statement.

Also,

You forgot a THEN statement in your case statement. Here is an example.

CASE WHEN DATEDIFF(DAY, DateReported, @endDate) < 1 THEN DATEDIFF(DAY, DateReported, @endDate) ELSE NULL END AS [LessThanDay]

Open in new window


I've never actually used a CASE in WHERE statement. Hope this helps.
0
 

Accepted Solution

by:
Internet_Engineer earned 0 total points
ID: 35327435
Here is the solution:

(@SEX = 'male' AND (TVSHOWS=@TVSHOWS AND RACING=@RACING AND NASCAR=@NASCAR))

(@SEX = 'female' AND (TVSHOWS=@TVSHOWS AND RACING=@RACING AND NASCAR=@NASCAR))
0
 

Author Closing Comment

by:Internet_Engineer
ID: 35360899
I was able to find solution outside the site
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

691 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