Solved

Help with Sql

Posted on 2011-03-16
5
253 Views
Last Modified: 2012-05-11
@a  int  ,@b  int  , @c  int  , @d   int


table

id   name   a    b   c    d


a,b,c,d have values of 0 or 1  only

@a belongs to column a , @b belongs to column b , @c belongs to column c , @d belongs to column d

@a,@b,@c and @d are populated when I click on check box in the front end


If only @a is checked then @a will be 1 and all the variables will be 0 . I want the value from table when  a = 1 .If only @a is 1
ans same logic for all variable if only 1 variable is checked in the front end

if a user checks more than 1(ex @b and @d)  than I want all  the values in which column b is 1 or column d is 1 (Basically b= 1 or d=1)

If the user don't check any variable I want all the data from table.

At this point I can only think of 'if' statement can this be done in any other method
0
Comment
Question by:vijay11
[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 32

Accepted Solution

by:
Ephraim Wangoya earned 167 total points
ID: 35152623

This is better handled in a stored procedure but here is the sequence you could use

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)

Open in new window

0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35152631
You need spaces in the filter
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)

Open in new window

0
 
LVL 11

Assisted Solution

by:JoeNuvo
JoeNuvo earned 167 total points
ID: 35153434

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

Open in new window

0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 166 total points
ID: 35153614
You can try something like this:

select * 
  from YourTable t
 where t.a = (2 - @a)
    or t.b = (2 - @b)
    or t.c = (2 - @c)
    or t.d = (2 - @d)
    or (@a + @b + @c + @d) = 0

Open in new window

0
 

Author Closing Comment

by:vijay11
ID: 35156217
All the Answers were Excellent .

Thanks for the help
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

734 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