我尝试做的就是声明一个名为@ReservationID
的变量,将其初始化为Reservation表中存在某些条件的值,并使用该值作为参数插入新的Folio表条目。当我去打印我的Folio表时,新的表条目不存在。我已经检查并运行了SELECT
语句,并根据条件提供了正确的值。我没有正确初始化或传递变量吗?我在这里错过了什么?任何帮助将不胜感激。
DECLARE @ReservationID smallint;
SET @ReservationID = (SELECT ReservationID FROM Reservation WHERE ReservationDate = CONVERT(varchar, GETDATE()) AND CreditCardID = 1)
SET IDENTITY_INSERT Folio OFF
INSERT INTO Folio (ReservationID, GuestID, RoomID, QuotedRate, CheckinDate, Nights, Status, Comments, DiscountID)
VALUES(@ReservationID, 500, 20, 35.50, '07/24/2016', 3, 'R', NULL, 1);
答案 0 :(得分:0)
您可能无法正确初始化或传递变量。
请参阅MSDN Here以将变量分配给表达式的值。
您需要更正SET& IDENTITY_INSERT如下,
DECLARE @ReservationID smallint;
select @ReservationID = ReservationID FROM Reservation
WHERE ReservationDate = CONVERT(varchar, GETDATE()) AND CreditCardID = 1
SET IDENTITY_INSERT Folio ON
INSERT INTO Folio (ReservationID, GuestID, RoomID, QuotedRate, CheckinDate,Nights, Status, Comments, DiscountID)
VALUES(@ReservationID, 500, 20, 35.50, '07/24/2016', 3, 'R', NULL, 1);
以下是我的快速测试代码
create table Reservation(ReservationID smallint identity, CreditCardID int)
insert into Reservation values(1),(2),(3),(4),(5)
SET IDENTITY_INSERT Reservation ON
DECLARE @ReservationID smallint;
select @ReservationID = ReservationID FROM Reservation
insert into Reservation (ReservationID, CreditCardID)
values(@ReservationID,100)