简体   繁体   English

带有表作为输入的存储过程事务执行很多次,直到达到事务限制

[英]Stored procedure transaction with table as input executes so many times until it reaches transaction limit

I'm working on transaction exercise. 我正在进行交易练习。 I have several tables as shown on the screenshot here: 我有几个表,如截图所示:

图

Also I have custom datatype declared: 我也声明了自定义数据类型:

create type TagList as table
(
    TagName varchar(255)
)

Now, I just tried to pass this table as variable to my stored procedure to test if it works, however when I execute following transaction it gets executed so many times until it reaches the transaction limit 现在,我只是尝试将此表作为变量传递给我的存储过程以测试它是否有效,但是当我执行以下事务时,它会执行很多次,直到达到事务限制

ALTER PROCEDURE NewBlogPost
    @headline VARCHAR(255),
    @content VARCHAR(MAX),
    @userId INT,
    @categoryId INT,
    @tagId INT,
    @tags AS dbo.TagList READONLY
AS
BEGIN TRY
    BEGIN TRANSACTION
        DECLARE @postId INT

        SELECT @postId = (SELECT TOP 1 PostId
                          FROM Posts
                          ORDER BY PostId DESC) + 1;

        INSERT INTO Posts (PostId, Headline, Content, UserId, CategoryId, StateId, PostedDate, LastEdit)
        VALUES (@postId, @headline, @content, @userId, @categoryId, 1, GETDATE(), GETDATE())

        INSERT INTO TagPost (PostId, TagId)
        VALUES (@postId, @tagId)

        COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
        SELECT
            ERROR_NUMBER() AS ErrorNumber,
            ERROR_SEVERITY() AS ErrorSeverity,
            ERROR_STATE() AS ErrorState,
            ERROR_PROCEDURE() AS ErrorProcedure,
            ERROR_LINE() AS ErrorLine,
            ERROR_MESSAGE() AS ErrorMessage

        ROLLBACK TRANSACTION
    END CATCH

DECLARE @TagTable TagList

INSERT INTO @TagTable(TagName)
VALUES ('Computers'), ('MobilePhones'), ('Pesos')

EXEC NewBlogPost @headline = 'Framework called VUE breaks new record', @content = 'New framework had broken world record in daily download', @userId = 1, @categoryId = 1, @tagId = 1, @tags = @TagTable

Any idea why am I executing this transaction so many times? 知道我为什么这么多次执行这个交易?

好吧,我已经想到了这一点,它不得不对Transaction做任何事情,我不小心将exec语句放入了事务中,因此我创建了一个无限循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM