Walter Fabio Simoni
Walter Fabio Simoni

Reputation: 5727

Dynamic where in a Sql Select Query

i would like to create a sql query like these :

Select* from Table where 
(if picod=1)
{
  dvdt= "xxxx" 
}
(if picod=2)
{
  cddt= "xxxx" 
}
(if picod=3)
{
  bldt= "xxxx" 
}
(if picod=3)
{
  fadt= "xxxx" 
}

I don't know how doing this in SQL .

Anyone could help me please ?

Thanks a lot :)

Upvotes: 1

Views: 126

Answers (3)

msi77
msi77

Reputation: 1632

 SELECT  *
    FROM    Table
    WHERE  'xxxx' = case Picod  
     when 1 then dvdt
     when 2 then cddt
     when 3 then bldt 
     when 4 then fadt 
    end

Upvotes: 0

GarethD
GarethD

Reputation: 69819

Just use OR

SELECT  *
FROM    Table
WHERE   (Picod = 1 AND dvdt = 'xxxx')
OR      (Picod = 2 AND cddt = 'xxxx')
OR      (Picod = 3 AND bldt = 'xxxx')
OR      (Picod = 3 AND fadt = 'xxxx');

Upvotes: 3

kleinohad
kleinohad

Reputation: 5912

Select* from Table 
where (picod=1 and dvdt= 'xxxx') or  (picod=2 and cddt= 'xxxx') or ....... (XXXX) or....

Upvotes: 4

Related Questions