March 29, 2010 at 5:29 am
the spaces are getting padded automatically somehow when i post !
March 29, 2010 at 5:33 am
🙂
March 29, 2010 at 1:47 pm
RTRIM and LTRIM are your friends in this case.
March 29, 2010 at 3:26 pm
malachyrafferty (3/29/2010)
its ok, iv got it working !thanks all for your help!
i must keep in mind what you say about multiple records tho!
Cool... can you post your final solution please? Thanks.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 30, 2010 at 2:11 am
sure i can 😀
create trigger TestStock on dbo.mal_test After insert as
declare @StockCode varchar(10)
declare @Description varchar(10)
set @StockCode =(select StockCode from Inserted)
set @Description =(select Description from Inserted)
begin
declare @msg varchar(500)
set @msg = 'New Item created with a Product Code"' + rtrim(@StockCode) +'"and a description"' + rtrim(@Description) +'".'
EXEC msdb.dbo.sp_send_dbmail
@recipients = 'email@address.com',
@subject = 'Inventory Delete/Create',
@body = @msg
end
March 30, 2010 at 3:38 pm
malachyrafferty (3/30/2010)
sure i can 😀create trigger TestStock on dbo.mal_test After insert as
declare @StockCode varchar(10)
declare @Description varchar(10)
set @StockCode =(select StockCode from Inserted)
set @Description =(select Description from Inserted)
begin
declare @msg varchar(500)
set @msg = 'New Item created with a Product Code"' + rtrim(@StockCode) +'"and a description"' + rtrim(@Description) +'".'
EXEC msdb.dbo.sp_send_dbmail
@recipients = 'email@address.com',
@subject = 'Inventory Delete/Create',
@body = @msg
end
Heh... that's what I thought. If you ever do have more than one entry in the same insert, you'll be the last to know with the code above.
Oddly enough, it actually takes less code to return all of the rows...
CREATE TRIGGER TestStock ON dbo.mal_test AFTER INSERT
AS
DECLARE @msg VARCHAR(MAX)
SELECT @msg = ISNULL(@msg+CHAR(10),'')
+ 'New Item created with a Product Code "' + RTRIM(StockCode) +'" and a description "' + RTRIM(Description) +'".'
FROM Inserted
EXEC msdb.dbo.sp_send_dbmail
@recipients = 'email@address.com',
@subject = 'Inventory Delete/Create',
@body = @msg
--Jeff Moden
Change is inevitable... Change for the better is not.
March 30, 2010 at 3:46 pm
cheers for that jeff! will come in useful im sure
March 31, 2010 at 8:50 am
Hi jeff
could you tell why this will wait until all updates are notified? i know it does i just dont understand why :w00t:
what is the SELECT @msg = ISNULL purpose?
March 31, 2010 at 1:54 pm
malachyrafferty (3/31/2010)
Hi jeffcould you tell why this will wait until all updates are notified? i know it does i just dont understand why :w00t:
what is the SELECT @msg = ISNULL purpose?
Not sure what you mean about "until all updates are notified". It will include all updates (INSERTS) in the single notification, if that's what you mean.
The ISNULL just keeps the last line from having a linefeed character in it. It's a classic way of doing concatenation.
SQL Server completes the insertion of all rows for a give INSERT statement and then fires the trigger only once for that insert. The trigger I wrote reads all of those rows from the INSERTED table and concatenates them into a single variable with CHAR(10) (a linefeed character) at the end of each "row". Then it sends that whole thing (hundreds of rows if they are inserted in the same INSERT) as a single email.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 31, 2010 at 3:25 pm
thats fine, i understand now!
thanks for getting back to me very much appreciated
March 31, 2010 at 4:43 pm
Thank you for the feedback, as well. It's actually a bit of a rare thing compared to the norm.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 11 posts - 16 through 25 (of 25 total)
You must be logged in to reply to this topic. Login to reply