如何在一个存储过程中编写多重插入语句
我想在三个表的一页中获取用户的记录 即 Sender
Receiver
和 Parcel
,我为它编写存储过程,但当我保存它时。它给出以下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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的过程末尾没有 END 子句。
应该是
另外,你的类型应该被清理;如果数据库中的所有内容都是 VARCHAR(MAX) 那么您有时会遇到一些空间问题。 SCOPE_IDENTITY 还返回一个整数,而不是数值;在这种情况下,这可能不是什么大问题,但这是我的一个小烦恼:)
You don't have an END clause at the end of your procedure.
It should be
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 :)