SQL 服务器:打印出游标值
SQL Server : print out cursor values
我希望能够为我的存储过程打印或输出游标的值。我很难弄清楚如何做到这一点。我需要将我的值输出到 sheet 上,例如关于哪些客户已付款和哪些客户未付款的报告。比较一下就好了。
以下是我针对未付款客户的存储过程:
CREATE PROC [dbo].[AdminReport1]
AS
BEGIN
SELECT
booking.bookingID, booking.totalCost,
booking.bookingDate, booking.paymentConfirmation,
customers.customersID,
customers.firstname, customers.surname,
customers.contactNum
FROM
booking
INNER JOIN
customers ON booking.customerID = customers.customersID
WHERE
paymentConfirmation = 'False'
ORDER BY
bookingDate ASC
END
GO
这是我为已付款的客户存储的程序:
CREATE PROC [dbo].[AdminReport2]
AS
BEGIN
SELECT
booking.bookingID, booking.totalCost,
booking.bookingDate, booking.paymentConfirmation,
customers.customersID,
customers.firstname, customers.surname,
customers.contactNum
FROM
booking
INNER JOIN
customers ON booking.customerID = customers.customersID
WHERE
paymentConfirmation = 'TRUE'
ORDER BY
bookingDate ASC
所以我需要一个光标来打印我的值。如果有人可以提供帮助,将不胜感激。如何在代码中执行此操作的示例将不胜感激。
虽然我不相信这是最有效的方法(SQL 服务器将数据提供给旨在制作显示数据的东西会更好;SSRS 甚至 Excel), 这是我一开始要做的:
--Create somewhere to put the proc data and fill it. This will give us something tangible to query against.
CREATE TABLE #NotPaid
(
bookingID INT ,
totalCost DECIMAL(7, 2) ,
bookingDate DATE ,
paymentConfirmation VARCHAR(50) ,
customersID INT ,
firstname VARCHAR(50) ,
surname VARCHAR(50) ,
contactNum VARCHAR(50)
)
CREATE TABLE #HasPaid
(
bookingID INT ,
totalCost DECIMAL(7, 2) ,
bookingDate DATE ,
paymentConfirmation VARCHAR(50) ,
customersID INT ,
firstname VARCHAR(50) ,
surname VARCHAR(50) ,
contactNum VARCHAR(50)
)
INSERT #NotPaid
EXEC AdminReport1
INSERT #HasPaid
EXEC AdminReport2
--Variables for use in our cursor. I'm only loading one table up as a demonstration.
DECLARE @bookingID INT ,
@totalCost DECIMAL(7, 2) ,
@bookingDate DATE ,
@paymentConfirmation VARCHAR(50) ,
@customersID INT ,
@firstname VARCHAR(50) ,
@surname VARCHAR(50) ,
@contactNum VARCHAR(50)
DECLARE @Library CURSOR
SET
@Library = CURSOR FOR
SELECT bookingID ,
totalCost ,
bookingDate ,
paymentConfirmation ,
customersID ,
firstname,
surname ,
contactNum FROM #HasPaid
--Run the cursor and print out the variables as we loop again. Any formatting will need to be done in here. Again, I'm not sure this is the best way to achieve what you are trying to achieve.
OPEN @Library
FETCH NEXT FROM @getProductID INTO @bookingID, @totalCost, @bookingDate,
@paymentConfirmation, @customersID, @firstname, @surname, @contactNum
PRINT 'I''m a header line'
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @firstname + ' Hasnt paid! His booking date is ' + STR(@bookingDate)
+ '. This is the middle part'
FETCH NEXT FROM @Library INTO @bookingID, @totalCost, @bookingDate,
@paymentConfirmation, @customersID, @firstname, @surname, @contactNum
END
PRINT 'I''m at the bottom'
CLOSE @Library
DEALLOCATE @Library
GO
这应该会给你一个很好的起点。
就一般效率而言,最好只有一个proc,你传递你想要的信息,所以,传递你只想要Paids,或者只想要NotPaids,或者每个人。顺便说一句,SQL 并不是真正进行这种格式设置的地方。有很多更好的工具可以做到这一点!
我建议远离游标,除非有特定的正当理由使用它们。您可以通过使用 view
来稍微简化一下,其中包含您要查找的列
CREATE VIEW [dbo].[AdminReport]
AS
BEGIN
SELECT
b.bookingID,
b.totalCost,
b.bookingDate,
b.paymentConfirmation,
c.customersID,
customers.firstname,
c.surname,
c.contactNum,
paymentConfirmation
FROM
booking b
INNER JOIN customers c
ON b.customerID= c.customersID
--Where
--paymentConfirmation = 'False'
ORDER BY
bookingDate ASC
END
GO
然后您可以 select 支付记录作为
Select * from AdminReport where paymentConfirmation = 'TRUE'
和未付的
Select * from AdminReport where paymentConfirmation = 'FALSE'
我希望能够为我的存储过程打印或输出游标的值。我很难弄清楚如何做到这一点。我需要将我的值输出到 sheet 上,例如关于哪些客户已付款和哪些客户未付款的报告。比较一下就好了。
以下是我针对未付款客户的存储过程:
CREATE PROC [dbo].[AdminReport1]
AS
BEGIN
SELECT
booking.bookingID, booking.totalCost,
booking.bookingDate, booking.paymentConfirmation,
customers.customersID,
customers.firstname, customers.surname,
customers.contactNum
FROM
booking
INNER JOIN
customers ON booking.customerID = customers.customersID
WHERE
paymentConfirmation = 'False'
ORDER BY
bookingDate ASC
END
GO
这是我为已付款的客户存储的程序:
CREATE PROC [dbo].[AdminReport2]
AS
BEGIN
SELECT
booking.bookingID, booking.totalCost,
booking.bookingDate, booking.paymentConfirmation,
customers.customersID,
customers.firstname, customers.surname,
customers.contactNum
FROM
booking
INNER JOIN
customers ON booking.customerID = customers.customersID
WHERE
paymentConfirmation = 'TRUE'
ORDER BY
bookingDate ASC
所以我需要一个光标来打印我的值。如果有人可以提供帮助,将不胜感激。如何在代码中执行此操作的示例将不胜感激。
虽然我不相信这是最有效的方法(SQL 服务器将数据提供给旨在制作显示数据的东西会更好;SSRS 甚至 Excel), 这是我一开始要做的:
--Create somewhere to put the proc data and fill it. This will give us something tangible to query against.
CREATE TABLE #NotPaid
(
bookingID INT ,
totalCost DECIMAL(7, 2) ,
bookingDate DATE ,
paymentConfirmation VARCHAR(50) ,
customersID INT ,
firstname VARCHAR(50) ,
surname VARCHAR(50) ,
contactNum VARCHAR(50)
)
CREATE TABLE #HasPaid
(
bookingID INT ,
totalCost DECIMAL(7, 2) ,
bookingDate DATE ,
paymentConfirmation VARCHAR(50) ,
customersID INT ,
firstname VARCHAR(50) ,
surname VARCHAR(50) ,
contactNum VARCHAR(50)
)
INSERT #NotPaid
EXEC AdminReport1
INSERT #HasPaid
EXEC AdminReport2
--Variables for use in our cursor. I'm only loading one table up as a demonstration.
DECLARE @bookingID INT ,
@totalCost DECIMAL(7, 2) ,
@bookingDate DATE ,
@paymentConfirmation VARCHAR(50) ,
@customersID INT ,
@firstname VARCHAR(50) ,
@surname VARCHAR(50) ,
@contactNum VARCHAR(50)
DECLARE @Library CURSOR
SET
@Library = CURSOR FOR
SELECT bookingID ,
totalCost ,
bookingDate ,
paymentConfirmation ,
customersID ,
firstname,
surname ,
contactNum FROM #HasPaid
--Run the cursor and print out the variables as we loop again. Any formatting will need to be done in here. Again, I'm not sure this is the best way to achieve what you are trying to achieve.
OPEN @Library
FETCH NEXT FROM @getProductID INTO @bookingID, @totalCost, @bookingDate,
@paymentConfirmation, @customersID, @firstname, @surname, @contactNum
PRINT 'I''m a header line'
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @firstname + ' Hasnt paid! His booking date is ' + STR(@bookingDate)
+ '. This is the middle part'
FETCH NEXT FROM @Library INTO @bookingID, @totalCost, @bookingDate,
@paymentConfirmation, @customersID, @firstname, @surname, @contactNum
END
PRINT 'I''m at the bottom'
CLOSE @Library
DEALLOCATE @Library
GO
这应该会给你一个很好的起点。
就一般效率而言,最好只有一个proc,你传递你想要的信息,所以,传递你只想要Paids,或者只想要NotPaids,或者每个人。顺便说一句,SQL 并不是真正进行这种格式设置的地方。有很多更好的工具可以做到这一点!
我建议远离游标,除非有特定的正当理由使用它们。您可以通过使用 view
来稍微简化一下,其中包含您要查找的列
CREATE VIEW [dbo].[AdminReport]
AS
BEGIN
SELECT
b.bookingID,
b.totalCost,
b.bookingDate,
b.paymentConfirmation,
c.customersID,
customers.firstname,
c.surname,
c.contactNum,
paymentConfirmation
FROM
booking b
INNER JOIN customers c
ON b.customerID= c.customersID
--Where
--paymentConfirmation = 'False'
ORDER BY
bookingDate ASC
END
GO
然后您可以 select 支付记录作为
Select * from AdminReport where paymentConfirmation = 'TRUE'
和未付的
Select * from AdminReport where paymentConfirmation = 'FALSE'