假设我们的类具有相同类型的属性(我将在C#中描述类,但这不重要)
class Exception
{
public string Message { get; set; }
public string StackTrace { get; set; }
public Exception InnerException { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
从上面的代码中可以看出,存在具有嵌套异常的异常类.
让我们创建用于存储这些异常的表
CREATE TABLE Exceptions
(
Id INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[Message] NVARCHAR(MAX) NOT NULL,
StackTrace NVARCHAR(MAX) NULL,
InnerExceptionId INT NULL,
CONSTRAINT FK__Exceptions_Id__Exceptions_InnerExceptionId
FOREIGN KEY(InnerExceptionId) REFERENCES Exceptions (Id)
);
Run Code Online (Sandbox Code Playgroud)
当然,我可以创建一些正常工作的代码:例如一些存储过程将TVP作为参数然后循环通过TVP行并逐个插入新行.
但是,是否有任何SQL代码可以优雅地插入嵌套异常?
Exceptions表,获取链接到原始代理ID的生成的IDENTITY值所以,@original_list应该是一个输入参数.然后你需要一个MERGE很好的技巧- 链接src.id到dst.id插入后.然后只需将旧值转换为新值.所有命令都是基于集合的,没有循环.
DECLARE @original_list TABLE (
surrogate_no int not null,
msg varchar(100) not null,
inner_surrogate_no int null
);
insert into @original_list (surrogate_no, msg, inner_surrogate_no)
values
(1000, 'err 1000', null),
(1010, 'err 1010->1000', 1000),
(1020, 'err 1020', null),
(1030, 'err 1030->1010', 1010)
-- args prepared, starting migration
DECLARE @migration TABLE (
src_id int not null,
dst_id int not null
)
merge Exceptions t
using @original_list s
on 1=0 --<< we are not looking for updates
when not matched by target then
insert (message)
values (s.msg)
output s.surrogate_no, inserted.id ---<<< here is the main trick: src.id and matching dst.id
into @migration(src_id, dst_id)
;
-- now all error messages are inserted, but none of them have InnerExceptionId
update e set
InnerExceptionId = mp.dst_id
from Exceptions e
inner join @migration m --<< get original surrogate_no
on m.dst_id = e.id
inner join @original_list o --<< grab original row
on o.surrogate_no = m.src_id
inner join @migration mp --<< locate dst.id for inner_surrogate_no
on mp.src_id = o.inner_surrogate_no
Run Code Online (Sandbox Code Playgroud)
这是此类任务的常见解决方案.完整来源
最终数据:
| Id | Message | StackTrace | InnerExceptionId |
|----|----------------|------------|------------------|
| 1 | err 1000 | (null) | (null) |
| 2 | err 1010->1000 | (null) | 1 |
| 3 | err 1020 | (null) | (null) |
| 4 | err 1030->1010 | (null) | 2 |
Run Code Online (Sandbox Code Playgroud)
Treeview by递归cte:
| Message | Lvl | id | InnerExceptionID |
|------------------------------------------------|-----|----|------------------|
| err 1000 | 1 | 1 | (null) |
| err 1020 | 1 | 3 | (null) |
| err 1010->1000>>>(err 1000) | 2 | 2 | 1 |
| err 1030->1010>>>(err 1010->1000>>>(err 1000)) | 3 | 4 | 2 |
Run Code Online (Sandbox Code Playgroud)
注意,sqlfiddle不允许我在更大的脚本中运行MERGE(它一直以分号异常失败),所以我将@表转换为持久表并将merge合并到动态sql中,但是你不需要这样做真实服务器.
我最终创建了带有 TVP 和游标循环的存储过程。
这是我的表值参数定义:
CREATE TYPE ExceptionTableType AS TABLE
(
[Message] NVARCHAR(MAX) NOT NULL,
StackTrace NVARCHAR(MAX) NULL
);
Run Code Online (Sandbox Code Playgroud)
这是带有游标循环的存储过程
CREATE PROCEDURE LogException
@exceptions ExceptionTableType READONLY
AS
BEGIN
DECLARE @cursor CURSOR;
DECLARE @message NVARCHAR(MAX);
DECLARE @stackTrace NVARCHAR(MAX);
DECLARE @innerExceptionId INT = NULL;
DECLARE @outputTable TABLE (Id INT);
BEGIN
SET @cursor = CURSOR FOR
SELECT [Message], StackTrace
FROM @exceptions;
OPEN @cursor
FETCH NEXT FROM @cursor
INTO @message, @stackTrace;
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO Exceptions
([Message], StackTrace, InnerExceptionId)
OUTPUT INSERTED.Id INTO @outputTable (Id)
VALUES
(@message, @stackTrace, @innerExceptionId);
SELECT @innerExceptionId = Id
FROM @outputTable;
FETCH NEXT FROM @cursor
INTO @message, @stackTrace;
END;
CLOSE @cursor;
DEALLOCATE @cursor;
END
END
Run Code Online (Sandbox Code Playgroud)
通过 SQL 调用存储过程的示例:
DECLARE @exceptions AS ExceptionTableType;
INSERT INTO @exceptions
([Message], [StackTrace])
VALUES
('My exception', 'Some stack trace here'),
('My inner exception', 'Dummy data'),
('My inner exception 2', 'Dummy data 2');
EXEC LogException @exceptions;
Run Code Online (Sandbox Code Playgroud)