Solved

Execute TSQL from an activex task in a DTS package

Posted on 2007-04-04
4
347 Views
Last Modified: 2008-01-09
I would like to have a dts package email the number of rows in a certain table.  I do not have sa rights to the server so I am using an activex task to send the email.  How can I get the results on the select into a variable in the activex script?

set @sql = 'SELECT @rowcount = COUNT(site_id) FROM gl_schedule WHERE (post_date = CONVERT(DATETIME, ''2007-04-04 00:00:00'', 102))'
0
Comment
Question by:lovingatx
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
rboyd56 earned 125 total points
ID: 18853702
If you are using xp_sendmail to send the email there is an @query parameter that you can pass the select statement. This will send the results of the query to the email recepient
0
 

Author Comment

by:lovingatx
ID: 18853742
I can't use xp_sendmail so I am sending the email from an active x task in dts.  I need to get the result of a SQL statement into a variable in the active x task
0
 
LVL 23

Assisted Solution

by:Christopher Kile
Christopher Kile earned 125 total points
ID: 18853783
What database technology are you familiar with in Visual Basic?  Personally, I would recommend ActiveX Data Objects (ADO).  Build a connection and a recordset in ADO just as you would in a script (using CreateObject() to get the objects).  If you need any help with this, please let me know.
0
 

Author Comment

by:lovingatx
ID: 18854337
This problem is resolved.  I added a sql task to the dts package, clicked parameters and defined a global vairable.  I referenced it in the active x script and emailed based on its value as detailed below.

'**********************************************************************
'  Visual Basic ActiveX Script
'************************************************************************

Function Main()
Dim url
Dim strbody
Dim iMsg
Dim iConf
Dim Flds
Dim strHTML
Dim rowcount
Dim strHTML2






'Send email to

Const cdoSendUsingPort = 2

Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

Set Flds = iConf.Fields

' Set the CDOSYS configuration fields to use port 25 on the SMTP server.

With Flds
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
    'ToDo: Enter name or IP address of remote SMTP server.
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.1.1.1"
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
    .Update
End With

' Build HTML for message body.
strHTML = "<HTML>"
strHTML = strHTML & "<HEAD>"
strHTML = strHTML & "<BODY><B>"
strHTML = strHTML & "TMR warehouse populated with " & DTSGlobalVariables("ROWCOUNT").Value & " rows"
strHTML = strHTML & "</br>"
strHTML = strHTML & "</B></BODY>"
strHTML = strHTML & "</HTML>"

strHTML2 = "<HTML>"
strHTML2 = strHTML2 & "<HEAD>"
strHTML2 = strHTML2 & "<BODY><B>"
strHTML2 = strHTML2 & "TMR warehouse has no data.  will follow up with a future email."
strHTML2 = strHTML2 & "</br>"
strHTML2 = strHTML2 & "</B></BODY>"
strHTML2 = strHTML2 & "</HTML>"

If DTSGlobalVariables("ROWCOUNT").Value = 0 Then
' Apply the settings to the message.
With iMsg
    Set .Configuration = iConf
   
    .To = "recipient"
    .replyto = "recipient@recipient.com"
    .From = "recipient@recipinet.com" 'ToDo: Enter a valid email address.
    .Subject = "FAILURE - TMR Cognos Dashboard Load"
    .HTMLBody = strHTML2
    .Send
End With

Else

' Apply the settings to the message.
With iMsg
    Set .Configuration = iConf
   
    .To = "recipient"
    .replyto = "recipient"
    .From = "recipient" 'ToDo: Enter a valid email address.
    .Subject = "Populated TMR warehouse with data"
    .HTMLBody = strHTML
    .Send
End With

End If


' Clean up variables.
Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing
      Main = DTSTaskExecResult_Success
End Function

0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now