Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
qutesanju
Posting Yak Master
193 Posts |
Posted - 2010-03-19 : 05:25:02
|
| I have below statements can anybody let me know where should I start and end transcationcurrently for below code it's showing an error asIncorrect syntax near the keyword 'TRAN'.---------------------------------------------------------------------Declare @LastRunDate as varchar(100)set @LastRunDate='2005-12-31'BEGIN TRYBEGIN TRANdelete from [PROD_SUM] where exists ( SELECT * FROM [PROD_SUM] where [KC_PROD_SUM_MES_PRODU_DATE_DT] <= @LastRunDate );END TRANEND TRYBEGIN CATCH IF @@ERROR <> 0 ROLLBACK TRAN -- Raise an error with the details of the exception DECLARE @ErrMsg1 nvarchar(4000), @ErrSeverity1 int, @ErrorState1 INT SELECT @ErrMsg1 = ERROR_MESSAGE(), @ErrSeverity1 = ERROR_SEVERITY(), @ErrorState1 = ERROR_STATE() RAISERROR(@ErrMsg1, @ErrSeverity1, @ErrorState1)END CATCH---------------------------------------------------------------------------BEGIN TRYBEGIN TRANDelete from [PROD_WASTE] where exists(SELECT * FROM [PROD_WASTE] where [KC_PROD_WASTE_MES_PRODU_DATE_D] <= @LastRunDate--getdate())END TRANEND TRY |
|
|
qutesanju
Posting Yak Master
193 Posts |
Posted - 2010-03-19 : 05:27:24
|
| Posting appropriate code againDeclare @LastRunDate as varchar(100)set @LastRunDate='2005-12-31'BEGIN TRYBEGIN TRANdelete from [PROD_SUM] where exists ( SELECT * FROM [PROD_SUM] where [KC_PROD_SUM_MES_PRODU_DATE_DT] <= @LastRunDate );END TRANEND TRYBEGIN CATCH IF @@ERROR <> 0 ROLLBACK TRAN -- Raise an error with the details of the exception DECLARE @ErrMsg1 nvarchar(4000), @ErrSeverity1 int, @ErrorState1 INT SELECT @ErrMsg1 = ERROR_MESSAGE(), @ErrSeverity1 = ERROR_SEVERITY(), @ErrorState1 = ERROR_STATE() RAISERROR(@ErrMsg1, @ErrSeverity1, @ErrorState1)END CATCH---------------------------------------------------------------------------BEGIN TRYBEGIN TRANDelete from [PROD_WASTE] where exists(SELECT * FROM [PROD_WASTE] where [KC_PROD_WASTE_MES_PRODU_DATE_D] <= @LastRunDate--getdate())END TRANEND TRYBEGIN CATCH IF @@ERROR <> 0 ROLLBACK TRAN -- Raise an error with the details of the exception DECLARE @ErrMsg2 nvarchar(4000), @ErrSeverity2 int, @ErrorState2 INT SELECT @ErrMsg2 = ERROR_MESSAGE(), @ErrSeverity2 = ERROR_SEVERITY(), @ErrorState2 = ERROR_STATE() RAISERROR(@ErrMsg2, @ErrSeverity2, @ErrorState2)END CATCH |
 |
|
|
Lumbago
Norsk Yak Master
3271 Posts |
Posted - 2010-03-19 : 08:59:44
|
The proper code for starting implicit transactions is:BEGIN TRAN--> Do your stuff...IF @SomethingWrongHappenedSomewhere = 1 ROLLBACKELSE COMMIT - LumbagoIf the facts don't fit the theory, change the facts. Albert Einstein |
 |
|
|
|
|
|
|
|