Internet_Engineer
asked on
How can I construct a sql statement using variables to change the where clause with several parameters
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.
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.
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+@Max imumRows As Varchar(3))+';';
Exec (@SQL1 + @SortCondition + @SQL2 + @WhereCondition + @SQL3 + @SQL4);
SET ROWCOUNT 0
END
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+@Max
Exec (@SQL1 + @SortCondition + @SQL2 + @WhereCondition + @SQL3 + @SQL4);
SET ROWCOUNT 0
END
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
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.
I've never actually used a CASE in WHERE statement. Hope this helps.
IS NOT NULL
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]
I've never actually used a CASE in WHERE statement. Hope this helps.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I was able to find solution outside the site
Open in new window