MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 17th, 2008, 06:02 AM
Cameron Taljard Cameron Taljard is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 7 Cameron Taljard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 47 sec
Reputation Power: 0
Update Trigger +where clause

Hi.

I have an issue with my tsql - I want to create a trigger based on change to a certain column change equal a certain field, then a take a field from the updated row and use it to send an email to.

Would any one like help me or make a suggestion on how to do it?

Any Help would be appreciated.

Code:
create trigger tr_Update_NewUpdate on [dbo].[RSLead]  		
	for update
	as
	set nocount on

	declare @rows int
	select @rows = count(*) from inserted

	-- send notification if TestTriggerSMS table is being updated		
	if @rows=1
 
begin	 		
if update(eventtypecode) 			
   if (eventtypecode) = 'UNVERS' or (eventtypecode)= 'VERSYS'	-- notification only if update of a certain field in eventtypecode field 	
		 					 			
Begin
declare @cellnumber varchar(12)  -- length 12 numbers  	

set @cellnumber = (select cellno from tbLead where...) --select the cellno from a column in the row that has been updated 	
			  			
declare @message varchar(160) 			
set @message = 'Something has happen...' + ' ' +  	

EXEC msdb.dbo.sp_send_dbmail ....
...
end 	end 	set nocount off 	

Reply With Quote
  #2  
Old July 17th, 2008, 07:19 AM
Cameron Taljard Cameron Taljard is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 7 Cameron Taljard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 47 sec
Reputation Power: 0
I have revised my code, please point out where I am still going wrong.

Thanks,

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER trigger [tr_NewLeadSMS_Unvers] on [dbo].[lead]
for update
as
set nocount on

declare @rows int
select @rows = count(*) from inserted

-- send notification if TestTriggerSMS table is being updated

if @rows=1 -- notification for all the rows update
begin

declare @eventtypecode varchar(10)
declare @cellnumber varchar(13)

if update(eventtypecode)
begin
SET @eventtypecode = (select eventtypecode from inserted)

if @eventtypecode = 'versys'
begin
SET @cellnumber = (select cellno from inserted)

DECLARE @msg varchar(200)
SET @msg = '1440,' + @cellnumber + ',Thanks for...'

--// CHANGE THE VALUE FOR @recipients
EXEC msdb.dbo.sp_send_dbmail
@recipients = N'bulksms@',
@copy_recipients= N'ctaljard@',
@body = @msg,
@subject = @cellnumber,
@profile_name = 'DBMailProfile'
end
END
set nocount off

Reply With Quote
  #3  
Old July 25th, 2008, 08:46 AM
Cameron Taljard Cameron Taljard is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 7 Cameron Taljard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 54 m 47 sec
Reputation Power: 0
got it, no worries.

No worries, got it eventually.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


-- =============================================
-- Author: <Cameron Taljard>
-- Create date: <17 July 2008,,>
-- Description: <SMS to customer when the new lead is --inserted into the database>
-- =============================================

CREEATTE TRIGGER [tr_sms_versys] ON [dbo].[lead] AFTER INSERT AS

SET NOCOUNT ON

declare @rows int
select @rows = count(*) from inserted

IF @rows=1
BEGIN
IF update(eventtypecode)
BEGIN

DECLARE @eventcode varchar(20)
SET @eventcode = (select eventtypecode from inserted)

IF @eventcode = 'VERSYS'
begin
DECLARE @cellnumber varchar(20)
SET @cellnumber = (select cellno from inserted)

--DECLARE @I INT
--SET @I = CHARINDEX (' ' , @cellnumber )
--SET @cellnumber = SUBSTRING(@cellnumber,1,@i-1) + --LTRIM(SUBSTRING(@cellnumber,@I,len(@cellnumber)))
--PRINT @cellnumber

--DECLARE @II INT
--SET @II = CHARINDEX (' ' , @cellnumber )
--SET @cellnumber = SUBSTRING(@cellnumber,1,@II-1) + --LTRIM(SUBSTRING(@cellnumber,@II,len(@cellnumber)))
--PRINT @cellnumber


SET @cellnumber = '+27' + right(@cellnumber,9)
PRINT @cellnumber

DECLARE @msg varchar(200)
--db_mail message
--SET @msg = '1440,' + @cellnumber + ',Thank you for --choosing.....';

--table mail message
SET @msg = 'Thank you for choosing...'

INSERT [172.23.0.105].mdb.dbo.CA_SMSLogout
(Cellno,messagestring,log_time,notificationStatus)
VALUES('@cellnumber',@msg,getdate(),0)

--// CHANGE THE VALUE FOR @recipients
-- EXEC msdb.dbo.sp_send_dbmail
--@recipients = N'bulksms@...',
--@copy_recipients= N'ctaljard@...',
--@body = @msg,
--@subject = @cellnumber,
--@profile_name = 'DBMailProfile'
end
end
END

Thanks anyway.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Update Trigger +where clause


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT