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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Trigger and emails...

Author  Topic 

bogus
Starting Member

41 Posts

Posted - 2007-02-14 : 13:47:51
I have a trigger that I wrote in SQL Server 2k that determines if a field has changed, if so, it kicks off and sends an email to a group of interested parties.

Here is the trigger:

quote:

Alter TRIGGER [dbo].[proc_trgr_Catalog] ON [dbo].[tblPart]
FOR INSERT, UPDATE
AS

DECLARE
@Idx INT,
@Idx2 INT,
@Upd INT,
@Limit INT,
@Ordinal INT,
@CatNo varchar(20),
@Column varBinary(100),
@ValueBefore VARCHAR(800),
@ValueAfter VARCHAR(800),
@Byte Bigint,
@Field VARCHAR(200),
@x VARCHAR(50),
@Src_Module VARCHAR(50),
@For_Whom VARCHAR(50),
@Dest_Email_Body VARCHAR(8000),
@Dest_Email_Subj VARCHAR(255)

SET @Dest_Email_Body = ''

IF EXISTS(SELECT * from Inserted where Technique_ID = 'G')
BEGIN
IF UPDATE(Cost) OR UPDATE(Status) OR UPDATE(US_List) OR UPDATE(PHE_Part_No) OR UPDATE(Lead_Time_Days) OR UPDATE(Description_1) OR UPDATE(Description_2)
BEGIN

SELECT @Limit = COUNT(1) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'tblPart'

SET @Idx = 1
SET @Byte = 1
SET @ValueBefore = (SELECT CATALOG_NO FROM Deleted)
SET @ValueAfter = (SELECT CATALOG_NO FROM Inserted)
SET @Dest_Email_Body = 'New catalog number ' + @ValueAfter + ' has been added.'
SET @Column = COLUMNS_UPDATED()
IF @ValueBefore = @ValueAfter
BEGIN
SET @Dest_Email_Body = ''
WHILE @Idx < @Limit + 1
BEGIN
SET @Upd = CONVERT(INT, SUBSTRING(@Column,@Idx,1))
IF @Upd > 0
BEGIN
SET @Ordinal = (@Idx - 1) * 8 + @Upd
SELECT @Field = COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'tblPart' AND ORDINAL_POSITION = @Ordinal
SET @Dest_Email_Body = @Dest_Email_Body + ' The value for the field(s), ' + @Field + ', for Catalog Number ' + @ValueBefore + ' Was Updated. '
SET @Field = ''
END
SET @Idx = @Idx + 1
END
END
BEGIN
EXEC master.dbo.xp_startmail
EXEC master.dbo.xp_sendmail
@recipients='group@email.xxx'
,@message=@Dest_Email_Body
,@subject='Changes have been made to the Part Catalog'
EXEC master.dbo.xp_stopmail
END
END
END



Now... when I run this from SQL Query Analyzer, it works swimmingly! Sends out a nice little notice to all interested parties! So exciting!

But as soon as it runs against the VB6 application, it sends a blank email. Nada. Nothing. Sometimes I can manipulate this thing to send a message, but it is most inconsistant. It's also slow as hell; in SQA it takes about 2 seconds to execute, in the VB6 application, it takes ~5 seconds to execute.

The latest development has an error number... -2147467259: Subquery returned more than one value. This is not permitted when the subquery follows =, !=, <, <=, >, >= or when the subquery is used as an expression.

Any thoughts would be greatly appreciated!!!

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-02-14 : 14:49:33
Your trigger is not designed to handle more than one row being updated or deleted.

Sending an email from a trigger is a really bad idea.





CODO ERGO SUM
Go to Top of Page

bogus
Starting Member

41 Posts

Posted - 2007-02-15 : 20:00:00
Sorry for the delay.

This is an online job, with only one person tasked to update the catalog (centralized control).

The problem? The VB6 application executes a gnarly little proc during the save process that does 80k things or something like that...

The solution? I moved the email into the app using EasyMail's SMPT control. Life is good.

Thanks for the input!
Go to Top of Page
   

- Advertisement -