views:

1261

answers:

7

Hi

I would like to create a stored procedure with parameters that indicate which fields should be selected.

E.g. I would like to pass two parameters "selectField1" and "selectField2" each as bools.

Then I want something like

SELECT

if (selectField1 = true) Field1 ELSE do not select Field1

if (selectField2 = true) Field2 ELSE  do not select Field2

FROM Table

Thanks Karl

+10  A: 

In SQL, you do it this way:

SELECT  CASE WHEN @selectField1 = 1 THEN Field1 ELSE NULL END,
        CASE WHEN @selectField2 = 1 THEN Field2 ELSE NULL END
FROM    Table

Relational model does not imply dynamic field count.

Instead, if you are not interested in a field value, you just select a NULL instead and parse it on the client.

Quassnoi
+4  A: 

You want the CASE statement:

SELECT
  CASE 
    WHEN @SelectField1 = 1 THEN Field1
    WHEN @SelectField2 = 1 THEN Field2
    ELSE NULL
  END AS NewField
FROM Table

EDIT: My example is for combining the two fields into one field, depending on the parameters supplied. It is a one-or-neither solution (not both). If you want the possibility of having both fields in the output, use Quassnoi's solution.

Welbog
I do this often, although SQL has trouble caching the statements but you can't really get around it.
Kezzer
That may fail if the columns are of different type e.g. int vs char
kristof
A: 

This is a psuedo way of doing it

IF (selectField1 = true) 
SELECT Field1 FROM Table
ELSE
SELECT Field2 FROM Table
Daniel A. White
A: 

what you want is:

 MY_FIELD=
  case 
   when (selectField1 = 1) then Field1
                                 else Field2     
  end,

in the select

However, y don't you just not show that column in your program?

waqasahmed
+1  A: 

Sounds like they wants the ability to return only fields that are allowed which means the number of fields returned also has to be dynamic. This will work with 2 variables. Anything more than than will be getting confusing.

IF (selectField1 = true AND selectField2 = true)
BEGIN
   SELECT Field1, Field2
   FROM Table
END
ELSE IF (selectField1 = true)
BEGIN
   SELECT Field1
   FROM Table
END
ELSE IF (selectField2 = true)
BEGIN
   SELECT Field2
   FROM Table
END

Dynamic SQL will help with multiples. This examples is assuming atleast 1 column is true.

DECLARE @sql varchar(MAX)
SET @sql = 'SELECT '
IF (selectField1 = true)
BEGIN
   SET @sql = @sql + 'Field1, '
END
IF (selectField2 = true)
BEGIN
   SET @sql = @sql + 'Field2, '
END
...
-- DROP ', '
@sql = SUBSTRING(@sql, 1, LEN(@sql)-2)

SET @sql = @sql + ' FROM Table'

EXEC(@sql)
Justin Balvanz
What I want to do is actually a lot more complex than just choosing to select 1 or two fields. I have a bunch of field that I need to be able to dynamically select, as well as doing a WHERE on certain fields, e.g:@whereField1 = 1@whereField2 = 1@whereField3 = 0etc.and then do a WHERE on Field1 only if @whereField1 = 1 etc.It seems to me like your second example might be the only way to really do this.How many characters is varchar(MAX) though?
Karl
SQL 2005 can store up to 8000 characters.
Justin Balvanz
@Justin varchar(max) in SQL2005 is not limited to 8000 characters. varchar(max) was introduced in SQL 2005 as some sort of replacement of text data type with the support of functions available to varchar.
kristof
All I did was google it and that's what I came up with. I've never reached a max.
Justin Balvanz
+1  A: 
@selectField1 AS bit
@selectField2 AS bit

SELECT
CASE 
    WHEN @selectField1 THEN Field1
    WHEN @selectField2 THEN Field2
    ELSE someDefaultField
END
FROM Table

Is this what you're looking for?

l3a0
A: 

@Justin Balvanz, your code gives me a Syntax error, is there something missing from that, which is understood to be there and I am missing it out?, Thanks

Akay