• t.brown 89142 (9/25/2012)


    Yes dynamic SQL would seem appropriate.

    Basically you create your entire SQL Statement in a string then call EXEC on it.

    e.g.

    DECLARE @SQL Varchar(MAX);

    DECLARE @TODAY Varchar(10);

    SELECT @TODAY = CONVERT( VARCHAR(10),GETDATE(), 120);

    SET @SQL = 'Create table emp_hrs as

    (select count(emp_hours) from Emp_table where emp_name=''BOB'' and func_date=''' + @TODAY + ''')'

    EXEC (@SQL)

    You can then chain all 15 of these together into a single stored procedure.

    Duplicate post - please reply to

    http://www.sqlservercentral.com/Forums/FindPost1363825.aspx

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden