Thursday, March 29, 2012
Combining Text and value in Trigger
I have written a trigger that emails a specified person.
I am trying to include a body of the email which comprises of the stock level and a warning. Pulling out my hair...Help
Code so far in the trigger is :
CREATE TRIGGER Warnings ON [dbo].[tbl_sql_cartridges_kh]
for update
AS
declare @.SL as int
declare @.SS as int
declare @.Msg as nvarchar(100)
set @.SL= (select stock_level from inserted)
set @.SS =(select cartridge_key from inserted)
set @.Msg = 'Print Cartridges Level Warning'
if @.SL < 3
begin
exec sp_send_cdontsmail 'Print-Cartridges','XXX@.XXXX.co.uk','Print Cartridges Level Warning',@.Msg
end
I would like the @.Msg to say something like Cartridge XXX stock level is YYY, where XXX and YYY are taken from the table after update. I can get the values, but cant put them in the MSG string..
Like @.msg & @.SL (SL being Stock Level)
Many Thanks
KenFirst problem you have is that you are treating the virtual tables as if they have only 1 row...inserted may have n rows, so
set @.SL= (select stock_level from inserted)
Would only return the last results...
Second, sending emails from a trigger is very messy. Why not just do it from a stored procedure? If all the code is isolated to sproc calls then you're golden. If you allow dynamic sql from code, then it's a problem...
As for the email, we a notus lotes so we're hosed here...|||This calls a stored procedure.
The trigger will only ever have 1 row as this Sql dbase has adreamweaver front end that only lets a singke line be updated.
I can grab any items that have been updated, I just cant combine them.
I have made sure all constraints are working..
It actually tells you @.SS will be cartridge HP045a for example and @.SL could 1.
I need the @.msg to say something like Cartridge HP045a stock level is now 1.
The Cdonts procedure is effective and uses SMTP and works well..
Wednesday, March 7, 2012
Column size in emails generated using XP sendmail
and have the results emailed to me. However, this is how its showing
up:
Accounting_Year WK_IN_FYEAR Location
GL_Account
Col Data Difference
----- ---- -----------
----
------- ------- -------
2007 49 Test1
500-001
-2587872.0200 -2587872.0200 .0000
2007 49 Test2
500-001
-3344713.5000 -3344713.5000 .0000
2007 49 Test3
500-001
Is there anyway to line them up side by side properly? When i have two
colms selected the format comes out ok. Thanks for all the help
again!
Here is the sp:
CREATE PROCEDURE [dbo].[spEmailVariance]
(
@.SubjectLine as varchar(500),
@.EmailRecipient VARCHAR(100)
)
AS
DECLARE @.strBody varchar(5000)
set @.SubjectLine = 'Weekly Flash Update'
SET @.strBody =
'Select statement'
exec master.dbo.xp_sendmail
@.recipients= 'XX@.XXXX.com',
@.subject= @.SubjectLine,
@.query = @.strbody
RETURN
GOTake a look at the @.width and @.attach_results parameters of xp_sendmail.
HTH,
Plamen Ratchev
http://www.SQLStudio.com|||On Dec 14, 7:52 am, "Plamen Ratchev" <Pla...@.SQLStudio.comwrote:
Quote:
Originally Posted by
Take a look at the @.width and @.attach_results parameters of xp_sendmail.
>
HTH,
>
Plamen Ratchevhttp://www.SQLStudio.com
You are awesome! That works... I didnt use the @.attach_results TRUE
because i was getting a system error. I used @.width set to 170 and
that worked fine.