从存储过程插入完成但不插入

时间:2017-08-17 04:16:27

标签: sql-server stored-procedures transactions insert try-catch

这段代码看起来像是为我工作,但事实并非如此。

ALTER PROC dbo.spInsertNewVendor
AS
BEGIN
        BEGIN TRY
            BEGIN TRAN
            INSERT INTO Vendors (VendorID, VendorName, Address, City, State, ZipCode, Active)
            VALUES (1002, 'Appleburg', '1472 Witch Hollow Way', 'Salt Lake City', 'Utah', 84115, 'TRUE')
            COMMIT TRAN
        END TRY
        BEGIN CATCH
            ROLLBACK TRAN
        END CATCH
END

EXEC dbo.spInsertNewVendor

我已经改变了存储过程,一切都很顺利,但是当我执行该过程时,它没有像我想要的那样插入任何行。有什么不同我应该做的吗?我失踪了什么?

更新:

Vendors
-VendorID(PK,int)
-VendorName(nvarchar)
-Address(nvarchar)
-City(nvarchar)
-State(nvarchar)
-ZipCode(int)
-Active(nvarchar)

3 个答案:

答案 0 :(得分:0)

 -- Vendor ID Is a Primary Key And You Cannot Insert Value In PK.  
      ALTER PROC dbo.spInsertNewVendor
        AS
        BEGIN
                BEGIN TRY
                    BEGIN TRAN
                    INSERT INTO Vendors ( VendorName, Address, City, State, ZipCode, Active)
                    VALUES ('Appleburg', '1472 Witch Hollow Way', 'Salt Lake City', 'Utah', 84115, 'TRUE')
                    COMMIT TRAN
                END TRY
                BEGIN CATCH
                    ROLLBACK TRAN
                END CATCH
        END
        GO
        EXEC dbo.spInsertNewVendor

答案 1 :(得分:0)

首先找出错误:

alter PROC dbo.spInsertNewVendor
AS
BEGIN
        BEGIN TRY
            BEGIN TRAN
            INSERT INTO Vendors (VendorID, VendorName, Address, City, State, ZipCode, Active)
            VALUES (1002, 'Appleburg', '1472 Witch Hollow Way', 'Salt Lake City', 'Utah', 84115, 'TRUE')
            COMMIT TRAN
        END TRY
        BEGIN CATCH
            ROLLBACK TRAN
            SELECT ERROR_MESSAGE() AS ErrorMessage;
        END CATCH
END

EXEC dbo.spInsertNewVendor

如果vendorID是Identity列,那么上面的代码会给你错误 试试这个

alter PROC dbo.spInsertNewVendor
AS
BEGIN
        BEGIN TRY
            BEGIN TRAN
            INSERT INTO Vendors ( VendorName, Address, City, State, ZipCode, Active)
            VALUES ( 'Appleburg', '1472 Witch Hollow Way', 'Salt Lake City', 'Utah', 84115, 'TRUE')
            COMMIT TRAN
        END TRY
        BEGIN CATCH
            ROLLBACK TRAN
            SELECT ERROR_MESSAGE() AS ErrorMessage;
        END CATCH
END

EXEC dbo.spInsertNewVendor

答案 2 :(得分:0)

我在本地复制了您的完整SQL查询,并且工作正常,更新表后您可能遇到的唯一问题是您必须已将VendorID列指定为自动增量和插入过程的标识您正在为VendorID提供值。

我会建议您在尝试再次执行程序后请清洁您的桌子。

这是我在本地系统上尝试的内容: 我希望我的Vendors表的架构必须和你的架构一样。

create table Vendors
(VendorID int,
VendorName nvarchar(100),
Address nvarchar(100),
City nvarchar(100),
State nvarchar(100),
ZipCode int,
Active nvarchar(100)
CONSTRAINT PK_Cendor PRIMARY KEY (VendorID))

CREATE PROC dbo.spInsertNewVendor
AS
BEGIN
        BEGIN TRY
            BEGIN TRAN
            INSERT INTO Vendors (VendorID, VendorName, Address, City, State, ZipCode, Active)
            VALUES (1002, 'Appleburg', '1472 Witch Hollow Way', 'Salt Lake City', 'Utah', 84115, 'TRUE')
            COMMIT TRAN
        END TRY
        BEGIN CATCH
            ROLLBACK TRAN
        END CATCH
END

EXEC dbo.spInsertNewVendor

<强>输出: enter image description here