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 '' '+yhmc+''' then 应发工资 else 0 end )as '''+yhmc+''','<br><br>from (select distinct yhmc from (select a.dwbm,a.dwmc,d.yhmc,sum(f.yfx)as 应发工资,<br>sum(f.b015) as 个人医疗保险, sum(f.b015/0.02*0.115) as 单位医疗保险, <br> sum(f.b014) as 单位住房,sum(f.b014) as 个人住房 from tbldwxx a,tblrsxx b,<br>tblkm c,tblyhxx d,tblgzsj f where a.dwbm=b.dwbm and b.kmbm=c.kmbm <br> and b.yhbm=d.yhbm and f.rybm=b.rybm 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 建行住房公积金, sum(应发工资)as 工资,sum(个人医疗保险)as 个人医疗保险,<br>sum(单位医疗保险)单位医疗保险, sum(个人住房)as 个人住房,sum(单位住房)as 单位住房<br><br> from (select a.dwbm,a.dwmc,d.yhmc,cast(sum(f.yfx)as numeric(10,2))as 应发工资,<br> cast(sum(f.b015)as numeric(10,2)) as 个人医疗保险, <br> cast(sum(f.b015/0.02*0.115)as numeric(10,2)) as 单位医疗保险,<br> cast(sum(f.b014)as numeric(10,2)) as 单位住房,<br>cast(sum(f.b014)as numeric(10,2))as 个人住房 from <br> tbldwxx a,tblrsxx b,tblkm c,tblyhxx d,tblgzsj f <br> where a.dwbm=b.dwbm and b.kmbm=c.kmbm and b.yhbm=d.yhbm <br>and f.rybm=b.rybm and a.dwbm in(091,092,093,094,095,096 )and f.rq=@sdate <br>group by a.dwbm,a.dwmc,d.yhmc ) aa <br>group by aa.dwbm,aa.dwmc order by dwbm'<br>exec(@sql)<br><br>本来一个是SQL语句的,但里面有变量和字符串引号太多了,放到DELPHI里我不会,所以想用存储过程。可是加了一个变量 @sdate调用运行总说没有申明这个变量。<br>中间的SQL语句你们可以不用看(主要是交叉表),那里是可以通过的,如果存储过程没有 @sdate这个变量,调用没有错,现在我怀疑是不是因为我的SQL里定义了一个变量,而存储过程外面也定义一个,是不是不给这样使用啊?大家帮帮忙咯!