被一个简单的存储过程卡住了!分不多,大家宽容一点咯 偶是MM^_^(20分)

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

happypx

Unregistered / Unconfirmed
GUEST, unregistred user!
问了好多人也试几好多,都没有办法解决!<br>create procedure showTable2 @sdate varchar(20)<br>as<br><br>declare @sql varchar(8000) <br> set @sql='select dwbm as 编号,dwmc as 乡镇,(sum(应发工资)+sum(单位医疗保险)+sum(单位住房))as 合计,' <br>select @sql=@sql+'sum(case yhmc when '' &nbsp;'+yhmc+''' then 应发工资 else 0 end )as &nbsp;'''+yhmc+''','<br><br>from &nbsp; (select distinct yhmc from &nbsp;(select a.dwbm,a.dwmc,d.yhmc,sum(f.yfx)as 应发工资,<br>sum(f.b015) as 个人医疗保险, &nbsp;sum(f.b015/0.02*0.115) as 单位医疗保险, <br> &nbsp;sum(f.b014) as 单位住房,sum(f.b014) as 个人住房 &nbsp;from &nbsp;tbldwxx a,tblrsxx b,<br>tblkm c,tblyhxx d,tblgzsj f &nbsp;where a.dwbm=b.dwbm &nbsp;and b.kmbm=c.kmbm <br> and b.yhbm=d.yhbm and f.rybm=b.rybm &nbsp;and a.dwbm in(091,092,093,094,095,096) and f.rq=@sdate<br> group by a.dwbm,a.dwmc,d.yhmc )as bb )cc <br><br> select @sql=left(@sql,len(@sql)-1) +',sum(单位医疗保险)as 农行社保专户,<br>sum(单位住房)as 建行住房公积金, &nbsp;sum(应发工资)as 工资,sum(个人医疗保险)as 个人医疗保险,<br>sum(单位医疗保险)单位医疗保险, &nbsp;sum(个人住房)as 个人住房,sum(单位住房)as 单位住房<br><br> &nbsp;from &nbsp;(select a.dwbm,a.dwmc,d.yhmc,cast(sum(f.yfx)as numeric(10,2))as 应发工资,<br> &nbsp;cast(sum(f.b015)as numeric(10,2)) as 个人医疗保险, <br> cast(sum(f.b015/0.02*0.115)as numeric(10,2)) as 单位医疗保险,<br> &nbsp;cast(sum(f.b014)as numeric(10,2)) as 单位住房,<br>cast(sum(f.b014)as numeric(10,2))as 个人住房 &nbsp;from <br> tbldwxx a,tblrsxx b,tblkm c,tblyhxx d,tblgzsj f <br> where a.dwbm=b.dwbm &nbsp;and b.kmbm=c.kmbm &nbsp;and b.yhbm=d.yhbm <br>and f.rybm=b.rybm &nbsp;and a.dwbm in(091,092,093,094,095,096 )and f.rq=@sdate <br>group by a.dwbm,a.dwmc,d.yhmc ) &nbsp;aa &nbsp;<br>group by aa.dwbm,aa.dwmc order by dwbm'<br>exec(@sql)<br><br>本来一个是SQL语句的,但里面有变量和字符串引号太多了,放到DELPHI里我不会,所以想用存储过程。可是加了一个变量 @sdate调用运行总说没有申明这个变量。<br>中间的SQL语句你们可以不用看(主要是交叉表),那里是可以通过的,如果存储过程没有 @sdate这个变量,调用没有错,现在我怀疑是不是因为我的SQL里定义了一个变量,而存储过程外面也定义一个,是不是不给这样使用啊?大家帮帮忙咯!
 
第一次发贴,以为只可以用专家分!后悔放分少,可是我想修改不给改分数!下次给多点分吧!
 
帮忙一下啊!
 
我估计可能是你delphi代码中调用参数有点问题。你可以这样测试:<br>1.写我带参数的存储过程,然后在查询分析器中调用,像<br>showTable2 '2006-10-01'看看存储过程本身有无问题<br>2.如果第一步有问题,修改存储过程,如果没有问题,肯定是你代码调用有问题。你如果有storeprocedure控件或其他控件的parameters传递参数,可以重置一下试试,就是重新刷新一下参数设置。
 
create procedure showTable2 @sdate varchar(20)<br>as<br><br>declare @sql varchar(8000) <br> set @sql='select dwbm as 编号,dwmc as 乡镇,(sum(应发工资)+sum(单位医疗保险)+sum(单位住房))as 合计,' <br>select @sql=@sql+'sum(case yhmc when '' &nbsp;'+yhmc+''' then 应发工资 else 0 end )as &nbsp;'''+yhmc+''','<br><br>from &nbsp; (select distinct yhmc from &nbsp;(select a.dwbm,a.dwmc,d.yhmc,sum(f.yfx)as 应发工资,<br>sum(f.b015) as 个人医疗保险, &nbsp;sum(f.b015/0.02*0.115) as 单位医疗保险, <br> &nbsp;sum(f.b014) as 单位住房,sum(f.b014) as 个人住房 &nbsp;from &nbsp;tbldwxx a,tblrsxx b,<br>tblkm c,tblyhxx d,tblgzsj f &nbsp;where a.dwbm=b.dwbm &nbsp;and b.kmbm=c.kmbm <br> and b.yhbm=d.yhbm and f.rybm=b.rybm &nbsp;and a.dwbm in(091,092,093,094,095,096) and f.rq=@sdate<br> group by a.dwbm,a.dwmc,d.yhmc )as bb )cc <br><br> select @sql=left(@sql,len(@sql)-1) +',sum(单位医疗保险)as 农行社保专户,<br>sum(单位住房)as 建行住房公积金, &nbsp;sum(应发工资)as 工资,sum(个人医疗保险)as 个人医疗保险,<br>sum(单位医疗保险)单位医疗保险, &nbsp;sum(个人住房)as 个人住房,sum(单位住房)as 单位住房<br><br> &nbsp;from &nbsp;(select a.dwbm,a.dwmc,d.yhmc,cast(sum(f.yfx)as numeric(10,2))as 应发工资,<br> &nbsp;cast(sum(f.b015)as numeric(10,2)) as 个人医疗保险, <br> cast(sum(f.b015/0.02*0.115)as numeric(10,2)) as 单位医疗保险,<br> &nbsp;cast(sum(f.b014)as numeric(10,2)) as 单位住房,<br>cast(sum(f.b014)as numeric(10,2))as 个人住房 &nbsp;from <br> tbldwxx a,tblrsxx b,tblkm c,tblyhxx d,tblgzsj f <br> where a.dwbm=b.dwbm &nbsp;and b.kmbm=c.kmbm &nbsp;and b.yhbm=d.yhbm <br>and f.rybm=b.rybm &nbsp;and a.dwbm in(091,092,093,094,095,096 )and f.rq=@sdate [:(!]<br>group by a.dwbm,a.dwmc,d.yhmc ) &nbsp;aa &nbsp;<br>group by aa.dwbm,aa.dwmc order by dwbm'<br>exec(@sql)<br><br>在这个存储过程中引用变量@sdate有问题,你注意一下第二个@sdate是放在一个字符串中可以将<br>and f.rybm=b.rybm &nbsp;and a.dwbm in(091,092,093,094,095,096 )and f.rq=@sdate [:(!]<br>group by a.dwbm,a.dwmc,d.yhmc ) &nbsp;aa &nbsp;<br>group by aa.dwbm,aa.dwmc order by dwbm'<br>改为<br>and f.rybm=b.rybm &nbsp;and a.dwbm in(091,092,093,094,095,096 )and f.rq='[8D]+@sdate+[8D]<br>'group by a.dwbm,a.dwmc,d.yhmc ) &nbsp;aa &nbsp;<br>group by aa.dwbm,aa.dwmc order by dwbm'
 
