如何创建存储过程?

时间:2015-04-05 04:34:48

标签: sql sql-server tsql stored-procedures

我正在尝试创建一个存储过程来生成更新语句,以便在超过193个表中使用正确的SSN更新错误输入的SSN,其中SSN列为一整个事务。我希望此存储过程记录错误消息,并在更新失败的情况下回滚输入事务。

我该如何处理?

我对此练习的方法如下所示:如果有一种有效的方法,请告诉我。我需要对我的代码进行质量检查。请帮忙!

CREATE PROCEDURE uspUpdateSSNErrorLog
AS
    SET NOCOUNT ON; 

    CREATE TABLE #UpdateSSNErrorLog
    (
      [ErrorID] [bigint] IDENTITY(1,1) NOT NULL PRIMARY KEY,
      [ErrorNumber] [nvarchar](50) NOT NULL,
      [ErrorDescription] [nvarchar](4000) NULL,
      [ErrorProcedure] [nvarchar](100) NULL,
      [ErrorState] [int] NULL,
      [ErrorSeverity] [int] NULL,
      [ErrorLine] [int] NULL,
      [ErrorTime] [datetime] NULL    
    );

    INSERT INTO #UpdateSSNErrorLog  
    (
         ErrorNumber 
        ,ErrorDescription 
        ,ErrorProcedure 
        ,ErrorState 
        ,ErrorSeverity 
        ,ErrorLine 
        ,ErrorTime 
    )
    VALUES
    (
         ERROR_NUMBER()
        ,ERROR_MESSAGE()
        ,ERROR_PROCEDURE()
        ,ERROR_STATE()
        ,ERROR_SEVERITY()
        ,ERROR_LINE()
        ,GETDATE()  
    );

    SET NOCOUNT OFF;  
    GO

    CREATE PROCEDURE uspUpdateSSN
    AS

    SET NOCOUNT ON;

DECLARE @OldSSN VARCHAR(9);
DECLARE @NewSSN VARCHAR(9);
DECLARE @CMD   VARCHAR( 400);

-- Create the table.
CREATE TABLE #updateSSNlist (
    Updatelist varchar(400)
);

INSERT INTO #updateSSNlist
SELECT 
'UPDATE [' + TABLE_SCHEMA + '].[' + TABLE_NAME + '] SET [' + Column_Name + ']=@NewSSN    WHERE [' + Column_Name + ']=@OldSSN;'
    FROM INFORMATION_SCHEMA.COLUMNS
WHERE Column_Name ='SSN';

-- Declare a cursor.

DECLARE SSN_Update CURSOR FOR
    SELECT *
    FROM #updateSSNlist

OPEN SSN_Update;

FETCH NEXT
FROM SSN_Update
INTO @CMD;

WHILE @@FETCH_STATUS = 0
BEGIN

BEGIN TRAN
BEGIN TRY
    EXEC @CMD;
FETCH NEXT
FROM SSN_Update
INTO @CMD;
COMMIT TRAN
END TRY

BEGIN CATCH
    EXEC uspUpdateSSNErrorLog --To log update errors
    ROLLBACK
END CATCH

CLOSE SSN_Update;
DEALLOCATE SSN_Update;

DROP TABLE  #UpdateSSNErrorLog;

1 个答案:

答案 0 :(得分:0)

我会首先使用表名创建一个物理表,以便您知道它出错的表。其次,我认为您应该立即执行所有更新。 注意:我不知道您的SSN列是INT还是VARCHAR。如果是Varchar,您需要在其周围添加引号。

试试这个:

CREATE TABLE UpdateSSNErrorLog
(
    [ErrorID] [bigint] IDENTITY(1,1) NOT NULL PRIMARY KEY,
    [TableName] VARCHAR(100) NOT NULL,
    [ErrorNumber] [nvarchar](50) NOT NULL,
    [ErrorDescription] [nvarchar](4000) NULL,
    [ErrorProcedure] [nvarchar](100) NULL,
    [ErrorState] [int] NULL,
    [ErrorSeverity] [int] NULL,
    [ErrorLine] [int] NULL,
    [ErrorTime] [datetime] NULL    
);


DECLARE @OldSSN VARCHAR(9) = 1;
DECLARE @NewSSN VARCHAR(9) = 2;
DECLARE @CMD   VARCHAR(MAX);

 SELECT @cmd =  COALESCE(@cmd,'') +
'BEGIN TRY 
 BEGIN TRANSACTION
    UPDATE [' + TABLE_SCHEMA + '].[' + TABLE_NAME + '] SET [' + Column_Name + '] = ' + @NewSSN + '  
    WHERE [' + Column_Name + '] = ' + @OldSSN + '
COMMIT TRANSACTION
END TRY

BEGIN CATCH
ROLLBACK TRANSACTION;
INSERT INTO UpdateSSNErrorLog  
(
     TableName
    ,ErrorNumber 
    ,ErrorDescription 
    ,ErrorProcedure 
    ,ErrorState 
    ,ErrorSeverity 
    ,ErrorLine 
    ,ErrorTime 
)
VALUES
(   ''' + Table_name  + '''
    ,ERROR_NUMBER()
    ,ERROR_MESSAGE()
    ,ERROR_PROCEDURE()
    ,ERROR_STATE()
    ,ERROR_SEVERITY()
    ,ERROR_LINE()
    ,GETDATE()  
);
END CATCH '
FROM INFORMATION_SCHEMA.COLUMNS
WHERE   TABLE_NAME != 'UpdateSSNErrorLog'
        --AND Column_Name ='SSN';

SELECT @cmd

----To Execute
--EXEC(@cmd)

此外,您可以向Update SSNErroLog添加一列以记录您是否已解决错误,或者您只需删除已解决的行。