Where clause based on parameters

  • ok this is doing my head in!

    create procedure myproc

    @aValue bigint

    begin

    as

    select aField from tableA

    inner join tableB on tableA.aField = tableB.aField

    where tableB.aField = @aValue

    end

    this works fine..but what i want to be able to do is to not filter on the 'aValue' if it is set to 0. ( so effectively the query becomes..

    select aField from tableA

    inner join tableB on tableA.aField = tableB.aField

    )

    i know i can do this with dynamic sql but i would rather find another way if it is possible, unfortunately my tsql is letting me down.

    any help appreciated!

  • Have you tried:

    create procedure myproc

    @aValue bigint

    begin

    as

    select aField from tableA

    inner join tableB on tableA.aField = tableB.aField

    where tableB.aField = @aValue

    OR @aValue = 0

    end

     

    Give this a go, let me know how it goes! Good luck.



    Ade

    A Freudian Slip is when you say one thing and mean your mother.
    For detail-enriched answers, ask detail-enriched questions...[/url]

  • Ahh yes. thats pointed me in the right direction..cheers.

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

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