delphi调用存储过程(100)

  • 主题发起人 主题发起人 xin92002
  • 开始时间 开始时间
X

xin92002

Unregistered / Unconfirmed
GUEST, unregistred user!
1.存储过程(pro_del) 测试成功(如果需要修改请指点)if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[pro_del]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[pro_del]GOSET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GOcreate procedure pro_del @pj_noqishi int, @pj_nozhongzhi int as begin delete from tb_pj0 where pj_no>=@pj_noqishi and pj_no<=@pj_nozhongzhi delete from tb_pj9 where pj_no>=@pj_noqishi and pj_no<=@pj_nozhongzhi delete from tb_pj_mx where pj_no>=@pj_noqishi and pj_no<=@pj_nozhongzhi delete from tb_pj_mx0 where pj_no>=@pj_noqishi and pj_no<=@pj_nozhongzhi endGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO2.利用delphi7调用该存储过程 adoconnection adostoreproc连接MS SQL数据库 (一个button按钮用于执行操作。edit1.edit2 用于给存储过程参数@pj_noqishi,@pj_nozhongzhi赋值)这个存储过程调用具体应该如何调用以及异常处理·(存储过程执行失败,存储过程调用失败)3.是否可以动态生成MS SQL 存储过程 具体代码小弟刚刚学习delphi+MS SQl 数据库的问题 还请大家 指教
 
tmpsql := 'pro_del @pj_noqishi='+''''+trim(edit1.text)+''''+',@pj_nozhongzhi='+''''+trim(edit2.text)+'''';然后你找个adoquery什么的执行这个sql语句就行异常处理的话,直接用adoconnection 的rollbacktrans就行了格式如下 if adoconnection.InTransaction then adoconnection.RollbackTrans; adoconnection.BeginTrans; //====进行存储过程调用 try adoconnection.CommitTrans; except adoconnection.RollbackTrans; showmessage('数据处理失败,请检查数据库连接或参数是否正确!'); end;
 
至于生成动态的存储过程,因为话题太过广泛,笨蛋就发个自己写的例子,看看LZ能不能有点收获CREATE PROCEDURE csz_gzb@gzyear char(4),@gzmonth char(2),@ztid char(2)ASdeclare @tmpsql varchar(8000)declare @xmid varchar(20)declare @csz decimal(18,6)set @xmid= ''set @csz=0declare gsjs cursor for select xmid,csz from infogzzt where ztid=@ztidopen gsjsfetch next from gsjs into @xmid,@cszwhile @@fetch_status = 0 begin set @tmpsql = 'update infogzb set '+cast(@xmid as varchar(20))+ ' = '+cast(@csz as varchar(20))+' where ztid='+''''+@ztid +''''+' and gzyear='+''''+@gzyear+''''+' and gzmonth='+''''+@gzmonth+''''+' and zjmark=0' exec(@tmpsql) fetch next from gsjs into @xmid,@csz endclose gsjsdeallocate gsjsGO通过游标从一个表读取参数,然后执行sql语句至于LZ的存储过程,没去细看,既然通过了 问题不大吧PS,笨蛋也是水平有限 若有说错 请指正
 
从执行··到异常处理 能具体 代码么··确实摸不到头脑···[:(]
 
给个简单的:create proc MyABC @a int ,@b int, @c int output as set @c=@a*@b在SQL中执行:declare @a int,@b int,@c int set @a=250set @b=40exec MyABC @a,@b,@c output --注意要添加output 关键字!select @c在Delphi中调用: ADOQuery1.Close; ADOQuery1.SQL.Clear; ADOQuery1.SQL.Add(‘execute MyABC :a,:b,:c output‘);//也要添加output 关键字! ADOQuery1.Parameters.ParamByName(‘a‘).Value:=250; ADOQuery1.Parameters.ParamByName(‘b‘).Value:=40; ADOQuery1.ExecSQL; showmessage(ADOQuery1.Parameters.ParamByName(‘c‘).Value);
 
多人接受答案了。
 
笨蛋兄``` 罗兄 各50 `` 分多分少 希望你们不要 介意``小弟非常感谢你们``[:)]
 
后退
顶部