Conditional column SELECT but fixed WHERE clause

  • Hi all,

    I have a select statement fired within a stored procedure which needs to return different columns depending on

    a parameter passed to the sp.

    I know I could build up the SELECT in a variable and the WHERE in a variable, append them together and run with EXEC,

    problem is the WHERE clause is rather complex and contains a lot of single quotes and I'm nervous about putting

    into a variable.

    This simple example will perhaps explain better

    create procedure test (@ExtractType )

    AS

    BEGIN

    declare @slct varchar(200)

    if @ExtractType = ‘All’

    BEGIN

    set @slct = ‘select * from country’

    END

    if @ExtractType = ‘Min’

    BEGIN

    set @slct = ‘select country_code, country from country’

    END

    exec (@slct) where country_code = 'GB'

    END

    Does anyone know if this can be achieved?

    Thanks in advance

    Dave

  • HI there,

    I try not to SET statements

    but this allows you to do this.

    SET QUOTED_IDENTIFIER OFF

    SELECT " tester'tester "

    Also you could look at this:

    sp_executesql

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life

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

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