Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
jose1lm
Yak Posting Veteran
70 Posts |
Posted - 2007-11-07 : 19:01:27
|
This procedure works fine if the body is not larger than 4000 chars, otherwisethe rest gets truncated. I was thinking that maybe some kind of loop can be placedin this code that will execute the CDONTS mail every 25th record. Example, if theuser has 52 records, the user will see a total of three emails. Does this makeany sense? Unless there's a nother way to fix the problem I'm having.In my sample data, pin 1 should see a total of 5 records. For testing purposes,how can I split this so the user only gets 2 records per email (The user should see a total of 3 emails).Tablesif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[iso_employees]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[iso_employees]GOCREATE TABLE [dbo].[iso_employees] ( [pin] [int] NOT NULL , [lname] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [fname] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [email] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,) ON [PRIMARY]GOINSERT iso_employees Values (1, 'Doe', 'Jon', 'abc@abc.com')INSERT iso_employees Values (2, 'Doe', 'Jane', 'cba@abc.com')GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[iso_employwi]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[iso_employwi]GOCREATE TABLE [dbo].[iso_employwi] ( [empWiID] [numeric](18, 0) IDENTITY (1, 1) NOT NULL , [pin] [int] NOT NULL , [wi] [nvarchar] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [wi_title] [nvarchar] (250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [rev] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [level_trained] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,) ON [PRIMARY]GOINSERT iso_employwi Values (1, 'Test1', 'Title one bla bla bla', 'b', 'a')INSERT iso_employwi Values (1, 'Test2', 'Title two bla bla bla', 'b', 'b')INSERT iso_employwi Values (1, 'Test3', 'Title three bla bla bla', 'c', 'b')INSERT iso_employwi Values (1, 'Test4', 'Title four bla bla bla', 'b', 'a')INSERT iso_employwi Values (1, 'Test5', 'Title five bla bla bla', 'b', 'a')INSERT iso_employwi Values (1, 'Zest6', 'Title six bla bla bla', 'b', 'a')INSERT iso_employwi Values (2, 'Test1', 'Title one bla bla bla', 'b', 'a') Stored Procedure-- example: exec sp_isowi_cdontsmail '1'CREATE PROCEDURE [dbo].[sp_isowi_cdontsmail] @Pin varchar(15)ASSET nocount ONDeclare @MailID intDeclare @hr intDeclare @To varchar(100)Declare @CC varchar(100)Declare @BCC varchar(100)Declare @From varchar(100)Declare @Subject varchar(100)Declare @Body varchar(8000)Declare @message varchar(8000)Declare @link varchar(100)Declare @empname nvarchar(255)SET @From = 'Training_Matrix@xxxxx.com'SET @CC = ''SET @link = 'http://xxxx.xxxxx.com/ISOWEB/pdf/'SET @Subject = 'WI-Updated'SELECT @empname = COALESCE(lname, '') + ' ' + COALESCE(fname, '')FROM dbo.iso_employeesWHERE (dbo.iso_employees.pin = @pin)SELECT @message = COALESCE(@message + '', '') + ' ' + dbo.iso_employwi.wi + ' ' + dbo.iso_employwi.wi_title + char(10) + ' ' + @link + dbo.iso_employwi.wi + '.pdf' + char(10) + char(10)FROM dbo.iso_employwi INNER JOIN dbo.iso_employees ON dbo.iso_employwi.pin = dbo.iso_employees.pinWHERE (dbo.iso_employwi.level_trained <> dbo.iso_employwi.rev) AND (dbo.iso_employwi.level_trained <> N'No') AND (dbo.iso_employees.email IS NOT NULL) AND (dbo.iso_employwi.pin = @pin) AND (dbo.iso_employwi.wi NOT LIKE N'Z%') OR (dbo.iso_employwi.level_trained IS NULL) AND (dbo.iso_employees.email IS NOT NULL) AND (dbo.iso_employwi.pin = @pin) AND (dbo.iso_employwi.wi NOT LIKE N'Z%')ORDER BY dbo.iso_employwi.wiSELECT @message = COALESCE(@message + '', '') + ' ' + dbo.iso_employwi.wi + ' ' + dbo.iso_employwi.wi_title + char(10) + char(10)FROM dbo.iso_employwi INNER JOINdbo.iso_employees ON dbo.iso_employwi.pin = dbo.iso_employees.pinWHERE (dbo.iso_employwi.level_trained <> dbo.iso_employwi.rev) AND (dbo.iso_employwi.level_trained <> N'No') AND (dbo.iso_employees.email IS NOT NULL) AND (dbo.iso_employwi.pin = @pin) AND (dbo.iso_employwi.wi LIKE N'Z%') OR(dbo.iso_employwi.level_trained IS NULL) AND (dbo.iso_employees.email IS NOT NULL) AND(dbo.iso_employwi.pin = @pin) AND (dbo.iso_employwi.wi LIKE N'Z%')ORDER BY dbo.iso_employwi.wiset @message = 'Work Instruction Updates For ' + @empname + ' - Pin [' + @pin + '] :' + char(10) + CHAR(10) + @message + CHAR(10) + 'Please review the listed Work Instruction(s) then update your status at:' + Char(10) + 'http://xxxx.xxxxx.com/isomatrix/' + Char(10) + Char(10) +'If you have any questions on courses starting with a "Z", please notify HR' + Char(10) + Char(10) + '--------------------------------------------------------------------------' + Char(10) + '** PLEASE DO NOT REPLY TO THIS E-MAIL **' + Char(10) + 'This email may contain confidential and proprietary material for the sole' + Char(10) + 'use of the intended recipient.' + Char(10) + 'If you are not the intended recipient please contact Kathy and' + Char(10) + 'delete all copies.' + Char(10) + '--------------------------------------------------------------------------'SELECT @messageSET @body = @messageSET @To = 'testAccount@xxx.com'--SELECT @To = email--FROM dbo.iso_employees--WHERE @pin = pinDeclare @resultcode intEXEC @resultcode = sp_OACreate 'CDONTS.Newmail', @MailID OUTif @resultcode = 0 BEGIN EXEC @hr = sp_OACreate 'CDONTS.NewMail', @MailID OUT EXEC @hr = sp_OASetProperty @MailID, 'From',@From EXEC @hr = sp_OASetProperty @MailID, 'Body', @message EXEC @hr = sp_OASetProperty @MailID, 'BCC',@BCC EXEC @hr = sp_OASetProperty @MailID, 'CC', @CC EXEC @hr = sp_OASetProperty @MailID, 'Subject', @Subject EXEC @hr = sp_OASetProperty @MailID, 'To', @To EXEC @hr = sp_OAMethod @MailID, 'Send', NULL EXEC @hr = sp_OADestroy @MailID ENDSET nocount offGO Thanks, Jose |
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-11-07 : 20:43:17
|
Do you intend to select @Message = twice? If you are appending the string twice, that would be a problem as it would certainly truncate once it exceed 8K.I copied and executed the code and sample procedure and the length of the message coming out is only 1159 characters. Below is right from my QA,although I removed the redundant code which appears to duplicate a sequence.quote: if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[iso_employees]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[iso_employees]GOSet Nocount on goCREATE TABLE [dbo].[iso_employees] ( [pin] [int] NOT NULL , [lname] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [fname] [nvarchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [email] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,) ON [PRIMARY]GOINSERT iso_employees Values (1, 'Doe', 'Jon', 'abc@abc.com')INSERT iso_employees Values (2, 'Doe', 'Jane', 'cba@abc.com')GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[iso_employwi]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[iso_employwi]GOCREATE TABLE [dbo].[iso_employwi] ( [empWiID] [numeric](18, 0) IDENTITY (1, 1) NOT NULL , [pin] [int] NOT NULL , [wi] [nvarchar] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [wi_title] [nvarchar] (250) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [rev] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [level_trained] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,) ON [PRIMARY]GOINSERT iso_employwi Values (1, 'Test1', 'Title one bla bla bla', 'b', 'a')INSERT iso_employwi Values (1, 'Test2', 'Title two bla bla bla', 'b', 'b')INSERT iso_employwi Values (1, 'Test3', 'Title three bla bla bla', 'c', 'b')INSERT iso_employwi Values (1, 'Test4', 'Title four bla bla bla', 'b', 'a')INSERT iso_employwi Values (1, 'Test5', 'Title five bla bla bla', 'b', 'a')INSERT iso_employwi Values (1, 'Zest6', 'Title six bla bla bla', 'b', 'a')INSERT iso_employwi Values (2, 'Test1', 'Title one bla bla bla', 'b', 'a')--Stored ProcedureGO-- example: exec sp_isowi_cdontsmail '1'ALTER PROCEDURE [dbo].[sp_isowi_cdontsmail] @Pin varchar(15)ASSET nocount ONDeclare @MailID intDeclare @hr intDeclare @To varchar(100)Declare @CC varchar(100)Declare @BCC varchar(100)Declare @From varchar(100)Declare @Subject varchar(100)Declare @Body varchar(8000)Declare @message varchar(8000)Declare @link varchar(100)Declare @empname nvarchar(255)SET @From = 'Training_Matrix@xxxxx.com'SET @CC = ''SET @link = 'http://xxxx.xxxxx.com/ISOWEB/pdf/'SET @Subject = 'WI-Updated'SELECT @empname = COALESCE(lname, '') + ' ' + COALESCE(fname, '')FROM dbo.iso_employeesWHERE (dbo.iso_employees.pin = @pin)SELECT @message = COALESCE(@message + '', '') + ' ' + dbo.iso_employwi.wi + ' ' + dbo.iso_employwi.wi_title + char(10) + ' ' + @link + dbo.iso_employwi.wi + '.pdf' + char(10) + char(10)FROM dbo.iso_employwi INNER JOIN dbo.iso_employees ON dbo.iso_employwi.pin = dbo.iso_employees.pinWHERE (dbo.iso_employwi.level_trained <> dbo.iso_employwi.rev) AND (dbo.iso_employwi.level_trained <> N'No') AND (dbo.iso_employees.email IS NOT NULL) AND (dbo.iso_employwi.pin = @pin) AND (dbo.iso_employwi.wi NOT LIKE N'Z%') OR (dbo.iso_employwi.level_trained IS NULL) AND (dbo.iso_employees.email IS NOT NULL) AND (dbo.iso_employwi.pin = @pin) AND (dbo.iso_employwi.wi NOT LIKE N'Z%')ORDER BY dbo.iso_employwi.wiSELECT @message = COALESCE(@message + '', '') + ' ' + dbo.iso_employwi.wi + ' ' + dbo.iso_employwi.wi_title + char(10) + char(10)FROM dbo.iso_employwi INNER JOINdbo.iso_employees ON dbo.iso_employwi.pin = dbo.iso_employees.pinset @message = 'Work Instruction Updates For ' + @empname + ' - Pin [' + @pin + '] :' + char(10) + CHAR(10) + @message + CHAR(10) + 'Please review the listed Work Instruction(s) then update your status at:' + Char(10) + 'http://xxxx.xxxxx.com/isomatrix/' + Char(10) + Char(10) +'If you have any questions on courses starting with a "Z", please notify HR' + Char(10) + Char(10) + '--------------------------------------------------------------------------' + Char(10) + '** PLEASE DO NOT REPLY TO THIS E-MAIL **' + Char(10) + 'This email may contain confidential and proprietary material for the sole' + Char(10) + 'use of the intended recipient.' + Char(10) + 'If you are not the intended recipient please contact Kathy and' + Char(10) + 'delete all copies.' + Char(10) + '--------------------------------------------------------------------------'Print: len(@message)Print: (@message)GOexec sp_isowi_cdontsmail '1'-- SET @body = @message-- -- SET @To = 'testAccount@xxx.com'-- --SELECT @To = email-- --FROM dbo.iso_employees-- --WHERE @pin = pin-- -- Declare @resultcode int-- EXEC @resultcode = sp_OACreate 'CDONTS.Newmail', @MailID OUT-- if @resultcode = 0-- BEGIN-- EXEC @hr = sp_OACreate 'CDONTS.NewMail', @MailID OUT-- EXEC @hr = sp_OASetProperty @MailID, 'From',@From-- EXEC @hr = sp_OASetProperty @MailID, 'Body', @message-- EXEC @hr = sp_OASetProperty @MailID, 'BCC',@BCC-- EXEC @hr = sp_OASetProperty @MailID, 'CC', @CC-- EXEC @hr = sp_OASetProperty @MailID, 'Subject', @Subject-- EXEC @hr = sp_OASetProperty @MailID, 'To', @To-- EXEC @hr = sp_OAMethod @MailID, 'Send', NULL-- EXEC @hr = sp_OADestroy @MailID-- END-- SET nocount off-- GO
produces the below output:quote: 1159Work Instruction Updates For Doe Jon - Pin [1] : Test1 Title one bla bla bla http://xxxx.xxxxx.com/ISOWEB/pdf/Test1.pdf Test3 Title three bla bla bla http://xxxx.xxxxx.com/ISOWEB/pdf/Test3.pdf Test4 Title four bla bla bla http://xxxx.xxxxx.com/ISOWEB/pdf/Test4.pdf Test5 Title five bla bla bla http://xxxx.xxxxx.com/ISOWEB/pdf/Test5.pdf Test1 Title one bla bla bla Test2 Title two bla bla bla Test3 Title three bla bla bla Test4 Title four bla bla bla Test5 Title five bla bla bla Zest6 Title six bla bla bla Test1 Title one bla bla blaPlease review the listed Work Instruction(s) then update your status at:http://xxxx.xxxxx.com/isomatrix/If you have any questions on courses starting with a "Z", please notify HR--------------------------------------------------------------------------** PLEASE DO NOT REPLY TO THIS E-MAIL **This email may contain confidential and proprietary material for the soleuse of the intended recipient.If you are not the intended recipient please contact Kathy anddelete all copies.--------------------------------------------------------------------------
as output the number at the top is the len(@message) |
 |
|
jose1lm
Yak Posting Veteran
70 Posts |
Posted - 2007-11-08 : 11:09:04
|
Thx dataguru1971 but I have the 'SELECT @message' twice because they are indeed different in the WHERE clause statement. One has [dbo.iso_employwi.wi LIKE N'Z%'] and the other [dbo.iso_employwi.wi NOT LIKE N'Z%'].Also, I only provided a bit of data for testing purposes to see if someone can help me with some code on how to split these results up as I mentioned above. When I actually run this procedure at work, the data can reach of up to 5k - 6k. |
 |
|
jose1lm
Yak Posting Veteran
70 Posts |
Posted - 2007-11-08 : 16:08:09
|
I figured out how to make it work the way I described. However, if theres a better wayto write this code, please let me know.-- example: exec sp_isowi_cdontsmail '1'CREATE PROCEDURE [dbo].[sp_isowi_cdontsmail] @Pin varchar(15)ASSET nocount ONDeclare @MailID intDeclare @hr intDeclare @To varchar(100)Declare @CC varchar(100)Declare @BCC varchar(100)Declare @From varchar(100)Declare @Subject varchar(100)Declare @Body varchar(8000)Declare @message varchar(8000)Declare @link varchar(100)Declare @empname nvarchar(255)SET @From = 'Training_Matrix@xxxxx.com'SET @CC = ''SET @link = 'http://xxxx.xxxxx.com/ISOWEB/pdf/'SET @Subject = 'WI-Updated'SELECT @empname = COALESCE(lname, '') + ' ' + COALESCE(fname, '')FROM dbo.iso_employeesWHERE (dbo.iso_employees.pin = @pin)Declare @RecsPerPage int, @Page int, @MoreRecords intSET @RecsPerPage = '2'SET @Page = '1'SET @MoreRecords = '1'WHILE (@MoreRecords != '0')BEGINCREATE TABLE #TempItems( ID int IDENTITY, wi varchar(8000))INSERT INTO #TempItems (wi)SELECT COALESCE(@message + '', '') + ' ' + dbo.iso_employwi.wi + ' ' + dbo.iso_employwi.wi_title + char(10) + ' ' + @link + dbo.iso_employwi.wi + '.pdf' + char(10) + char(10)FROM dbo.iso_employwi INNER JOINdbo.iso_employees ON dbo.iso_employwi.pin = dbo.iso_employees.pinWHERE (dbo.iso_employwi.level_trained <> dbo.iso_employwi.rev) AND (dbo.iso_employwi.level_trained <> N'No') AND (dbo.iso_employees.email IS NOT NULL) AND (dbo.iso_employwi.pin = @pin) AND (dbo.iso_employwi.wi NOT LIKE N'Z%') OR (dbo.iso_employwi.level_trained IS NULL) AND (dbo.iso_employees.email IS NOT NULL) AND (dbo.iso_employwi.pin = @pin) AND (dbo.iso_employwi.wi NOT LIKE N'Z%')ORDER BY dbo.iso_employwi.wiINSERT INTO #TempItems (wi)SELECT COALESCE(@message + '', '') + ' ' + dbo.iso_employwi.wi + ' ' + dbo.iso_employwi.wi_title + char(10) + char(10)FROM dbo.iso_employwi INNER JOINdbo.iso_employees ON dbo.iso_employwi.pin = dbo.iso_employees.pinWHERE (dbo.iso_employwi.level_trained <> dbo.iso_employwi.rev) AND (dbo.iso_employwi.level_trained <> N'No') AND (dbo.iso_employees.email IS NOT NULL) AND (dbo.iso_employwi.pin = @pin) AND (dbo.iso_employwi.wi LIKE N'Z%') OR(dbo.iso_employwi.level_trained IS NULL) AND (dbo.iso_employees.email IS NOT NULL) AND(dbo.iso_employwi.pin = @pin) AND (dbo.iso_employwi.wi LIKE N'Z%')ORDER BY dbo.iso_employwi.wiDECLARE @FirstRec int, @LastRec intSELECT @FirstRec = (@Page - 1) * @RecsPerPageSELECT @LastRec = (@Page * @RecsPerPage + 1)SET @MoreRecords = (SELECT TOP 1 MoreRecords = ( SELECT COUNT(*) FROM #TempItems TI WHERE TI.ID >= @LastRec ) FROM #TempItemsWHERE ID > @FirstRec AND ID < @LastRec)DECLARE @message2 varchar(8000)SELECT @message2 = COALESCE(@message2, '') + wiFROM #TempItemsWHERE ID > @FirstRec AND ID < @LastRecset @message2 = 'Work Instruction Updates For ' + @empname + ' - Pin [' + @pin + '] :' + char(10) + CHAR(10) + @message2 + CHAR(10) + 'Please review the listed Work Instruction(s) then update your status at:' + Char(10) + 'http://xxxx.xxxxx.com/isomatrix/' + Char(10) + Char(10) +'If you have any questions on courses starting with a "Z", please notify HR' + Char(10) + Char(10) + '--------------------------------------------------------------------------' + Char(10) + '** PLEASE DO NOT REPLY TO THIS E-MAIL **' + Char(10) + 'This email may contain confidential and proprietary material for the sole' + Char(10) + 'use of the intended recipient.' + Char(10) + 'If you are not the intended recipient please contact Kathy and' + Char(10) + 'delete all copies.' + Char(10) + '--------------------------------------------------------------------------'SET @To = 'testAccount@xxx.com'--SELECT @To = email--FROM dbo.iso_employees--WHERE @pin = pinDeclare @resultcode intEXEC @resultcode = sp_OACreate 'CDONTS.Newmail', @MailID OUTif @resultcode = 0BEGINEXEC @hr = sp_OACreate 'CDONTS.NewMail', @MailID OUTEXEC @hr = sp_OASetProperty @MailID, 'From',@FromEXEC @hr = sp_OASetProperty @MailID, 'Body', @message2EXEC @hr = sp_OASetProperty @MailID, 'BCC',@BCCEXEC @hr = sp_OASetProperty @MailID, 'CC', @CCEXEC @hr = sp_OASetProperty @MailID, 'Subject', @SubjectEXEC @hr = sp_OASetProperty @MailID, 'To', @ToEXEC @hr = sp_OAMethod @MailID, 'Send', NULLEXEC @hr = sp_OADestroy @MailIDENDDROP TABLE #TempItemsSET @message = ''SET @message2 = ''SET @Page = @Page + '1'ENDSET nocount offGO Here are the EMAIL sample outputs:EMAIL 1Work Instruction Updates For Doe Jon - Pin [1] : Test1 Title one bla bla bla http://xxxx.xxxxx.com/ISOWEB/pdf/Test1.pdf Test3 Title three bla bla bla http://xxxx.xxxxx.com/ISOWEB/pdf/Test3.pdfPlease review the listed Work Instruction(s) then update your status at:http://xxxx.xxxxx.com/isomatrix/If you have any questions on courses starting with a "Z", please notify HR--------------------------------------------------------------------------** PLEASE DO NOT REPLY TO THIS E-MAIL **This email may contain confidential and proprietary material for the soleuse of the intended recipient.If you are not the intended recipient please contact Kathy anddelete all copies.--------------------------------------------------------------------------Email 2Work Instruction Updates For Doe Jon - Pin [1] : Test4 Title four bla bla bla http://xxxx.xxxxx.com/ISOWEB/pdf/Test4.pdf Test5 Title five bla bla bla http://xxxx.xxxxx.com/ISOWEB/pdf/Test5.pdfPlease review the listed Work Instruction(s) then update your status at:http://xxxx.xxxxx.com/isomatrix/If you have any questions on courses starting with a "Z", please notify HR--------------------------------------------------------------------------** PLEASE DO NOT REPLY TO THIS E-MAIL **This email may contain confidential and proprietary material for the soleuse of the intended recipient.If you are not the intended recipient please contact Kathy anddelete all copies.--------------------------------------------------------------------------Email 3Work Instruction Updates For Doe Jon - Pin [1] : Zest6 Title six bla bla blaPlease review the listed Work Instruction(s) then update your status at:http://xxxx.xxxxx.com/isomatrix/If you have any questions on courses starting with a "Z", please notify HR--------------------------------------------------------------------------** PLEASE DO NOT REPLY TO THIS E-MAIL **This email may contain confidential and proprietary material for the soleuse of the intended recipient.If you are not the intended recipient please contact Kathy anddelete all copies.--------------------------------------------------------------------------Jose |
 |
|
|
|
|
|
|