Solved

Execute TSQL from an activex task in a DTS package

Posted on 2007-04-04
4
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SELECT INTO from XML 6 48
SQL Server 2014 Express vs Full - Service Pack question 5 46
Search Text in Views 2 27
Enable TLS 1.2 for SQL 2012 Web Edition 1 23
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

737 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