-2

我写了一个存储过程,它看起来设置正确,这里是存储过程:

CREATE PROCEDURE sp_Insert$Order$For$Travel
(
@CardHolderName varchar(50),--Userprofile
@CardNumber int, --Userprofile
@SecurityCode int, --Userprofile
@ExpiryDate date, --Userprofile
@DeparturePoint int, --Order
@DestinationPoint int, --Order
@DepartureTime datetime, --Order
@DestinationTime datetime, --Order
@Passengers int, --Order
@RoundTrip char(3), --Order
@ReturnDate datetime, --Order
@OrderNumber int --order
)
AS
Declare @UserIdentity int

Insert into [UserProfile]
(CardHolderName, CardHolderName, SecurityCode, ExpiryDate)
values
(@CardHolderName, @CardNumber, @SecurityCode, @ExpiryDate)
set @UserIdentity = @@IDENTITY

Insert into [Order]
(DeparturePoint, DestinationPoint, DepartureTime, DestinationPoint,DestinationTime,Passengers,RoundTrip, ReturnDate, OrderNumber, UserID)
values
(@DeparturePoint, @DestinationPoint, @DepartureTime, @DestinationPoint, @DestinationTime,@Passengers, @RoundTrip, @ReturnDate, @OrderNumber, @UserIdentity)

当我去执行它时,我收到这两个错误。

消息 264,级别 16,状态 1,过程 sp_Insert$Order$For$Travel,第 25 行
在 INSERT 的 SET 子句或列列表中多次指定列名“CardHolderName”。在同一个子句中,一列不能分配多个值。修改子句以确保一列只更新一次。如果此语句更新或将列插入到视图中,列别名可以隐藏代码中的重复项。

消息 264,级别 16,状态 1,过程 sp_Insert$Order$For$Travel,第 31 行
在 SET 子句或 INSERT 的列列表中多次指定列名“DestinationPoint”。在同一个子句中,一列不能分配多个值。修改子句以确保一列只更新一次。如果此语句更新或将列插入到视图中,列别名可以隐藏代码中的重复项。

我查看了我的存储过程并查看了我过去做过的其他过程,看起来还不错,我只是看不出问题出在哪里。

任何人都看到我遇到的问题是什么?我已经检查了我的参数,根据我要插入它们的表中的列,它们都是正确的......

有任何想法吗?

4

3 回答 3

2

It's exactly what the error says:

 Insert into [UserProfile]
 (CardHolderName, CardHolderName, SecurityCode, ExpiryDate)
 values
 (@CardHolderName, @CardNumber, @SecurityCode, @ExpiryDate)

... has CardHolderName twice in the destination columns. Presumably the second one should be CardNumber.

The other is similar.

于 2013-09-01T15:46:26.503 回答
1

Try this instead:

Insert into [UserProfile](CardHolderName, CardNumber, SecurityCode, ExpiryDate)
    values (@CardHolderName, @CardNumber, @SecurityCode, @ExpiryDate);

set @UserIdentity = SCOPE_IDENTITY();

I think you want CardNumber for the second variable rather than CardHolderName.

I also changed the @@IDENTITY to SCOPE_IDENTITY(), which is safer.

EDIT:

I think the documentation does a good job of explaining it here.

The basic idea is that the four ways of getting the most recent identity (@@IDENTITY, SCOPE_IDENTIY(), IDENT_CURRENT(), and the OUTPUT clause). The safest is the OUTPUT clause. The other three depend on three things: the "table", the "connection", and the "scope".

The first two return the most recent identity regardless of table, but on the same connection. If there is an insert trigger on the table, the trigger might insert a value into another table. @@IDENTITY will return the id from that table (same connection, different scope). SCOPE_IDENTITY() will return the id from the intended table (same connection, same scope). IDENT_CURRENT() is just inviting a race condition with other connections that might also be adding rows onto the table.

The OUTPUT clause doesn't suffer from these interpretations. It just puts the values from the insert into a temporary table -- at the expense of a bit more coding and learning something new.

于 2013-09-01T15:48:31.750 回答
0

You have errors in the following lines:

Insert into [UserProfile]
(CardHolderName, CardHolderName, SecurityCode, ExpiryDate)

Repeated column name "CardHolderName" twice in insert statement to table "UserProfile"

Insert into [Order]
(DeparturePoint, DestinationPoint, DepartureTime, DestinationPoint)

Repeated column name "DestinationPoint" twice in insert statement to table "Order".

于 2013-09-01T15:46:03.533 回答