Query On Variable Columns

  • I have a stored proc that I want to pass a string variable of the columns I want returned. The string variable is of the format "ID, Shift, DateIN, DateOUT" (without the quotes). Stored proc is as below:

    CREATE Procedure WareTATReportFilter

    (

    @FilterFields varchar(1000)

    )

    As

      set nocount on

    SELECT     @ReportFields

    FROM       W_TempReportFilter

    GO

    But it does not give me the columns, it selects the variable string the number of times there are records in the table.

     

    I have been through BOL but can't find any reference or indicator as to how to check the syntax. All assistance is appreciated.

    rgds

    denis

     

     

  • I presume this is what you require, execute this script in northwind database and get back to me if your problem is something other than this.

    CREATE Procedure ProductFilter

    (

    @FilterFields varchar(1000)

    )

    As

    set nocount on

    exec ('SELECT ' + @FilterFields + ' FROM Products')

    GO

    exec ProductFilter 'ProductID,ProductName'

  • http://www.sommarskog.se/dyn-search.html

     

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

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

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