Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. USE [SomeDatabase]
  2.  
  3. BEGIN TRANSACTION
  4. GO
  5. IF ( OBJECT_ID(N'[dbo].[ProcedureToUpdate]') IS NOT NULL )
  6. DROP PROCEDURE [dbo].[ProcedureToUpdate]
  7. GO
  8. IF @@ERROR <> 0
  9. AND @@TRANCOUNT > 0
  10. ROLLBACK TRAN
  11. GO
  12. IF @@TRANCOUNT = 0
  13. BEGIN
  14. --Flag that the procedure hasn't been dropped, begin another transaction
  15. BEGIN TRANSACTION
  16. END
  17. GO
  18.  
  19. PRINT 'Creating procedure'
  20. GO
  21.  
  22. CREATE PROCEDURE [dbo].[ProcedureToUpdate]
  23. AS
  24. SELECT *
  25. FROM [dbo].[SomeTable] st
  26. INNER JOIN [dbo].[AnotherTable] at ON st.PK = at.fk
  27.  
  28. GO
  29.  
  30. IF @@ERROR <> 0
  31. AND @@TRANCOUNT > 0
  32. ROLLBACK TRAN
  33. GO
  34. IF @@TRANCOUNT = 0
  35. BEGIN
  36. --Flag that the procedure hasn't been created, begin another transaction
  37. BEGIN TRANSACTION
  38. END
  39. GO
  40.  
  41. Using con As New SqlConnection(ConnectionString)
  42. con.Open()
  43. Dim cmd As SqlCommand = con.CreateCommand()
  44. Dim transaction As SqlTransaction = con.BeginTransaction("Upgrade")
  45. cmd.Connection = con
  46. cmd.Transaction = transaction
  47. Try
  48. For Each transactionalScript In transactionalScripts
  49. cmd.CommandText = transactionalScript
  50. Dim result = cmd.ExecuteNonQuery()
  51. Next
  52. transaction.Commit()
  53. Catch ex As Exception
  54. Log(String.Format("{0} : Script Failed", DateTime.Now.ToString()))
  55. Log(String.Format("{0} : Reason: {1}", DateTime.Now.ToString(), ex.Message))
  56. transaction.Rollback("Upgrade")
  57. Log(String.Format("{0} : Database rolled back", DateTime.Now.ToString()))
  58. End Try
  59. End Using
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement