[英]How do I create a stored procedure?
I'm am trying to create a stored procedure that generates the update statements to update incorrectly entered SSN with the correct SSN in over 193 tables with SSN columns one whole transaction. 我正在尝试创建一个存储过程,该存储过程将生成更新语句,以在超过193个具有SSN列的表中使用正确的SSN更新输入错误的SSN,这是一个完整的事务。 I want this stored procedure to log the error messages and rollback the enter transaction in cases where the update fails. 我希望此存储过程记录错误消息并在更新失败的情况下回滚enter事务。
How do I approach this? 我该如何处理?
My approach to this exercise is as listed below: if there is an efficient way of doing this, please let me know. 我的练习方法如下:如果有有效的方法,请告诉我。 I need quality check done on my code. 我需要对代码进行质量检查。 Please help! 请帮忙!
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;
I would create a physical table first off with the table name so you know which table it errored out on. 我将首先使用表名创建一个物理表,以便您知道它在哪个表上出错。 Second, I think you should execute all your updates at once. 其次,我认为您应该立即执行所有更新。 Note: I don't know if your SSN column is INT or VARCHAR. 注意:我不知道您的SSN列是INT还是VARCHAR。 If Varchar, you'll need to add quotes around it. 如果是Varchar,则需要在其周围添加引号。
Try this out: 试试看:
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)
Also you could add a column to Update SSNErroLog to note whether you resolved an error or not or you could simply delete the rows once resolved. 您也可以在Update SSNErroLog中添加一列,以记录是否解决了错误,或者可以在解决后直接删除行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.