5
Answers

Syntax to create store proc with transaction commit and roll

Photo of Nepethya Rana

Nepethya Rana

8y
228
1
How do I create store procedure with commit and rollback to delete records that are older than current date ?

Answers (5)

1
Photo of Pradeep Shet
NA 6.3k 2.1m 8y
Hi Gaurav,
Its simple, I am showing delete of records without timestamp.
BEGIN TRAN
DELETE FROM tbl WHERE CAST(datecolumn AS DATE) < CAST(GETDATE() AS DATE);
COMMIT TRAN;
END TRY
BEGIN CATCH
ROLLBACK TRAN;
END CATCH
Hope this is what you are expecting. If this answers your query, dont forget to mark it as answered.
Accepted
1
Photo of Ramesh Palaniappan
NA 13.2k 722.7k 8y
  1. BEGINT1
  2. DELETEFROMTABLEWHERECAST(DateColumnUsedASDATE)<CAST(GETDATE()ASDATE);
  3. COMMITT1
  4. ENDT1
  5. BEGINCATCH
  6. ROLLBACKT1
  7. ENDCATCH
0
Photo of Nepethya Rana
NA 335 20.2k 8y
Thank you all of you. I wish i could accept all of the answers but system allows only one. But you all have helped me. Thank you
0
Photo of Midhun T P
NA 19.7k 281.2k 8y
Hi,
Please check below link -
http://www.infoa2z.com/sqlserver/use-of-begin-commit-transaction-rollback-in-a-stored-procedure-sql-server
0
Photo of Krishna Rajput Singh
NA 5.5k 2m 8y
Thank yiu for writing below links helpful for you....
https://msdn.microsoft.com/en-IN/library/ms188929.aspx
http://www.4guysfromrolla.com/webtech/080305-1.shtml