谢谢楼上GG<br>1、这样的方式 showTable2 '2006-10-01'我昨天晚上问别人也试过了,还有带参数也试过了,都是提示没有申明变量@sdate<br>2、我刚也按你说的showTable2 '2006-10-01' 到OPEN时复制到分析器里 也是提示‘必须声明变量 '@sdate'。’<br>估计是不可以这样使用变量了!<br>3、看来只能想办法直接把这堆语句放到DELPHI了!大家有什么方法
 
To:zzh0918<br> 我用了你的方法,但提示‘列名 '8D' 无效。’!<br>这个[8D]是什么意思呢
 
我也试了加上连接符,也是不行这个8D无效<br>+[8D]+@sdate+[8D]+
 
改为<br>and f.rybm=b.rybm &nbsp;and a.dwbm in(091,092,093,094,095,096 )and f.rq='+@sdate+'group by a.dwbm,a.dwmc,d.yhmc ) &nbsp;aa &nbsp;<br>group by aa.dwbm,aa.dwmc order by dwbm'
 
其实你将代码放到分析器中一看就明白了:<br>and f.rybm=b.rybm &nbsp;and a.dwbm in(091,092,093,094,095,096 )and f.rq=@sdate <br>是在字符串中,你应该改为<br>and f.rybm=b.rybm &nbsp;and a.dwbm in(091,092,093,094,095,096 )and f.rq='+@sdate+' <br>要不然你生成的sql语句了有@sdate,而它应该是一个具体的值传递进来的。
 
