using ssis to create tables dynamically

  • eg i want to output a new table everytime the code is run showing as part of the tablename the date the code was run..

    thanks

  • You will need to use dynamic sql and the getDate() function.

    This should get you started...

    @TABLE_NAME VARCHAR(50),

    @SQL VARCHAR(MAX),

    Set @TABLE_NAME = CONVERT(VarChar(20), GETDATE(), 101)

    /* Generate table */

    SET @SQL = Add create table sql here.

    /* Execute dynamic sql*/

    EXEC(@SQL)

  • really? i can't create a table in ssis? do you know which task i use?? thanks

  • may be there is a reason, but why create table for last run and where do you want to create it?

    ---- [font="Tahoma"]Live and Let Live![/font] ----

  • To run that code you will need a SQL Task in the Control Flow view. You probably should save the table name to a global SSIS variable so you can reference it in other places if you are trying to populate it. I'm still not 100% convinced you need a table for each run. Generally you put a time stamp on a record to determine when it was inserted, but you may have a practical application.

    Cheers,

    Brian

  • Yes, you can definitely create Tables using SSIS. In Data Flow Task, create a OLE DB Destination and for the Data Access Mode, select "Table Name or View Name Variable" or "SQL Command" and use some kind of Variable to set the table name. Never actually bothered using it so I'll leave the finer details to yourself or another poster, but you can definitely do it.

  • Yes, you CAN create a uniquely named table in each run of your SSIS package. You could simply build a create table DDL statement using variables, part of which would be the current date, time or whatever, and run it in an Exec SQL task.

    But that doesn't mean you SHOULD do that. As suggested, the date and time could be part of the row definition so you could limit the domain of queries with a simple where clause.

    Why not create new tables? A few reasons might be:

    -- Variable table names would require any application using the data to build queries dynamically just to find the table name(s). Say you run daily and create a table for each day. What happens when you want to summarize data for a number of days?

    -- The overhead of creating tables and indexes (you do want indexes, right?) isn't necessarily enormous, but if not necessary you'll want to avoid it.

    -- It could be that in the future, someone will want to define a constraint based on this table (a foreign key....), but then they'll see that it's not a table at all but an ephemeral family of tables. Each new table would then require its own FK definition in the dependant table.

  • hi,

    according to beezells answer:

    if i use a sql task to create a new table in a ssis package, how can i save the name of this table to a global variable?

    i'm trying to import serveral excel files. i already created the loop to do this.

    but since this is a job that has to be done every month, i need a new table to every month.

    what i wanted to do was a put a sql task in front of the excel importing loop to create the table and to assign the table name to a variable, so i can use it for the ole db destination.

    anybody an idea how to save the name of the in the sql task created table?

Viewing 8 posts - 1 through 7 (of 7 total)

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