Solved

Execute TSQL from an activex task in a DTS package

Posted on 2007-04-04
4
358 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

809 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