存储过程输出错误

时间:2013-05-07 13:01:35

标签: sql-server stored-procedures

我的存储过程有两个游标。 存储过程的输出采用以下格式。

companyid   companyname                                        id          idname                                             level3                                             level4                                             level5                                                                                                                                                                                                                                                         currency                                           accountdefinition                                  accname                                            accountno   debits                                  credits
----------- -------------------------------------------------- ----------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ----------- --------------------------------------- ---------------------------------------
101         Fund One Company                                   1001        Test Fund                                          Assets                                             Assets for PL Accounts                             NULL                                               NULL                                               NULL                                               NULL                                               NULL                                               KWD                                                CA                                                 Current Account                                    NULL        NULL                                    NULL

(1 row(s) affected)

companyid   companyname                                        id          idname                                             level3                                             level4                                             level5                                                                                                                                                                                                                                                         currency                                           accountdefinition                                  accname                                            accountno   debits                                  credits
----------- -------------------------------------------------- ----------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ----------- --------------------------------------- ---------------------------------------
101         Fund One Company                                   1001        Test Fund                                          Assets                                             Assets for PL Accounts                             NULL                                               NULL                                               NULL                                               NULL                                               NULL                                               KWD                                                CA                                                 Current Account                                    NULL        NULL                                    NULL

(1 row(s) affected)

companyid   companyname                                        id          idname                                             level3                                             level4                                             level5                                                                                                                                                                                                                                                         currency                                           accountdefinition                                  accname                                            accountno   debits                                  credits
----------- -------------------------------------------------- ----------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ----------- --------------------------------------- ---------------------------------------
101         Fund One Company                                   1001        Test Fund                                          Assets                                             Accounts for MM PL accounts                        NULL                                               NULL                                               NULL                                               NULL                                               NULL                                               KWD                                                MPL                                                PL accounts for MM                                 NULL        NULL                                    NULL

(1 row(s) affected)

而不是将所有3行显示为单个输出。它将每行显示为单个输出。这导致报告级别出现问题。

代码:

alter  PROCEDURE TrialBalance
(
@companyid INT,   
@id    INT                --Input parameter ,  Studentid of the student
--@id VARCHAR(200)  OUT        -- Out parameter declared with the help of OUT keyword
)
AS
BEGIN
declare @level3 varchar(50),@level4 varchar(50),@level5 varchar(50),@level6 varchar(50),@level7 varchar(50),@level8 varchar(50),@level9 varchar(50),@level10 varchar(50),@level11 varchar(50),@rfrom int,@rto int,
@companyname varchar(50),@idname varchar(50),@accname varchar(50),@debits decimal(15,3),@credits decimal(15,3),@acno int,@acname varchar(25)
declare c cursor for SELECT 
level3,
level4,
level5,
level6,
level7,
level8,
level9,
level10,
level11,
rangefrom,
rangeto
from linedetails where level1 = @companyid and level2 = @id order by reportlineid

select @companyname = description from company where companycode = @companyid
select @idname = description from fundparameters where fundid = @id


open c
fetch next from c into @level3,@level4,@level5,@level6,@level7,@level8,@level9,@level10,@level11,@rfrom,@rto
WHILE @@FETCH_STATUS = 0
BEGIN
select @accname = description from accounttype where accounttypeid = @level11
if @rfrom is not null and @rto is not null 
declare d cursor for select accountno,totaldebitslcy,totalcreditslcy from fund.dbo.account where (accountno >= @rfrom and accountno <= @rto) and currency = @level10
open d
fetch next from d into @acno,@debits,@credits
WHILE @@FETCH_STATUS = 0
BEGIN
select @companyid as companyid ,@companyname as companyname ,@id as id ,@idname as idname,@level3 as level3 ,@level4 as level4,@level5 as level5,
@level6,
@level7,
@level8,
@level9,
@level10 as currency,
@level11 as accountdefinition ,@accname as accname,@acno as accountno, @debits as debits,@credits as credits
fetch next from d into @acno,@debits,@credits
end
close d
deallocate d
fetch next from c into @level3,@level4,@level5,@level6,@level7,@level8,@level9,@level10,@level11,@rfrom,@rto
end
end
close c
deallocate c

2 个答案:

答案 0 :(得分:0)

在游标循环的每次运行中都进行SELECT。这导致每个循环运行一个选择输出。

如果你想让它们一起在循环之外声明一个临时表,并在循环期间将结果插入到该表中。

然后在循环结束后选择该临时表的所有内容。

答案 1 :(得分:0)

问题在于嵌套游标中的Select查询是从变量中选择数据,这些变量一次只能保存单个值,因此当游标迭代时,select语句仅为每次迭代返回一个元组。因此输出在逻辑上是正确的。

问题在于:

select @companyid as companyid ,@companyname as companyname ,@id as id ,@idname as     idname,@level3 as level3 ,@level4 as level4,@level5 as level5,
@level6, @level7,@level8,@level9,Slevel10 as currency,@level11 as accountdefinition @accname as accname,@acno as accountno, @debits as debits,@credits as credits

相反,创建一个表来临时保存光标中的数据,并在光标迭代完成后,从该表中选择数据。

而不是选择,写一个这样的插入查询:

insert into tmpTable values(@comapnyid,@companyname,@id,...etc);

而且,就在你的程序结束之前,

select * from tmpTable;

还有一件事,

在SP的开头,您可能希望删除tmpTable,以便表中任何已填充的数据不会影响当前值。等,

drop table tmpTable;