SQL Server:打印出游标值

时间:2015-03-20 01:05:01

标签: sql sql-server printing cursor

我希望能够打印输出光标中的值或输出存储过程的值。我很难弄清楚如何做到这一点。我需要将我的值输出到一张表格上,例如关于哪些客户已经付款以及哪些没有付款的报告。比较会很棒。

以下是我尚未付款的客户的存储过程:

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

所以我需要一个光标来打印我的值。如果有人可以提供帮助,我将不胜感激。如何在代码中执行此操作的示例将不胜感激。

2 个答案:

答案 0 :(得分:1)

我建议远离游标,除非有特定的正当理由使用游标。您可以通过让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 * from AdminReport where paymentConfirmation = 'TRUE'

和未付款的

Select * from AdminReport where paymentConfirmation = 'FALSE'

答案 1 :(得分:0)

虽然我不相信这是最有效的方法(SQL Server将数据提供给设计用于制作显示数据的东西; 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并不是真正做这种格式化的地方。那里有更好的工具!