declare @sql varchar(max)
declare @a int ,@b int , @c int , @d int
set @sql = '';
if @a = 1
set @sql = 'WHERE a = 1'
if @b = 1
if @sql <> ''
set @sql = 'AND b = 1'
else
set @sql = 'WHERE b = 1'
if @c = 1
if @sql <> ''
set @sql = 'AND c = 1'
else
set @sql = 'WHERE c = 1'
if @d = 1
if @sql <> ''
set @sql = 'AND d = 1'
else
set @sql = 'WHERE d = 1'
set @sql = 'SELECT * FROM TABLEA ' + @sql
exec(@sql)
declare @sql varchar(max)
declare @a int ,@b int , @c int , @d int
set @sql = '';
if @a = 1
set @sql = ' WHERE a = 1'
if @b = 1
if @sql <> ''
set @sql = ' AND b = 1'
else
set @sql = ' WHERE b = 1'
if @c = 1
if @sql <> ''
set @sql = ' AND c = 1'
else
set @sql = ' WHERE c = 1'
if @d = 1
if @sql <> ''
set @sql = ' AND d = 1'
else
set @sql = ' WHERE d = 1'
set @sql = 'SELECT * FROM TABLEA ' + @sql
exec(@sql)
Declare @s int
SET @s = @a + @b + @c + @d
SELECT * FROM table
WHERE
a = CASE WHEN @s = 0 THEN a WHEN @a = 1 THEN 1 ELSE 2 END
OR b = CASE WHEN @s = 0 THEN b WHEN @b = 1 THEN 1 ELSE 2 END
OR c = CASE WHEN @s = 0 THEN c WHEN @c = 1 THEN 1 ELSE 2 END
OR d = CASE WHEN @s = 0 THEN d WHEN @d = 1 THEN 1 ELSE 2 END
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
VIDEO DATASOURCE Control Resize issue | 2 | 20 | |
Return 0 on SQL count | 24 | 30 | |
CPU high usage when update statistics | 2 | 30 | |
Have a conversion issue with varchar to int in a SQL: Query. | 1 | 30 |
Join the community of 500,000 technology professionals and ask your questions.