Generating SQL scripts on the fly

  • Forgive me in advance for the sheer stupidity of this question:

    I've got a T-SQL script that we use to carry out some balancing activities on a monthly basis. When this script is run, it creates a series of work tables in the format <TABLENAME>_<MONTH>.

    Rather than having to Search and Replace for month names, I'd like to automate things a bit and generate tablenames on the fly.

    I've started playing around with sp_executesql to generate the various SELECT INTO queries. Is this the best (and/or only) way to do this in T-SQL?

    John

  • Yes, as far as I know, sp_executesql is the best way to go.

    Alternatively, use EXECUTE (<someString>) to achieve the same sort of thing.

    Just wondering.... do you really have to have tables for each month? How about just one table and either:

    a) queries that are restricted to a particular month

    or

    b) monthly views on the single table

    Cheers,

    - Mark


    Cheers,
    - Mark

  • quote:


    Just wondering.... do you really have to have tables for each month? How about just one table and either:

    a) queries that are restricted to a particular month

    or

    b) monthly views on the single table


    Originally I was going to say 'no' because the raw data we're getting doesn't actually have any sort of date field in it. However, I suppose that I could always insert a datefield when the data is loaded. This would allow either of the two options you suggest.

    Thanks for the tip.

    John

  • Maybe of some relevance.

    See BOL 2000: Using Partitioned Views

    quote:


    The SQL Server query optimizer recognizes that the search condition in this SELECT statement references only rows in the May1998Sales and Jun1998Sales tables, and limits its search to those tables.


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

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