We help IT Professionals succeed at work.
Get Started

SQL Trigger and performance

624 Views
Last Modified: 2012-08-18
I have a table called SMS_log that logs SMS messages. If a message is sent correct the "ok" field is True, if it fails the "ok" field is false.

I have a SQL Trigger that check if the message is sent or not. If its not ("ok" = 0) the message shall be sent again (but from another number / system).

My question is:
Can SQL Triggers like mine (see attached code) cause a performance issue? Sometimes we send about 2500 messages at the same time. Is there something in my code that can be optimized to increase the performance?

ALTER TRIGGER [dbo].[trgErrorHandler]
   ON  [dbo].[SMS_Out_Log] 
   for insert
AS 
BEGIN
	SET NOCOUNT ON

	DECLARE @MsgId varchar(18)
	DECLARE @FromNumber varchar(18)
	DECLARE @Tel varchar(18)
	DECLARE @Msg varchar(160)
	DECLARE @PriceGroup varchar(8)
	DECLARE @CampaignId varchar(8)
	DECLARE @Ok bit
	DECLARE @ErrorCode varchar(50)
	DECLARE @SentWithOperator varchar(8)
	DECLARE @Type varchar(18)
	DECLARE @Description varchar(max)

	SET @Ok = (select ok from INSERTED)

	IF (@Ok = 0)
	BEGIN

		SET @FromNumber = (select fromNumber from INSERTED)
		SET @SentWithOperator = (select SentWithOperator from INSERTED)
		SET @ErrorCode = (select ErrorCode from INSERTED)

		IF (@FromNumber = '5100' AND @SentWithOperator = '4711' AND @ErrorCode = '1030')
		BEGIN

			-- Message info
			SET @MsgId = (select msgID from INSERTED)
			SET @Tel = (select Tel from INSERTED)
			SET @Msg = (select msg from INSERTED)
			SET @PriceGroup = (select PriceGroup from INSERTED)
			SET @CampaignId = (select CampaignID from INSERTED)
			SET @Type = (select Optional2 from INSERTED)
			SELECT @Description = Optional1 from SMS_Out_Log where msgID = @MsgId

			-- Resend message from another number
			Insert into SMS_Out 
			(fromNumber, Tel, msg, PriceGroup, Operator, Optional1, Optional2, CampaignID) 
			values 
			('6300', @Tel, @Msg, @PriceGroup, '0', @Description, @Type, @CampaignId)

		END

	END
END

Open in new window

Comment
Watch Question
Senior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
This problem has been solved!
Unlock 2 Answers and 13 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE