Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. ParentTable
  2.  
  3. (
  4.  
  5. Id uniqueidentifier DEFAULT (newsequentialid()) not null,
  6.  
  7. Created datetime not null,
  8.  
  9. Creator uniqueidentifier not null,
  10.  
  11. Modifier uniqueidentifier null,
  12.  
  13. Modified datetime null
  14.  
  15. )
  16.  
  17. ChildTable
  18.  
  19. (
  20.  
  21. Id uniqueidentifier DEFAULT (newsequentialid()) not null,
  22.  
  23. ParentTable_Id not null,
  24.  
  25. Created datetime not null,
  26.  
  27. Creator uniqueidentifier not null,
  28.  
  29. Modifier uniqueidentifier null,
  30.  
  31. Modified datetime null
  32. )
  33.  
  34. create Procedure InsertIntoChildTable
  35. AS
  36. BEGIN
  37. DECLARE @countDset int
  38. DECLARE @todaysdate datetime
  39. DECLARE @UserName uniqueidentifier
  40. DECLARE @ParentTable_Id uniqueidentifier
  41. DECLARE @insertIntoChildTable nvarchar(max)
  42. DECLARE @ChildTableName nvarchar(35)
  43.  
  44. SET @ChildTableName = ChildTable
  45. SET @countDset = 6
  46. SET @todaysdate = GETDATE()
  47. SET @UserName = 'e86aacf4-9887-e911-9724-4439c492b2a7'
  48.  
  49. BEGIN TRY
  50. BEGIN TRANSACTION
  51. SET @insertIntoChildTable = 'INSERT INTO ' + @ChildTableName + '
  52. (ParentTable_Id, Created, Creator, Modified, Modifier)
  53. VALUES ( (select max(Id) from ParentTable) , @todaysdate, @UserName ,
  54. NULL, NULL) ' + ' GO ' + @countDset
  55.  
  56. EXECUTE sp_executesql @insertIntoChildTable,N'@ChildTableName
  57. nvarchar(35), @todaysdate datetime, @UserName uniqueidentifier,
  58. @countDset int', @ChildTableName = @ChildTableName, @todaysdate =
  59. @todaysdate, @UserName = @UserName, @countDset = @countDset
  60.  
  61. COMMIT TRANSACTION
  62. END TRY
  63. BEGIN CATCH
  64. PRINT 'Could not insert in the Child table'
  65. ROLLBACK TRANSACTION
  66. RETURN
  67. END CATCH
  68.  
  69. END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement