我正在尝试创建一个函数,该函数返回一个包含酒店客人个人信息的表格(该功能应仅打印每个账单上的客人个人信息),并使用光标打印每个结算项目以及最终结果总。简单地说,我正在尝试打印客人的酒店账单。单个客人可能有多个住宿的多个账单。不幸的是,我需要使用游标。
到目前为止,这是我的代码。我的代码不会编译,因为我不知道要为某些部分添加什么。
IF OBJECT_ID(N'dbo.fn_ProduceBill', N'FN') IS NOT NULL
DROP FUNCTION dbo.fn_ProduceBill;
GO
CREATE FUNCTION dbo.fn_ProduceBill(@FolioID smallint)
RETURNS @Result TABLE
(
GuestFirst varchar(20), GuestLast varchar(20), GuestAddress1 varchar(30), GuestAddress2 varchar(10), GuestCity varchar(20), GuestState char(2), GuestPostalCode char(10), CheckinDate smalldatetime, RoomNumber varchar(5)
)
AS
BEGIN
INSERT INTO @Result
SELECT G.GuestFirst, G.GuestLast, G.GuestAddress1, G.GuestAddress2, G.GuestCity, G.GuestState, G.GuestPostalCode, F.CheckinDate, R.RoomNumber
FROM Folio F, Guest G, Room R
WHERE F.GuestID = G.GuestID
AND F.RoomID = R.RoomID
AND F.FolioID = @FolioID;
RETURN
END
DECLARE @BillingCategoryID smallint
DECLARE @BillingAmount smallmoney
DECLARE BillingCursor CURSOR FOR
SELECT BillingCategoryID, BillingAmount
FROM Billing
WHERE FolioID = @FolioID --ERROR: Must declare scalar variable @FolioID
OPEN BillingCursor
FETCH NEXT FROM BillingCursor
INTO @BillingCategoryID, @BillingAmount
WHILE @@FETCH_STATUS = 0
BEGIN
--How do I add each billing amount?????
SELECT @BillingCategoryID, @BillingAmount
FROM --?
FETCH NEXT FROM BillingCursor
INTO @BillingCategoryID, @BillingAmount
END
CLOSE BillingCursor
DEALLOCATE BillingCursor