真的,我连存储过程都不会用,为什么老是返回0(50分)

  • 主题发起人 主题发起人 hujunyi
  • 开始时间 开始时间
H

hujunyi

Unregistered / Unconfirmed
GUEST, unregistred user!
我建了一个存储过程,在SyBase的SQL Advantage中能执行,并能正确的返回Return值,可是在Delphi4里就不能返回正确的Return值。而且有时候能插入数据,有时不能插入,传入的参数都是一样的。请问各位大仙,请是什么。
为什么受伤的总是我?

这是我的代码:
StoredProc1.ParamByName('@S_jj').Value := '2000000001402001000';
StoredProc1.ParamByName('@S_dkqx').Value := '120';
StoredProc1.ParamByName('@czyh').Value := '01010101';
StoredProc1.ExecProc; (StoreProc11.Open我也试过了)
Caption := StoredProc1.ParamByName('Result').AsString;

//这就是我那可恨的Proc
create proc p_dkzq (@S_jj char(20),@S_dkqx char(3),@czyh char(8))
as
declare @xh char(10),@jgh char(5),@yqx char(3),@d_hkr char(10),@mqhkr char(8)
declare @zhtbm char(20)
declare @chtbm char(20)
declare @khh char(10)
declare @dkzl char(2)
declare @xjj char(20)
declare @hm char(30)
declare @qsrq char(8)
declare @je decimal(16,2)
declare @jkzl char(2)
declare @yjzyll decimal(6,4),@jzyll decimal(6,4),@jkll decimal(6,4)
declare @fdl decimal(4,2)
declare @jkqx char(3)
declare @yqll decimal(6,4)
declare @hkfs char(3),@hkfs1 char(1),@hkfs2 char(3),@hkfs3 char(1),@hkfs4 char(1)
declare @mm char(6)
declare @ffzh char(18),@dkzh char(18),@kkzh char(18)
select * from T_gw
select @kkzh=T_jkcht.kkzh,@khh=T_jkcht.jkrbm,@ffzh=T_jkzht.skrzh,@dkzh=T_jkzht.dkzh,@mm=T_jkcht.dkmm,@hkfs1=T_jkzht.hkfsbm,@hkfs2=T_jkzht.ydsj,
@hkfs3=T_jkzht.hkr,@jkll=T_jj.jkll,@jkqx=T_jj.jkqx,@jkzl=T_jkzht.jkzl,@je=T_jj.jkje,@qsrq=T_jj.jkr,@hm=T_grzl.mc
from T_grzl,T_jkcht,T_jkzht,T_jj where T_jj.bm=@S_jj and T_jj.zhtbm=T_jkzht.bm and T_jkzht.chtbm=T_jkcht.bm
and T_jkcht.jkrbm=T_grzl.bm

select @hkfs4=""
if convert(int,@hkfs2)=1
select @hkfs4="y"
if convert(int,@hkfs2)=3
select @hkfs4="J"
if convert(int,@hkfs2)=6
select @hkfs4="B"
if convert(int,@hkfs2)=12
select @hkfs4="N"
select @hkfs=@hkfs1+@hkfs4+@hkfs3
select @jzyll=min(jzyll) from T_dkll where dkzlbm=@jkzl and convert(int,dkqx)>=convert(int,@S_dkqx)
if (@jzyll is null)
begin
select @jzyll=max(jzyll) from T_dkll where dkzlbm=@jkzl
end
select @yjzyll=min(jzyll) from T_dkll where dkzlbm=@jkzl and convert(int,dkqx)>=convert(int,@jkqx)
if (@yjzyll is null)
begin
select @yjzyll=max(jzyll) from T_dkll where dkzlbm=@jkzl
end
select @fdl=convert(decimal(4,2),((@jkll/@yjzyll)-1)/100.0)
select @yqll=convert(decimal(6,4),sj) from T_xtsz where lx="Rate" and xm="Over"
begin tran tr_dkzq
/*补充借据*/
select @jgh=ssjgh from T_xdry where bm=@czyh
if (@@error!=0 or @@rowcount!=1)
begin
rollback tran tr_dkzq
return 1
end
select @xh=xygbm from T_bmxd where bm="T_jj" and xm=@jgh
if (@@error!=0 or @@rowcount!=1)
begin
rollback tran tr_dkzq
return 2
end
select @zhtbm=zhtbm,@yqx=jkqx,@mqhkr=mqhkr from T_jj
where (bm=@S_jj and ybm is null) or (bm!=@S_jj and ybm=@S_jj)
if(@@error!=0 or @@rowcount!=1)
begin
rollback tran tr_dkzq
return 3
end

select * from T_jj where ybm=@S_jj
if (@@error!=0 or @@rowcount>0)
begin

rollback tran tr_dkzq
return 4
end


insert T_Tjj select * from T_jj
where (bm=@S_jj and ybm is null) or (bm!=@S_jj and ybm=@S_jj)
if (@@error!=0 or @@rowcount!=1)
begin
rollback tran tr_dkzq
return 5
end

