troubleshooting Question

Transaction or Stored Procedure MSSQL 2012

Avatar of Rob
RobFlag for Australia asked on
DatabasesMicrosoft SQL ServerSQL
4 Comments1 Solution162 ViewsLast Modified:
Should this be a stored procedure? If so, how? if not, can I do this in a transaction that I can rollback if any of them error?

I have 4 commands to run: INSERT then UPDATE then INSERT then UPDATE

I have a feeling this isn't the best way to do this... and I'm trying to use my basic MySQL skills to do this project in MSSQL.

INSERT INTO ProductsLog (ID, logdate)
(SELECT ID, Getdate() FROM InactiveProperties WHERE p_id IS NULL AND (enabled=1))
;
UPDATE Products SET
enabled=0
WHERE ID IN (
SELECT ID FROM InactiveProperties WHERE p_id IS NULL AND (enabled=0)
);
INSERT INTO ProductsLog (PID, logdate)
(SELECT ID, Getdate() FROM InactiveProperties WHERE p_id IS NOT NULL AND (enabled=0))
;
UPDATE Products SET
enabled=1
WHERE ID IN (
SELECT ID FROM InactiveProperties WHERE p_id IS NOT NULL AND (enabled=0)
);
ASKER CERTIFIED SOLUTION
Pavel Celba
All in One

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros