using sp_executesql

  • I essentially am using this for Reporting Services.  But we have a table that actually stores SQL statements to grab data for use in a report.  This is a summary report that has about 250 different line items.  I have created the following:

    DECLARE

    @sql nvarchar(4000)

    SET

    @sql=(SELECT AdHocSQL

    FROM

    RptValueTypeMap

    WHERE

    RptValueTypeMap.SectionCd in ('ITEM0010'))

    exec

    sp_executesql @sql

    This works just fine, however there is no column name understandably.  I have tried using the 'AS' statement to create an alias, however this just generates an error.  What would I need to do to return something so this field is named and can be placed on the report?

    Thanks for the information.

  • Can you please provide some of the SQL statements from the table?

    Where did you try to insert the "AS" keyword?

  • For now, it is just one simple select count(*) statement that is being returned from the table:

    SELECT COUNT(*) FROM CADINV

    I am just using that select for test purposes only.

    I have tried using the as right after the EXEC sp_executesql statement and most other places in the SQL. 

  • I got it.  I tried putting the alias in the select statement in the table and it is returning now with the field name alias.

    Thanks for your help.  I am newer and still learning T-SQL.

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

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