update T_bmxd set xygbm=stuff("0000",5-len(ltrim(rtrim(convert(char(4),convert(int,@xh)+1)))),len(ltrim(rtrim(convert(char(4),convert(int,@xh)+1)))),ltrim(rtrim(convert(char(4),convert(int,@xh)+1))))
where bm="T_jj" and xm=@jgh
if(@@error!=0 or @@rowcount!=1)
begin
rollback tran tr_dkzq
return 6
end

select @zhtbm=zhtbm,@yqx=jkqx,@mqhkr=mqhkr from T_jj
where (bm=@S_jj and ybm is null) or (bm!=@S_jj and ybm=@S_jj)
if(@@error!=0 or @@rowcount!=1)
begin
rollback tran tr_dkzq
return 7
end
select @d_hkr=convert(char(10),dateadd(mm,convert(int,@S_dkqx)-convert(int,@yqx),convert(datetime,substring(@mqhkr,1,4)+"."+substring(@mqhkr,5,2)+"."+substring(@mqhkr,7,2))),102)
select @mqhkr=substring(@d_hkr,1,4)+substring(@d_hkr,6,2)+substring(@d_hkr,9,2)
select @xjj=convert(char(4),datepart(yy,getdate()))+substring(@S_jj,5,9)+stuff("0000",5-len(ltrim(rtrim(@xh))),len(ltrim(rtrim(@xh))),ltrim(rtrim(@xh)))+"00"
update T_Tjj set
bm=@xjj,
ybm=@S_jj,
jkqx=@S_dkqx,
jkll=@jzyll*(1+@fdl),
mqhkr=@mqhkr
where bm=@S_jj
if (@@error!=0 or @@rowcount!=1)
begin
rollback tran tr_dkzq
return 8
end

/*补充主合同*/

select @dkzl=jkzl from T_jkzht
where (bm=@zhtbm and ybm is null) or (bm!=@zhtbm and ybm=@zhtbm)
select * from T_jkzht
where ((bm=@zhtbm and ybm is null) or (bm!=@zhtbm and ybm=@zhtbm)) and mqhkr<@mqhkr
if (@@error!=0 or @@rowcount>1)
begin
rollback tran tr_dkzq
return 9
end
select count(*) from T_jkzht
where ((bm=@zhtbm and ybm is null) or (bm!=@zhtbm and ybm=@zhtbm)) and mqhkr<@mqhkr
if (@@error=0 and @@rowcount=1)
begin
insert T_Tjkzht select * from T_jkzht
where (bm=@zhtbm and ybm is null) or (bm!=@zhtbm and ybm=@zhtbm)
if (@@error!=0 or @@rowcount!=1)
begin
rollback tran tr_dkzq
return 10
end

select @xh=xygbm from T_bmxd where bm="T_jkzht" and xm=@jgh
if (@@error!=0 or @@rowcount!=1)
begin
rollback tran tr_dkzq
return 11
end
update T_bmxd set xygbm=stuff("0000",5-len(ltrim(rtrim(convert(char(4),convert(int,@xh)+1)))),len(ltrim(rtrim(convert(char(4),convert(int,@xh)+1)))),ltrim(rtrim(convert(char(4),convert(int,@xh)+1))))
where bm="T_jkzht" and xm=@jgh
if(@@error!=0 or @@rowcount!=1)
begin
rollback tran tr_dkzq
return 12
end
select @chtbm=chtbm from T_jkzht
where (bm=@zhtbm and ybm is null) or (bm!=@zhtbm and ybm=@zhtbm)
if(@@error!=0 or @@rowcount!=1)
begin
rollback tran tr_dkzq
return 13
end

update T_Tjkzht set
bm=convert(char(4),datepart(yy,getdate()))+substring(@zhtbm,5,9)+stuff("0000",5-len(ltrim(rtrim(@xh))),len(ltrim(rtrim(@xh))),ltrim(rtrim(@xh)))+"00",
ybm=@zhtbm,
jkqx=@S_dkqx,
jkll=@jzyll*(1+@fdl),
mqhkr=@mqhkr
where bm=@zhtbm
if (@@error!=0 or @@rowcount!=1)
begin
rollback tran tr_dkzq
return 14
end

/*补充从合同*/

select @khh=jkrbm from T_jkcht
where ((bm=@chtbm and ybm is null) or (bm!=@chtbm and ybm=@chtbm)) and htzzr<@mqhkr
if (@@error!=0 or @@rowcount>1)
begin
rollback tran tr_dkzq
return 15
end
select @khh=jkrbm from T_jkcht
where ((bm=@chtbm and ybm is null) or (bm!=@chtbm and ybm=@chtbm)) and htzzr<@mqhkr
if (@@error=0 and @@rowcount=1)
begin
insert T_Tjkcht select * from T_jkcht
where (bm=@chtbm and ybm is null) or (bm!=@chtbm and ybm=@chtbm)

