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:

Professional Opinions
Ask a Question
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

troubleshooting Question

Loops in SQL server code

Avatar of Declan Basile
Declan BasileFlag for United States of America asked on
Microsoft SQL Server
4 Comments1 Solution122 ViewsLast Modified:
Is there a way to create a loop in SQL Server for the repeating code?

ALTER TRIGGER [dbo].[trigUpdateBitValues] 
ON [dbo].[CodeTrackingReqs] FOR INSERT, UPDATE

	DECLARE @CumulativeSame As Integer
	DECLARE @CumulativeDontCare As Integer
	DECLARE @CodeTrackingReqId As Integer
	DECLARE @Attrib As TinyInt

	SET @CodeTrackingReqId = (SELECT Inserted.CodeTrackingReqId FROM INSERTED)
	SET @CumulativeSame = 0
	SET @CumulativeDontCare = 0

	SET @Attrib = (SELECT Inserted.Attrib1 FROM INSERTED)
	If @Attrib = 1
		SET @CumulativeSame = @CumulativeSame + POWER(2,0)
	If @Attrib = 2
		SET @CumulativeDontCare = @CumulativeDontCare + POWER(2,0)
	SET @Attrib = (SELECT Inserted.Attrib2 FROM INSERTED)
	If @Attrib = 1
		SET @CumulativeSame = @CumulativeSame + POWER(2,1)
	If @Attrib = 2
		SET @CumulativeDontCare = @CumulativeDontCare + POWER(2,1)

	SET @Attrib = (SELECT Inserted.Attrib3 FROM INSERTED)
	If @Attrib = 1
		SET @CumulativeSame = @CumulativeSame + POWER(2,2)
	If @Attrib = 2
		SET @CumulativeDontCare = @CumulativeDontCare + POWER(2,2)

	SET @Attrib = (SELECT Inserted.Attrib4 FROM INSERTED)
	If @Attrib = 1
		SET @CumulativeSame = @CumulativeSame + POWER(2,3)
	If @Attrib = 2
		SET @CumulativeDontCare = @CumulativeDontCare + POWER(2,3)

	UPDATE CodeTrackingReqs SET SameMask = @CumulativeSame, DontCareMask = @CumulativeDontCare
		 WHERE CodeTrackingReqId = @CodeTrackingReqId