WEB开发网
开发学院数据库MSSQL Server 基于SQL Server 的Exception Handling - PART II 阅读

基于SQL Server 的Exception Handling - PART II

 2008-12-06 10:15:28 来源:WEB开发网   
核心提示: · Create Role:T_ROLES_ICREATE Procedure T_ROLES_I(@role_idvarchar(50),@role_namenvarchar(256))ASDECLARE @error_numberINTDECLARE @error_ser

·     Create Role:T_ROLES_I

CREATE Procedure T_ROLES_I
  (
    @role_id      varchar(50),
    @role_name  nvarchar(256)
  )
AS
  
DECLARE @error_number  INT
DECLARE @error_serverity  INT
DECLARE @error_state    INT
  
BEGIN TRY
  
IF(EXISTS(SELECT * FROM dbo.T_ROLES WHERE LOWERED_ROLE_NAME = LOWER(@ROLE_name) OR [ROLE_ID] = @role_id))
  BEGIN
    RAISERROR (50002,16,1)
  END
  
INSERT INTO dbo.T_ROLES
      ([ROLE_ID]
      ,[ROLE_NAME]
      ,LOWERED_ROLE_NAME)
   VALUES(@ROLE_id, @ROLE_name, LOWER(@ROLE_name))
  
   END TRY
  
BEGIN CATCH
  
  SET @error_number    = ERROR_NUMBER()
  SET @error_serverity  =ERROR_SEVERITY()
  SET @error_state    = ERROR_STATE()
  RAISERROR(@error_number,@error_serverity,@error_state)    
END CATCH

·     Add User in Role:P_USERS_IN_ROLES_I

CREATE Procedure P_USERS_IN_ROLES_I
  (
    @user_name  NVARCHAR(256),
    @role_name  NVARCHAR(256)
  )
AS
  
DECLARE @user_id  VARCHAR(50)
DECLARE @role_id  VARCHAR(50)
  
DECLARE @error_number  INT
DECLARE @error_serverity  INT
DECLARE @error_state    INT
  
BEGIN TRY
  
SELECT @user_id = [USER_ID] FROM dbo.T_USERS WHERE LOWERED_USER_NAME = LOWER(@user_name)
  
IF(@user_id IS NULL)
  BEGIN
    RAISERROR (50003,16,1)
  END
  
SELECT @role_id = [ROLE_ID] FROM dbo.T_ROLES WHERE LOWERED_ROLE_NAME = LOWER(@role_name)
  
IF(@role_id IS NULL)
  BEGIN
    RAISERROR (50004,16,1)
  END
  
IF(EXISTS(SELECT * FROM T_USERS_IN_ROLES WHERE [USER_ID] = @user_id AND ROLE_ID = @role_id))
  BEGIN
    RAISERROR (50005,16,1)
  END
  
INSERT INTO   dbo.T_USERS_IN_ROLES([USER_ID],ROLE_ID) VALUES(@user_id,@role_id)
END TRY
  
BEGIN CATCH
  
  SET @error_number    = ERROR_NUMBER()
  SET @error_serverity  =ERROR_SEVERITY()
  SET @error_state      = ERROR_STATE()
  RAISERROR(@error_number,@error_serverity,@error_state)  
END CATCH

上一页  1 2 3 4 5 6 7 8 9  下一页

Tags:基于 SQL Server

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接