sql2000的简单问题,会者请进! (100分)

Y

yypeng

Unregistered / Unconfirmed
GUEST, unregistred user!
use storemanager
go


if exists (select name from sysobjects where name='statetable1')
begin
drop table statetable1
end

create table statetable1(材料编号 varchar(10) not null,材料名称 varchar(20),单位 varchar(4),消耗量 numeric,年份 varchar(4),月份 varchar(2),金额 numeric,
占水泥制造成本 numeric,矿山分厂消耗 numeric,烧成分厂消耗 numeric,制成分厂消耗 numeric,干法分厂消耗 numeric,其他消耗 numeric,调拔 numeric)

declare @stateyear varchar(4),@statemonth varchar(2),@ProductCost varchar(10),@ProductOutPut varchar(10),@haverecord integer,@havepriorYMSC varchar(10),@realcount float,@haveRecord1 integer
select @statemonth='2' ,@stateyear='2002',@productCost='184.3',@productOutPut='123'
/*------------------------------------------------------------------------------------------------
问题是出在赋值上,如果将上一行中的 @stateyear='2002'去掉,程序将会运行,但没有结果
但我明明声明了@stateyear,而系统却提示
“服务器: 消息 137,级别 15,状态 2,行 1
必须声明变量 '@stateyear'。
希望大家帮帮忙,我实在是没哲了!
-------------------------------------------------------------------------------------
*/

select @haverecord=(select count(*) from 原燃材料收支存统计月报表 where 月份=@statemonth and 年份=@stateyear)
select @haverecord1=(select count(*) from 公司各单位物料消耗统计表 where 月份=@statemonth and 年份=@stateyear)

if @haverecord>=1 /*如果“原燃材料收支存统计月报表”中有统计月的记录,则继续,否则不进行统计*/
begin
if @haverecord1>=1 delete from 公司各单位物料消耗统计表 where 年份=@stateyear and 月份=@statemonth /*如果在“物料消耗统计表中存在本月记录,则将其全部删除*/

insert into statetable1(材料编号,材料名称,单位,年份,月份)
execute('select 材料编号, 材料名称,计量单位,年份,月份 from 原燃材料收支存统计月报表 where 年份=@stateyear and 月份=@statemonth')

update statetable1
set 金额=(select sum(计划总价) from 领料单 where year(领用日期)=@stateyear and month(领用日期)=@statemonth)

update statetable1
set 消耗量=(select 本月发出 from 原燃材料收支存统计月报表 where 年份=@stateyear and 月份=@statemonth)

update statetable1
set 占水泥制造成本=(金额/(convert(float,@productCost)*convert(float,@productOutPut))*100)

update statetable1
set 矿山分厂消耗=(select sum(实发数量) from 领料单 where 领用部门='矿山分厂' and year(领用日期)=@stateyear and month(领用日期)=@statemonth)
update statetable1
set 烧成分厂消耗=(select sum(实发数量) from 领料单 where 领用部门='烧成分厂' and year(领用日期)=@stateyear and month(领用日期)=@statemonth )
update statetable1
set 制成分厂消耗=(select sum(实发数量) from 领料单 where 领用部门='制成分厂' and year(领用日期)=@stateyear and month(领用日期)=@statemonth )
update statetable1
set 干法分厂消耗=(select sum(实发数量) from 领料单 where 领用部门='干法分厂' and year(领用日期)=@stateyear and month(领用日期)=@statemonth )
update statetable1
set 其他消耗=(select sum(其他) from 原燃材料收支存统计月报表 where 年份=@stateyear and 月份=@statemonth )
update statetable1
set 调拔=(select sum(调拔) from 原燃材料收支存统计月报表 where 年份=@stateyear and 月份=@statemonth )

/*insert into 公司各单位物料消耗统计表 */
select * from statetable1

end
 
是不是打错了,换个变量名试试!
 
再DB2中使用Set赋值时只能针对一个变量
再SQL中Select???
 
问题出在这里
insert into statetable1(材料编号,材料名称,单位,年份,月份)
execute('select 材料编号, 材料名称,计量单位,年份,月份 from 原燃材料收支存统计月报表 where 年份=@stateyear and 月份=@statemonth')

execute('select 材料编号, 材料名称,计量单位,年份,月份 from 原燃材料收支存统计月报表 where 年份='''+@stateyear+''' and 月份='''+@statemonth+'''')

 
十分感谢,问题解决了。分全给你
 
顶部