[8D]是加的论坛的表情符号
 
还有,你的这块代码:<br>from &nbsp; (select distinct yhmc from &nbsp;(select a.dwbm,a.dwmc,d.yhmc,sum(f.yfx)as 应发工资,<br>sum(f.b015) as 个人医疗保险, &nbsp;sum(f.b015/0.02*0.115) as 单位医疗保险, <br> &nbsp;sum(f.b014) as 单位住房,sum(f.b014) as 个人住房 &nbsp;from &nbsp;tbldwxx a,tblrsxx b,<br>tblkm c,tblyhxx d,tblgzsj f &nbsp;where a.dwbm=b.dwbm &nbsp;and b.kmbm=c.kmbm <br> and b.yhbm=d.yhbm and f.rybm=b.rybm &nbsp;and a.dwbm in(091,092,093,094,095,096) and f.rq=@sdate<br> group by a.dwbm,a.dwmc,d.yhmc )as bb )cc <br><br>是不是也应放在串内?
 
To:zzh0918<br>你说对了问题是在第2个变量@sdate,我把这个去掉留第一个运行可以!<br>现在是在字符串中怎么调用这个变量呢!呵呵
 
你的第一段也应该是在字符串中,在调用存储过程时,传递进取的@sdate值就已经替换了@sdate,如传递进去@sdate=‘2006-11-01’则<br>and f.rybm=b.rybm &nbsp;and a.dwbm in(091,092,093,094,095,096 )and f.rq='+@sdate+'group by a.dwbm,a.dwmc,d.yhmc ) &nbsp;aa &nbsp;<br>group by aa.dwbm,aa.dwmc order by dwbm' &nbsp;<br><br>变为<br>and f.rybm=b.rybm &nbsp;and a.dwbm in(091,092,093,094,095,096 )and f.rq=2006-11-01 group by a.dwbm,a.dwmc,d.yhmc ) &nbsp;aa &nbsp;<br>group by aa.dwbm,aa.dwmc order by dwbm'
 
解决了,谢谢各位!<br>把第2个变量@sdate 改为<br>and f.rybm=b.rybm &nbsp;and a.dwbm in(091,092,093,094,095,096 )and f.rq='''+@sdate+''' 就可以了,其他不用动!感激ING!
 
把字符串搞混了,是不是女生搞程序都和我一样糊涂啊!第一段没有问题的,SQL语句我是调试过的,我是一层层实现到最后一层无法解决滴!呵呵 ^_^
 
多人接受答案了。
 
后退
顶部