Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. +---------------+---------------+----------+
  2. | ColumnName | DataType | Nullable |
  3. +---------------+---------------+----------+
  4. | Id | INT | NOT NULL |
  5. | CreatedDate | DATETIME | NOT NULL |
  6. | ApiMethodName | NVARCHAR(MAX) | NOT NULL |
  7. | Request | NVARCHAR(MAX) | NOT NULL |
  8. | Result | NVARCHAR(MAX) | NOT NULL |
  9. | Method_Id | INT | NOT NULL |
  10. | User_Id | INT | NULL |
  11. +---------------+---------------+----------+
  12.  
  13. +--------------------+----------+----------+
  14. | ColumnName | DataType | Nullable |
  15. +--------------------+----------+----------+
  16. | Id | INT | NOT NULL |
  17. | AuditUserMethod_Id | INT | NOT NULL |
  18. | ErrorCode | INT | NOT NULL |
  19. +--------------------+----------+----------+
  20.  
  21. CREATE PROCEDURE [api].[Audit_V1_GetAuditDetails]
  22. (
  23. @Users XML = NULL,
  24. @Methods XML = NULL,
  25. @ErrorCodes XML = NULL,
  26. @FromDate DATETIME = NULL,
  27. @ToDate DATETIME = NULL,
  28. @PageSize INT = 5,
  29. @PageIndex INT = 0
  30. )
  31. AS
  32. BEGIN
  33.  
  34. DECLARE @UserIds TABLE (Id INT)
  35. DECLARE @MethodNames TABLE (Name NVARCHAR(256))
  36. DECLARE @ErrorCodeIds TABLE (Id INT)
  37.  
  38. DECLARE @FilterUsers BIT = 0
  39. DECLARE @FilterMethods BIT = 0
  40. DECLARE @FilterErrorCodes BIT = 0
  41.  
  42. INSERT @UserIds
  43. SELECT
  44. x.y.value('.', 'int')
  45. FROM @Users.nodes('Ids/x/@i') AS x (y)
  46. INSERT @MethodNames
  47. SELECT
  48. x.y.value('.', 'NVARCHAR(256)')
  49. FROM @Methods.nodes('ArrayOfString/string') AS x (y)
  50. INSERT @ErrorCodeIds
  51. SELECT
  52. x.y.value('.', 'int')
  53. FROM @ErrorCodes.nodes('Ids/x/@i') AS x (y)
  54.  
  55. IF EXISTS (SELECT TOP 1 0 FROM @UserIds)
  56. SET @FilterUsers = 1
  57. IF EXISTS (SELECT TOP 1 0 FROM @MethodNames)
  58. SET @FilterMethods = 1
  59. IF EXISTS (SELECT TOP 1 0 FROM @ErrorCodeIds)
  60. SET @FilterErrorCodes = 1
  61.  
  62. DECLARE @StartRow INT = @PageIndex * @Pagesize
  63.  
  64. DECLARE @PageDataResults TABLE
  65. (
  66. Id INT,
  67. CreatedDate DATETIME,
  68. ApiMethodName NVARCHAR(256),
  69. Request NVARCHAR(MAX),
  70. Result NVARCHAR(MAX),
  71. MethodId INT,
  72. UserId INT,
  73. TotalRows INT
  74. );
  75.  
  76. WITH PageData AS
  77. (
  78. SELECT
  79. id AS id
  80. ,createddate AS createddate
  81. ,apimethodname AS apimethodname
  82. ,request AS request
  83. ,result AS result
  84. ,method_id AS method_id
  85. ,user_id AS user_id
  86. ,ROW_NUMBER() OVER (ORDER BY createddate DESC, id DESC) AS row_number
  87. ,COUNT(*) OVER() as TotalRows
  88. FROM dbo.AuditUserMethods AS aum
  89. WHERE (@FromDate IS NULL OR (@FromDate IS NOT NULL AND aum.createddate > @FromDate))
  90. AND (@ToDate IS NULL OR (@ToDate IS NOT NULL AND aum.createddate < @ToDate))
  91. AND (@FilterUsers = 0 OR (@FilterUsers = 1 AND aum.user_id IN (SELECT Id FROM @UserIds)))
  92. AND (@FilterMethods = 0 OR (@FilterMethods = 1 AND aum.ApiMethodName IN (SELECT Name FROM @MethodNames)))
  93. AND
  94. (
  95. @FiltererRorCodes = 0 OR
  96. (
  97. @FiltererRorCodes = 1 AND EXISTS
  98. (
  99. SELECT 1
  100. FROM AuditUserMethodErrorCodes e
  101. WHERE e.AuditUserMethod_Id = aum.Id
  102. AND e.ErrorCode IN (SELECT Id FROM @ErrorCodeIds)
  103. )
  104. )
  105. )
  106. )
  107. INSERT @PageDataResults
  108. SELECT TOP (@Pagesize)
  109. PageData.id AS id
  110. ,PageData.createddate AS createddate
  111. ,PageData.apimethodname AS apimethodname
  112. ,PageData.request AS request
  113. ,PageData.result AS result
  114. ,PageData.method_id AS method_id
  115. ,PageData.user_id AS user_id
  116. ,PageData.TotalRows AS totalrows
  117. FROM PageData
  118. WHERE PageData.row_number > @StartRow
  119. ORDER BY PageData.createddate DESC
  120.  
  121. SELECT Id, CreatedDate, ApiMethodName, Request, Result, MethodId, UserId
  122. FROM @PageDataResults
  123.  
  124. SELECT aumec.AuditUserMethod_Id, aumec.ErrorCode
  125. FROM @PageDataResults ps
  126. INNER JOIN AuditUserMethodErrorCodes aumec ON ps.Id = aumec.AuditUserMethod_Id
  127.  
  128. SELECT TOP 1 TotalRows NumberOfReturnedAuditEntries FROM @PageDataResults
  129.  
  130. END
  131.  
  132. EXEC [api].[Audit_V1_GetAuditDetails] @Users = N'<Ids><x i="1" /></Ids>'
  133. ,@Methods = NULL
  134. ,@ErrorCodes = N'<Ids />'
  135. ,@FromDate = '2015-02-15 07:18:59.613'
  136. ,@ToDate = '2015-07-02 08:18:59.613'
  137. ,@Pagesize = 5000
  138. ,@PageIndex = 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement