I need help with SQL syntax

Hi experts,
Please let me know if the SQL syntax below is correct (in SQL Server 2008)?  Thanks in advance:

BEGIN TRY
	BEGIN TRANSACTION [TRAN1]
		CASE WHEN @OrderNumberCustomer LIKE 'DS-KM%' OR @OrderNumberCustomer LIKE 'DS-SE%' 
		THEN
			INSERT INTO [INFORPROD].[GPInterface].[WH1].ShippingPackage(OrderNumberCustomer,TrackingNumber,ShippingCost)
			VALUES(@OrderNumberCustomer, @TrackingNumber, 0)
		ELSE
			INSERT INTO [INFORPROD].[GPInterface].[WH1].ShippingPackage(OrderNumberCustomer,TrackingNumber,ShippingCost)
			VALUES(@OrderNumberCustomer, @TrackingNumber, @ShippingCost)							
		END
	COMMIT TRANSACTION [TRAN1]
END TRY

Open in new window

mainrotorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
BEGIN TRY
	BEGIN TRANSACTION 
		IF <your logic> 
                   BEGIN
			INSERT INTO [INFORPROD].[GPInterface].[WH1].ShippingPackage(OrderNumberCustomer,TrackingNumber,ShippingCost)
			VALUES(@OrderNumberCustomer, @TrackingNumber, 0)
                    END
		ELSE
                     BEGIN
			   INSERT INTO [INFORPROD].[GPInterface].[WH1].ShippingPackage(OrderNumberCustomer,TrackingNumber,ShippingCost)
			   VALUES(@OrderNumberCustomer, @TrackingNumber, @ShippingCost)							
		     END
	COMMIT TRANSACTION 
END TRY
BEGIN CATCH
       ROLLBACK TRANSACTION 
END CATCH

Open in new window


It is not quite clear what your logic is at:   IF <your logic>
0
OriNetworksCommented:
You would have to replace the  CASE with IF and make sure you include a CATCH block to handle any errors including rolling back the transaction

BEGIN TRANSACTION [TRAN1]
BEGIN TRY

		IF @OrderNumberCustomer LIKE 'DS-KM%' OR @OrderNumberCustomer LIKE 'DS-SE%' 
		BEGIN
			INSERT INTO [INFORPROD].[GPInterface].[WH1].ShippingPackage(OrderNumberCustomer,TrackingNumber,ShippingCost)
			VALUES(@OrderNumberCustomer, @TrackingNumber, 0)
		END
		ELSE
		BEGIN
			INSERT INTO [INFORPROD].[GPInterface].[WH1].ShippingPackage(OrderNumberCustomer,TrackingNumber,ShippingCost)
			VALUES(@OrderNumberCustomer, @TrackingNumber, @ShippingCost)							
		END
	COMMIT TRANSACTION [TRAN1]
END TRY
BEGIN CATCH
--do something to handle error and rollback transaction
ROLLBACK TRANSACTION [TRAN1]

END CATCH

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vikas GargBusiness Intelligence DeveloperCommented:
Hi,

You can try this one also without any if else condition

BEGIN TRY
	BEGIN TRANSACTION
		INSERT INTO [INFORPROD].[GPInterface].[WH1].ShippingPackage(OrderNumberCustomer,TrackingNumber,ShippingCost)
		VALUES(@OrderNumberCustomer, @TrackingNumber, CASE WHEN @OrderNumberCustomer LIKE 'DS-KM%' OR @OrderNumberCustomer LIKE 'DS-SE%' THEN 0 ELSE @ShippingCost END)
	COMMIT TRANSACTION 
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
END CATCH

Open in new window

0
Vitor MontalvãoMSSQL Senior EngineerCommented:
You only have a single command so being inside a transaction is useless since in case of error the INSERT won't happen anyway. You can use a single command to perform what you want:
INSERT INTO [INFORPROD].[GPInterface].[WH1].ShippingPackage(OrderNumberCustomer,TrackingNumber,ShippingCost)
SELECT @OrderNumberCustomer, @TrackingNumber, CASE 
						WHEN @OrderNumberCustomer LIKE 'DS-KM%' OR @OrderNumberCustomer LIKE 'DS-SE%' THEN 0
						ELSE @ShippingCost
						END

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.