Guest User

Untitled

a guest
Jul 19th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 5.79 KB | None | 0 0
  1. DROP FUNCTION [dbo].[udf_ActionsAvalableForUser]
  2. GO
  3.  
  4.  
  5. CREATE FUNCTION [dbo].[udf_ActionsAvalableForUser] (@UserID BIGINT)
  6. RETURNS @asd TABLE (
  7.    ActionID uniqueidentifier      NOT NULL
  8. )
  9. AS
  10. BEGIN
  11.     INSERT INTO @asd (ActionID)
  12.     SELECT [Action_7e82].ActionID
  13.     FROM   [Action] AS [Action_7e82]
  14.     WHERE  [Action_7e82].ActionID IN ((SELECT [Action_69df].ActionID
  15.                                        FROM   [Action] AS [Action_69df]
  16.                                        WHERE  ( (SELECT COUNT(*)
  17.                                                  FROM   [_UserRole] AS [_UserRole_9d12]
  18.                                                  WHERE  ( [_UserRole_9d12].GroupID IS NULL )
  19.                                                         AND ( [_UserRole_9d12].AuditeeID IS NULL )
  20.                                                         AND [_UserRole_9d12].RoleCode IS NOT NULL
  21.                                                         AND ( [_UserRole_9d12].UserID = @UserID )
  22.                                                         AND ( [_UserRole_9d12].RoleCode IS NOT NULL
  23.                                                               AND [_UserRole_9d12].RoleCode IN ( 1 ) )) > 0 )
  24.                                        UNION
  25.                                        SELECT [Action_9a94].ActionID
  26.                                        FROM   [Action] AS [Action_9a94]
  27.                                        WHERE  ( [Action_9a94].CompanyId IN ((SELECT [Company_fed5].CompanyID
  28.                                                                              FROM   [Company] AS [Company_fed5]
  29.                                                                              WHERE  [Company_fed5].CompanyID IN ((SELECT [Company_1533].CompanyID
  30.                                                                                                                   FROM   [Company] AS [Company_1533]
  31.                                                                                                                   WHERE  ( [Company_1533].ParentCompanyID IS NOT NULL
  32.                                                                                                                            AND [Company_1533].ParentCompanyID IN ((SELECT [Users_b545].CompanyID
  33.                                                                                                                                                                    FROM   [Users] AS [Users_b545]
  34.                                                                                                                                                                    WHERE  ( [Users_b545].UserID = @UserID ))) )
  35.                                                                                                                   UNION
  36.                                                                                                                   SELECT [Company_fa24].CompanyID
  37.                                                                                                                   FROM   [Company] AS [Company_fa24]
  38.                                                                                                                   WHERE  [Company_fa24].CompanyID IN ((SELECT [Users_b545].CompanyID
  39.                                                                                                                                                        FROM   [Users] AS [Users_b545]
  40.                                                                                                                                                        WHERE  ( [Users_b545].UserID = @UserID )))))))
  41.                                                 AND ( (SELECT COUNT(*)
  42.                                                        FROM   [_UserRole] AS [_UserRole_ba65]
  43.                                                        WHERE  ( [_UserRole_ba65].GroupID IS NULL )
  44.                                                               AND ( [_UserRole_ba65].AuditeeID IS NULL )
  45.                                                               AND [_UserRole_ba65].RoleCode IS NOT NULL
  46.                                                               AND ( [_UserRole_ba65].UserID = @UserID )
  47.                                                               AND ( [_UserRole_ba65].RoleCode IS NOT NULL
  48.                                                                     AND [_UserRole_ba65].RoleCode IN ( 2 ) )) > 0 ) )
  49.                                        UNION
  50.                                        SELECT [Action_826e].ActionID
  51.                                        FROM   [Action] AS [Action_826e]
  52.                                        WHERE  ( [Action_826e].AuditeeID IS NOT NULL
  53.                                                 AND [Action_826e].AuditeeID IN ((SELECT [_UserRole_d4fc].AuditeeID
  54.                                                                                  FROM   [_UserRole] AS [_UserRole_d4fc]
  55.                                                                                  WHERE  ( [_UserRole_d4fc].RoleCode IS NOT NULL
  56.                                                                                           AND [_UserRole_d4fc].RoleCode IN ( 4, 5, 5.3, 5.5,
  57.                                                                                                                              8, 9, 10, 11,
  58.                                                                                                                              12, 15, 14 ) )
  59.                                                                                         AND ( [_UserRole_d4fc].UserID = @UserID ))) )
  60.                                        UNION
  61.                                        SELECT [Action_c2ed].ActionID
  62.                                        FROM   [Action] AS [Action_c2ed]
  63.                                        WHERE  ( [Action_c2ed].ActionID IN ((SELECT [Action_13d2].ActionID
  64.                                                                             FROM   [Action] AS [Action_13d2]
  65.                                                                             WHERE  ( ( ( (SELECT [Company_f6be].ResponsiblePartyCode
  66.                                                                                           FROM   [Company] AS [Company_f6be]
  67.                                                                                           WHERE  ( [Company_f6be].CompanyID = [Action_13d2].CompanyId )) = 1 )
  68.                                                                                        AND [Action_13d2].ResponsibleUserID IS NOT NULL )
  69.                                                                                      AND ( [Action_13d2].ResponsibleUserID = @UserID ) )
  70.                                                                             UNION
  71.                                                                             SELECT [Action_b1f0].ActionID
  72.                                                                             FROM   [Action] AS [Action_b1f0]
  73.                                                                             WHERE  ( ( ( (SELECT [Company_f747].ResponsiblePartyCode
  74.                                                                                           FROM   [Company] AS [Company_f747]
  75.                                                                                           WHERE  ( [Company_f747].CompanyID = [Action_b1f0].CompanyId )) = 2 )
  76.                                                                                        AND [Action_b1f0].ResponsibleCompanyID IS NOT NULL )
  77.                                                                                      AND [Action_b1f0].ResponsibleCompanyID IN ((SELECT [Company_e8f9].CompanyID
  78.                                                                                                                                  FROM   [Company] AS [Company_e8f9]
  79.                                                                                                                                  WHERE  [Company_e8f9].CompanyID IN ((SELECT [Users_cc1f].CompanyID
  80.                                                                                                                                                                       FROM   [Users] AS [Users_cc1f]
  81.                                                                                                                                                                       WHERE  [Users_cc1f].UserID IN ( @UserID ))))) )))
  82.                                                 AND ( (SELECT COUNT(*)
  83.                                                        FROM   [_UserRole] AS [_UserRole_035c]
  84.                                                        WHERE  ( [_UserRole_035c].GroupID IS NULL )
  85.                                                               AND ( [_UserRole_035c].AuditeeID IS NULL )
  86.                                                               AND [_UserRole_035c].RoleCode IS NOT NULL
  87.                                                               AND ( [_UserRole_035c].UserID = @UserID )
  88.                                                               AND ( [_UserRole_035c].RoleCode IS NOT NULL
  89.                                                                     AND [_UserRole_035c].RoleCode IN ( 3, 4, 5, 5.3,
  90.                                                                                                        5.5, 6, 8, 9,
  91.                                                                                                        10, 11, 12, 15, 14 ) )) > 0 ) )
  92.                                        UNION
  93.                                        SELECT [Action_9500].ActionID
  94.                                        FROM   [Action] AS [Action_9500]
  95.                                        WHERE  ( ( [Action_9500].CreatedByUserID IS NOT NULL
  96.                                                   AND ( [Action_9500].CreatedByUserID = @UserID ) )
  97.                                                 AND ( (SELECT COUNT(*)
  98.                                                        FROM   [_UserRole] AS [_UserRole_1b74]
  99.                                                        WHERE  ( [_UserRole_1b74].GroupID IS NULL )
  100.                                                               AND ( [_UserRole_1b74].AuditeeID IS NULL )
  101.                                                               AND [_UserRole_1b74].RoleCode IS NOT NULL
  102.                                                               AND ( [_UserRole_1b74].UserID = @UserID )
  103.                                                               AND ( [_UserRole_1b74].RoleCode IS NOT NULL
  104.                                                                     AND [_UserRole_1b74].RoleCode IN ( 3, 4, 5, 5.3,
  105.                                                                                                        5.5, 6, 8, 9,
  106.                                                                                                        10, 11, 12, 15, 14 ) )) > 0 ) )))
  107.     RETURN;
  108. END
Add Comment
Please, Sign In to add comment