CASCADED SELECT AND IF

  • I have the following stored procedure:

    CREATE PROCEDURE GetData

    @ID NVARCHAR(100)

    AS

    SELECT Type,

    Year,

    (SELECT English FROM lookupTable WHERE ID = Month),

    R1,

    R2,

    R3,

    R4,

    R5,

    FROM tblData

    WHERE ID = @ID

    GO

    The problem is I need IF statements for all R1, R2, r3, R4, R5

    If they equal 1 then I want to select data from a table, like

    CREATE PROCEDURE GetData

    @ID NVARCHAR(100)

    AS

    SELECT Type,

    Year,

    (SELECT English FROM lookupTable WHERE ID = Month),

    IF R1=1 SELECT English FROM TableR1,

    IF R2=1 SELECT English FROM TableR2,

    IF R3=1 SELECT English FROM TableR3,

    IF R4=1 SELECT English FROM TableR4,

    IF R5=1 SELECT English FROM TableR5,

    FROM tblData

    WHERE ID = @ID

    GO

    How can I do this? Thanks for the help on my previous threads, Mark!

  • Try something like

    Case When R1=1 Then (SELECT Top 1 English FROM TableR1) Else '' End
  • Works like a charm. Thank you.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply