How To? -- DTS, Excel, Send Mail Task

  • Not sure where to post my question, so I applolgize if this is in the wrong forum.

    I have a DTS job that runs a sql query to join 3 tables to pull data needed for an excel spreadsheet and then on completion, the excel spreadsheet is emailed to a user.

    My problem is that this runs multiple times a day and currently, the excel spreadsheet is just appended to.  I need it to NOT append but to create a new excel spreadsheet.  If I manually remove the excel spreadsheet, then DTS fails indicating that a temp table(created by DTS for the excel spreadsheet) does not exist.

    How do I create the spreadsheet from scratch each time the DTS is executed?

    Jeff

  • There are more than one way to skin a cat.  One solution in this case is to use ActiveX, open the Excel spreadsheet and remove all the data at the beginning of the DTS.


    Joseph

  • Create your Excel workbook, format as required, then delete any data rows in it, then store a copy somewhere the DTS can access. Have an ActiveX delete the existing workbook, copy the new empty workbook to the same location then run the DTS task to transfer your data to the new empty workbook. Data goes in and adopts all the formatting applied to the original data...

  • I skin my cat this way - I create a sql task who's connection is the spreadsheet and drop the table and recreate it. eg. drop table err_screwup go create table 'err_screwup' ('user' varchar(30), 'err' decimal (28,8)). Works a treat.

  • Could you give me some more detail on how you do this, wildh? I am currently doing the ActiveX script to copy an empty template over the end excel file, but if I could simplify the process, I would certainly like to!

  • In excel

    Create a spreadsheet called screwup.xls

    In MMC

    Create a SQL server connection

    Create a spreadsheet connection (who’s filename is the screwup.xls file)

    Create the Transform data task, in the source type “select 'user1' as [username], '1000' as err” in the SQL query. In destination click “create and change table name to “screwup”. Note. copy out the statement you will need it later it may look like CREATE TABLE `screwup` (`username` VarChar (5) , `err` VarChar (4) )

    Execute the Transform data task twice and open the spreadsheet. You will see two records.

    Create a SQL task who’s existing connection is the excel spreadsheet. Remember, the sheet “screwup” is treated like a table so you must drop it and re- create it, so type in “drop table screwup go” and now paste your create table syntax. Truncate table won’t work.

    Create an on completion/ success precedence from the SQL task to the SQL server connection.

    Run the package again and open the spreadsheet, there should be only one record.

    Hope this is the kind of thing you’re looking for and that my guide is straightforward.

  • That works like a charm!  Simple and straight-forward.  Thanks for the help.

  • Create a SQL task who’s existing connection is the excel spreadsheet. Remember, the sheet “screwup” is treated like a table so you must drop it and re- create it, so type in “drop table screwup go” and now paste your create table syntax

    I tried the above suggestion of wildh and get an error on the drop table:

    "Cannot drop table 'Screwup', because it does not exist in the system catalog"

    The Excel file was created with the 2 records as described.  What am I missing?

    Terri

    tsanta@lsuhsc.edu



    Terri

    To speak algebraically, Mr. M. is execrable, but Mr. C. is
    (x+1)-ecrable.
    Edgar Allan Poe
    [Discussing fellow writers Cornelius Mathews and William Ellery Channing.]

  • Never mind, the SQL task had the wrong connection.  It works now, if the Excel file exists.  Is there a way to check if the file exists before dropping it?  I tried : IF NOT (OBJECT_ID('Screwup') IS NULL) DROP TABLE Screwup

    but I get an error.  Where can I find the syntax I can use here?



    Terri

    To speak algebraically, Mr. M. is execrable, but Mr. C. is
    (x+1)-ecrable.
    Edgar Allan Poe
    [Discussing fellow writers Cornelius Mathews and William Ellery Channing.]

  • tsanta, sorry for any delay!! have a browse and replace the print statement with other syntax.

    http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=8&messageid=114315#bm116592

    Hop it points you in the right direction.

  • I realize this is a really old thread but I am trying to use the drop/create method described above to clear out an excel sheet before dumping the new data in. I can "DROP TABLE Results", and that will get rid of my data and column headers but not delete the sheet itself. From here I can't put data in because it doesn't see any column headers, but I also cannot "CREATE TABLE Results...." because it sees the sheet with that name (even though it is blank) and tells me that table already exists.

    What am I missing??!

    Thanks in advance for your help.

  • I deal with this by:

    1) having a "template" generically named spreadsheet (c:\template\salesdata.xls),

    2) copy it to a working file (c:\working\salesdata.xls),

    3) populate the data via a transformation,

    4) rename it to the saved version (c:\salesreports\salesdata_FredFlintstone_yyyymmdd.xls), and

    5) then email it (to the sales rep in this case).

    I can loop through a variable set of people and data this way and each person gets their own spreadsheet. However... I have been replacing this with xp_sendmail using the query option so they get a small text email with their data instead of a large spreadsheet.

Viewing 12 posts - 1 through 11 (of 11 total)

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