如何在一个存储过程中编写多重插入语句

发布于 2024-12-22 08:46:06 字数 1853 浏览 2 评论 0原文

我想在三个表的一页中获取用户的记录 即 Sender ReceiverParcel ,我为它编写存储过程,但当我保存它时。它给出以下ERROR 我无法理解错误,即@P_ID处的语法不正确,

存储过程代码

ALTER PROCEDURE dbo.ExSenderReceiveParcel

@S_Name varchar(Max),
@S_Country varchar(Max), 
@S_City varchar(Max), 
@S_StreetNo varchar(Max), 
@S_HouseNo varchar(Max), 
@S_Phone varchar(Max), 
@S_Mobile varchar(Max), 
@S_Email varchar(Max), 
@S_CreditCardNo varchar(Max), 
@S_PinCode varchar(Max),

@R_Name varchar(Max), 
@R_Country varchar(Max), 
@R_City varchar(Max), 
@R_StreetNo varchar(Max), 
@R_HouseNo varchar(Max), 
@R_Phone varchar(Max), 
@R_Mobile varchar(Max), 
@R_Email varchar(Max),

@P_Weight varchar(MAX),
@P_Status varchar(MAX),
@P_Location varchar(MAX),
@P_Id numeric(18, 0) out

AS 一样 开始

DECLARE @S_Id numeric(18, 0),
@R_Id numeric(18, 0)

INSERT INTO Sender
                      (Name, S_Country, S_City, StreetNo, HouseNo, Phone, Mobile, Email, CreditCardNo, PinCode)
VALUES     (
    @S_Name,
    @S_Country,
    @S_City, 
    @S_StreetNo, 
    @S_HouseNo, 
    @S_Phone, 
    @S_Mobile, 
    @S_Email, 
    @S_CreditCardNo, 
    @S_PinCode);


    SET @S_Id = SCOPE_IDENTITY();

    INSERT INTO Receiver
       (Name, R_Country, R_City, StreetNo, HouseNo, Phone, Mobile, Email)
VALUES 
        (
            @R_Name, 
            @R_Country, 
            @R_City, 
            @R_StreetNo, 
            @R_HouseNo, 
            @R_Phone, 
            @R_Mobile, 
            @R_Email
        );


        SET @R_Id = SCOPE_IDENTITY();

        INSERT INTO Parcel
    (Weight, Status, Location, Sender_Id, Receiver_Id)
VALUES     
(
    @P_Weight,
    @P_Status,
    @P_Location,
    @S_Id,
    @R_Id
);

SET @P_Id = SCOPE_IDENTITY();

Select @P_Id

I want to Get the REcord from the user in one page of three table
i.e Sender Receiver and Parcel and i write the store procedure for it as but when i save it. It gives the following ERROR
I Can't understand the ERROR i.e Incorrect syntax at @P_ID

The Store Procedure code is as

ALTER PROCEDURE dbo.ExSenderReceiveParcel

@S_Name varchar(Max),
@S_Country varchar(Max), 
@S_City varchar(Max), 
@S_StreetNo varchar(Max), 
@S_HouseNo varchar(Max), 
@S_Phone varchar(Max), 
@S_Mobile varchar(Max), 
@S_Email varchar(Max), 
@S_CreditCardNo varchar(Max), 
@S_PinCode varchar(Max),

@R_Name varchar(Max), 
@R_Country varchar(Max), 
@R_City varchar(Max), 
@R_StreetNo varchar(Max), 
@R_HouseNo varchar(Max), 
@R_Phone varchar(Max), 
@R_Mobile varchar(Max), 
@R_Email varchar(Max),

@P_Weight varchar(MAX),
@P_Status varchar(MAX),
@P_Location varchar(MAX),
@P_Id numeric(18, 0) out

AS
Begin

DECLARE @S_Id numeric(18, 0),
@R_Id numeric(18, 0)

INSERT INTO Sender
                      (Name, S_Country, S_City, StreetNo, HouseNo, Phone, Mobile, Email, CreditCardNo, PinCode)
VALUES     (
    @S_Name,
    @S_Country,
    @S_City, 
    @S_StreetNo, 
    @S_HouseNo, 
    @S_Phone, 
    @S_Mobile, 
    @S_Email, 
    @S_CreditCardNo, 
    @S_PinCode);


    SET @S_Id = SCOPE_IDENTITY();

    INSERT INTO Receiver
       (Name, R_Country, R_City, StreetNo, HouseNo, Phone, Mobile, Email)
VALUES 
        (
            @R_Name, 
            @R_Country, 
            @R_City, 
            @R_StreetNo, 
            @R_HouseNo, 
            @R_Phone, 
            @R_Mobile, 
            @R_Email
        );


        SET @R_Id = SCOPE_IDENTITY();

        INSERT INTO Parcel
    (Weight, Status, Location, Sender_Id, Receiver_Id)
VALUES     
(
    @P_Weight,
    @P_Status,
    @P_Location,
    @S_Id,
    @R_Id
);

SET @P_Id = SCOPE_IDENTITY();

Select @P_Id

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

未蓝澄海的烟 2024-12-29 08:46:06

您的过程末尾没有 END 子句。

应该是

CREATE PROC pSomething (@params) AS     
BEGIN..<code here>..END

另外,你的类型应该被清理;如果数据库中的所有内容都是 VARCHAR(MAX) 那么您有时会遇到一些空间问题。 SCOPE_IDENTITY 还返回一个整数,而不是数值;在这种情况下,这可能不是什么大问题,但这是我的一个小烦恼:)

You don't have an END clause at the end of your procedure.

It should be

CREATE PROC pSomething (@params) AS     
BEGIN..<code here>..END

Also, your types should be cleaned up; if everything in your database is a VARCHAR(MAX) then you will run into some space issues at some point. SCOPE_IDENTITY also returns an integer, not a numeric value; it may not be a big deal in this context, but it's a pet peeve of mine :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文