SQL Server:为所有用户提供所有数据

时间:2011-02-24 13:15:56

标签: sql sql-server sql-server-2005

我制作了一个用于检查用户是否可用的sp。 sp计算用户的平衡。

现在,如果从UI中选择用户,它运行正常。但如果没有选中,那么我需要为应用程序中的所有用户带来余额。

为此我已经制作了光标,为所有用户带来了平衡。但是我的sp有很多案例 - 当把光标放在里面时这是不可行的解决方案。

因此我做了一个函数,但抛出了

的错误

函数中包含的Select语句无法将数据返回给客户端。

我正在使用SQL Server 2005.请指导我。

更新:已添加功能

ALTER FUNCTION dbo.fngetBalance()

RETURNS @balanceTable TABLE
(
    Parambalance bigint
)   
AS
BEGIN
--INSERT INTO @balanceTable (Parambalance) VALUES (1)

DECLARE getBalance CURSOR
FOR 
  SELECT  temp2.BillCharge - temp2.Receipt - temp2.AdjustedAmt AS Balance from
                    (SELECT SUM(Fees) AS BillCharge, temp.*  FROM BillDetail
                    JOIN
                    (SELECT
                            BillDetail.PatientID,
                            CONVERT(VARCHAR(15), dbo.Examination.ExamDt, 101) AS BillDate,
                           'Payment received' AS DescOfService,
                            Payment.BillID AS BillID, 
                            SUM(Isnull(Payment.PlanPaid, 0)) + SUM(IsNull(Payment.PatPaid, 0))  as Receipt,
                            SUM(ISNULL(Payment.WriteOff1, 0)) + SUM(IsNUll(Payment.Writeoff2, 0))  as AdjustedAmt
                           --Examination.ExamDt   
                          FROM
                            Billdetail
                            JOIN Payment ON Payment.BillDetailID = BillDetail.BillDetailID
                            JOIN dbo.Examination ON dbo.BillDetail.ExaminationID = dbo.Examination.ExaminationID 
                            GROUP BY Payment.BillID, Examination.ExamDt,BillDetail.PatientID) temp ON 
                            temp.BillID = Billdetail.BillID
                            GROUP BY temp.BillID, temp.Receipt, temp.AdjustedAmt,temp.BillDate, temp.DescOfService, temp.PatientID) AS temp2
                            ORDER BY temp2.PatientID
--SELECT temp-temp2  AS balance from
--(SELECT SUM (BillDetail.Fees) AS fees  FROM dbo.BillDetail GROUP BY billdetail.PatientID) AS temp
--JOIN
--(SELECT SUM (payment.planPaid) + SUM(payment.patpaid) + SUM (Payment.WriteOff1) + SUM (Payment.WriteOff2) AS payment
--FROM dbo.Payment GROUP BY payment.PatientID) temp2
OPEN getBalance 
FETCH NEXT FROM getBalance
WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM getBalance
END

CLOSE getBalance
DEALLOCATE getBalance
RETURN
END

2 个答案:

答案 0 :(得分:1)

您需要将fetch语句的结果插入到返回值表中。

  CREATE FUNCTION dbo.fngetBalance()
  RETURNS @balanceTable TABLE
  (
     Parambalance bigint
  )
  AS
  BEGIN
     DECLARE getBalance CURSOR FOR
     SELECT [... insert your code here ...]

     DECLARE @balance bigint

     OPEN getBalance
     FETCH NEXT FROM getBalance
     INTO @balance
     WHILE @@FETCH_STATUS = 0
     BEGIN
       INSERT INTO @balanceTable VALUES(@balance)
       FETCH NEXT FROM getBalance
       INTO @balance
     END
     CLOSE getBalance
     DEALLOCATE getBalance
     RETURN   
  END

答案 1 :(得分:0)

听起来你想使用table valued function。您需要从函数中返回表。注意:要生成可用的结果集,您需要像这样调用函数:

select * from myFunction

希望有所帮助。