I created a DTS package that sends and e-mail just fine.
But I would like it to send the results too in the message.
So I found this T-SQL code but I get an error.
Server: Msg17966, Level 16, State 1, Line 1
SQL Mail session not started
Fantastic !!!!!
There was a comma there and I deleted it messing around with trying to get the Mail going.
Now instead of getting and attachment can I just have the information in the message.
All I need to see is the record count
I also need to see it for 2 tables one is [Copy-PartMaster Lite] the other is [Copy-PartMaster Lite-OLD]
Change @attach_results to 'False' and the query results will appear in the message body. Change the query as follows to return two rows.
@query =
'SELECT TblName=''Copy-PartMaster Lite'',
RowCnt=Count(*)
From [Copy-PartMaster Lite] Union All
SELECT TblName=''Copy-PartMaster Lite-OLD'',
RowCnt=Count(*)
From [Copy-PartMaster Lite-OLD]', Terry L. Broadbent - DBA
Computing Links:
Server: Msg 170, Level 15, State 1, Line 5
Line 5: Incorrect syntax near ' '.
-------------- here is my code -----------------
EXEC xp_startmail @user = 'Administrator', @password = 'rfrlhtn4s'
EXEC xp_sendmail @recipients = 'douglasp',
@query =
'SELECT TblName=''Copy-PartMaster Lite'',
RowCnt=Count(*)
From [Copy-PartMaster Lite] Union All
SELECT TblName=''Copy-PartMaster Lite-OLD'',
RowCnt=Count(*)
From [Copy-PartMaster Lite-OLD]',
@subject = 'SQL Server Report',
@message = 'The contents of INFORMATION_SCHEMA.TABLES:',
@attach_results = 'FALSE', @width = 250
I thought about this when posting the last answer but opted to be lazy. Declare a variable for the query statement. Load the variable and pass the variable contents to xp_sendmail.
Cose as follows ---------------
EXEC xp_startmail @user = 'Administrator', @password = 'rfrlhtn4s'
Declare @sql varchar(1000)
Select @sql= 'SELECT TblName=''Copy-PartMaster Lite'' RowCnt=Count(*) From [Copy-PartMaster Lite] Union All SELECT TblName=''Copy-PartMaster Lite-OLD'' RowCnt=Count(*) From [Copy-PartMaster Lite-OLD] ,
EXEC xp_sendmail @recipients = 'douglasp',
@query =@sql
@subject = 'SQL Server Report',
@message = 'The contents of INFORMATION_SCHEMA.TABLES:',
@attach_results = 'FALSE', @width = 250
------------ Error message ---------------------
Server: Msg 170, Level 15, State 1, Line 3
Line 3: Incorrect syntax near ' '.
Server: Msg 105, Level 15, State 1, Line 8
Unclosed quotation mark before the character string ', @width = 250
You removed needed commas and replaced an ending quote with a comma. The statement should be...
Select @sql= 'SELECT TblName=''Copy-PartMaster Lite'', RowCnt=Count(*) From [Copy-PartMaster Lite] Union All SELECT TblName=''Copy-PartMaster Lite-OLD'', RowCnt=Count(*) From [Copy-PartMaster Lite-OLD]'Terry L. Broadbent - DBA
Computing Links:
It does not like anything I do on that line.
After some exploration in Books-on-line I see that they refer to a stored procedure to run any thing other than the Schema stuff listed in the Original paost I had up top.
Like so
EXEC xp_sendmail 'robertk', @query = 'sp_configure'
You don't need to create a stored proc. We send results of queries via xp_sendmail several times each day. You just need to get the syntax corrected.
Here is an actual xp_sendmail statement extracted from a SP that we run weekly via a job. @query1 contains a select statement that is built earlier in the SP.
exec master.dbo.xp_sendmail
@recipients = @Sendto1,
@query = @Query1,
@subject = 'Hours Report',
@message = 'Employees with less than 40 hours',
@attach_results='TRUE',
@width =250,
@copy_recipients= @Sendto2 Terry L. Broadbent - DBA
Computing Links:
@attach_results='TRUE' says to send an attachment. Changing 'TRUE' to 'FALSE' changes the behavior as noted earlier. Terry L. Broadbent - DBA
Computing Links:
Most of the scripts I run were developed in SQL 7. The basic syntax is the same in SQL 2000 as it was in SQL 7. I don't believe that is the problem.
Do you want to mail the SP to me and perhaps I can analyze it offline? Use the HOTMAIL address below. Include TEK-TIPS in the subject line or my filters will toss your message.
tlbroadbentREMOVE@hotmail.com Terry L. Broadbent - DBA
Computing Links:
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.