if (@@error!=0 or @@rowcount!=1)
begin
rollback tran tr_dkzq
return 16
end

select @xh=xygbm from T_bmxd where bm="T_jkcht" and xm=@jgh
if (@@error!=0 or @@rowcount!=1)
begin
rollback tran tr_dkzq
return 17
end
update T_bmxd set xygbm=stuff("000000",7-len(ltrim(rtrim(convert(char(6),convert(int,@xh)+1)))),len(ltrim(rtrim(convert(char(6),convert(int,@xh)+1)))),ltrim(rtrim(convert(char(6),convert(int,@xh)+1))))
where bm="T_jkcht" and xm=@jgh
if(@@error!=0 or @@rowcount!=1)
begin
rollback tran tr_dkzq
return 18
end
update T_Tjkcht set
bm=convert(char(4),datepart(yy,getdate()))+substring(@chtbm,5,7)+stuff("000000",7-len(ltrim(rtrim(@xh))),len(ltrim(rtrim(@xh))),ltrim(rtrim(@xh)))+"00",
ybm=@chtbm,
htzzr=@mqhkr
where bm=@chtbm
if (@@error!=0 or @@rowcount!=1)
begin
rollback tran tr_dkzq
return 19
end
end
end

/*
insert T_jytmp values(
jyrq int, 交易日期(YYYYMMDD),jyzl char(2), 交易种类,jgh int,机构号,khh int,客户号,hth char(20) NULL,合同号,
zh char(20) NULL,贷款帐号,dkzl int,贷款种类,kph int NULL,卡片号,hm char(30),户名,
qsrq int,起始日期(YYYYMMDD),dqrq int,到期日期(YYYYMMDD),qx int,期限(以月为单位),
je decimal(16,2), 金额(以元为单位,整数14位,小数2位),ll int,年利率(以%为单位,整数2位,小数4位),
yqll int,逾期利率(以%为单位,整数2位,小数4位),hkfs char(3),还款方式,bz int,标志,mm char(6),密码,
dkxt char(1),代扣系统,dkzh char(20),代扣帐号,ffxt char(1),发放系统,ffzh char(20),发放帐号,
clbz int,处理标志,czy int,操作员,bz1 char(20),备注1,bz2 int,备注2,bz3 decimal(16,2)),备注3
*/

insert T_jytmp values(convert(int,(datepart(yy,getdate())*100+datepart(mm,getdate()))*100+datepart(dd,getdate())),"13",
convert(int,@jgh),convert(int,@khh),@xjj,
@dkzh,convert(int,@dkzl),NULL,@hm,convert(int,@qsrq),convert(int,@mqhkr),convert(int,@S_dkqx),
@je,convert(int,@jzyll*@fdl*1200),convert(int,@yqll*10000),@hkfs,0,@mm,NULL,@kkzh,NULL,
@ffzh,1,convert(int,@czyh),NULL,NULL,NULL)

if(@@error!=0 or @@rowcount!=1)
begin
rollback tran tr_dkzq
return 20
end

commit tran tr_dkzq
return 0
go


多谢!
 
程序太长,估计其中运行出了问题
 
你设置result参数的类型为ptresult了吗?
 
>> Caption := StoredProc1.ParamByName('Result').AsString;
试一试:
Caption := StoredProc1.ParamByName('RETURN_VALUE').AsString;
or:
Caption := StoredProc1.FieldByName('RETURN_VALUE').AsString;

或者直接用ADO连接,肯定没问题!
 
是用ptResult,就是不知道为什么不能返回正确的值?
 
hujunyi:如果你还要继续讨论请定期提前你的帖子,如果不想继续讨论请结束帖子。
 
在取结果值之前,加一句
StoredProc1.GetResults;

这个方法可以强制Sybase或MSSQL存储过程返回结果。在其他Server上,结果是自动返回
到客户端,但是Sybase和MSSQL server 在大部分情况不能自动返回。
 
可是当我使用StoredProc1.GetResults后返回的仍是 0 !
 
zfs2000,你好
对这个问题,我不敢说是数据库或Delphi本身的问题,但只要在写存储过程时注意几点就
没有问题了。
反是在select Columns from Table 的语句的地方,如果你要取回查询的字段值时就改写
这样:select @ColoumValue1 = Column1, ... from Table
如果仅仅为知道返回的行数,就写成select count(*) from Table

再试试!
 
除了StoredProc1.GetResults;之外,
把取得结果的语句改为:
Caption := StoredProc1.ParamByName('RETURN_VALUE').AsString;
或者
Caption := StoredProc1.Params[0].AsString;
再试一下。
 
hujunyi,您好:

关于Delphi4.0 中使用StoredProc构件调用MS SQLServer的存储过程,总是返回0的问题,
我已找到一个解决办法:简单得一塌糊涂,就是安装Delphi5.0,然后使用ADO StoredProc
保证你能搞定,不信可试一试。
 
请尽快结束你的贴子,否则我可要痛下杀手了^-^
 
接受答案了.
 
后退
顶部