简体   繁体   中英

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语句放入了事务中,因此我创建了一个无限循环。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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