Solved

Email from Insert Trigger-SQL

Posted on 2008-06-26
8
1,077 Views
Last Modified: 2012-05-05
I am looking to send an email everytime a record is inserted into a table, i have a attached the code i have in the trigger,

and below is the message i am getting when i attemp to insert into the table:

Msg 22050, Level 16, State 1, Line 0
Error formatting query, probably invalid parameters
Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 478
Query execution failed: Msg 208, Level 16, State 1, Server LINE500V7\SAGE, Line 2
Invalid object name 'inserted'.
Msg 22050, Level 16, State 1, Line 0
Error formatting query, probably invalid parameters
Msg 14661, Level 16, State 1, Procedure sp_send_dbmail, Line 478
Query execution failed: Msg 208, Level 16, State 1, Server LINE500V7\SAGE, Line 2
Invalid object name 'inserted'.
ALTER TRIGGER [scheme].[email_insert] 

   ON  [scheme].[opdetm] 

   AFTER INSERT

AS 

BEGIN

	-- SET NOCOUNT ON added to prevent extra result sets from

	-- interfering with SELECT statements.

	SET NOCOUNT ON;
 
 

EXEC msdb.dbo.sp_send_dbmail 

@profile_name = '1',

@recipients = 'eoin.conway@meteorelectrical.com',

@subject = 'subject',

@body = 'body',

@query = 

'SELECT order_no FROM INSERTED with (NOLOCK)',

@execute_query_database = 'demo'

END

Open in new window

0
Comment
Question by:meteorelec
  • 4
  • 4
8 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21873267
the "inserted" is not available in the sp_send_dbmail procedure context.

you will have to make it like this:
ALTER TRIGGER [scheme].[email_insert] 
   ON  [scheme].[opdetm] 
   AFTER INSERT
AS 
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON; 
DECLARE @sql VARCHAR(1000)
SET @sql = 'SELECT ' + cast(order_no as varchar(100)) + ' order_no ' from inserted
 
EXEC msdb.dbo.sp_send_dbmail 
@profile_name = '1',
@recipients = 'eoin.conway@meteorelectrical.com',
@subject = 'subject',
@body = 'body',
@query = @sql,
@execute_query_database = 'demo'
END 

however, that will only send 1 of the values of order_no in case several orders are inserted at once.

Open in new window

0
 
LVL 2

Author Comment

by:meteorelec
ID: 21873337
Msg 156, Level 15, State 1, Procedure email_insert, Line 16
Incorrect syntax near the keyword 'from',

thats what i'm getting when i try to modify the trigger to that.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21873361
sorry...
ALTER TRIGGER [scheme].[email_insert] 

   ON  [scheme].[opdetm] 

   AFTER INSERT

AS 

BEGIN

      -- SET NOCOUNT ON added to prevent extra result sets from

      -- interfering with SELECT statements.

      SET NOCOUNT ON; 

DECLARE @sql VARCHAR(1000)

SELECT @sql = 'SELECT ' + cast(order_no as varchar(100)) + ' order_no ' 

from inserted

 

EXEC msdb.dbo.sp_send_dbmail 

@profile_name = '1',

@recipients = 'eoin.conway@meteorelectrical.com',

@subject = 'subject',

@body = 'body',

@query = @sql,

@execute_query_database = 'demo'

END 

Open in new window

0
 
LVL 2

Author Comment

by:meteorelec
ID: 21873746
how would i add the order_line_no, i tryed to

@query = @OrderNo,@OrderLineNo,

but that didn't work,
declare @OrderNo CHAR(10)

declare @OrderLineNo CHAR(5)
 

select @OrderNo = [order_no] from inserted with (NOLOCK)

select @OrderLineNo = [order_line_no] from inserted with (NOLOCK)

 

Begin
 

EXEC msdb.dbo.sp_send_dbmail 

@profile_name = '1',

@recipients = 'eoin.conway@meteorelectrical.com',

@subject = 'subject',

@body = 'body',

@query = @OrderNo,

@execute_query_database = 'demo'
 

	End

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 21873793
here we go:
ALTER TRIGGER [scheme].[email_insert] 

   ON  [scheme].[opdetm] 

   AFTER INSERT

AS 

BEGIN

      -- SET NOCOUNT ON added to prevent extra result sets from

      -- interfering with SELECT statements.

      SET NOCOUNT ON; 

DECLARE @sql VARCHAR(1000)

SELECT @sql = 'SELECT ' + cast(order_no as varchar(100)) + ' order_no ,' 

 + cast(order_line_no as varchar(100)) + ' order_line_no '

from inserted

 

EXEC msdb.dbo.sp_send_dbmail 

@profile_name = '1',

@recipients = 'eoin.conway@meteorelectrical.com',

@subject = 'subject',

@body = 'body',

@query = @sql,

@execute_query_database = 'demo'

END 

Open in new window

0
 
LVL 2

Author Comment

by:meteorelec
ID: 21874196
I am trying to add a value using a join, but it doesn't seem to be working...

remove the below line it works fine,

+ cast(d.product as varchar(100)) + ' product '
   AFTER INSERT

AS 

BEGIN

	-- SET NOCOUNT ON added to prevent extra result sets from

	-- interfering with SELECT statements.

	SET NOCOUNT ON;
 

DECLARE @Cost FLOAT

DECLARE @sql VARCHAR(1000)
 

SELECT @Cost = [cost]from inserted with (NOLOCK)

SELECT @sql = 

'SELECT ' 

+ cast(i.order_no as varchar(100)) + ' OrderNo ,' 

+ cast(i.order_line_no as varchar(100)) + ' OrderLineNo ,'

+ cast(d.product as varchar(100)) + ' product '

from inserted i

JOIN scheme.opdetm d

ON i.order_no = d.order_no

AND i.order_line_no = d.order_line_no
 

IF @Cost=0
 

BEGIN 
 

EXEC msdb.dbo.sp_send_dbmail 

@profile_name = '1',

@recipients = 'eoin.conway@meteorelectrical.com',

@subject = 'subject',

@body = 'body',

@query = @sql,

@execute_query_database = 'demo'
 

END

END

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21874343
is the product a varchar field?

if yes, change that line to:
+  '''' + replace(d.product , '''', '''''') + ''' product '

Open in new window

0
 
LVL 2

Author Comment

by:meteorelec
ID: 21874474
Brilliant,

thats sorted, thanks a million
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now