注册 登录
编程论坛 SQL Server论坛

SQL SERVER 存储过程

遗忘的爱 发布于 2013-01-18 10:21, 593 次点击
程序代码:
DECLARE @sql VARCHAR(1000)
SET @sql='SELECT Item_id as '+'周数'
select @sql=@sql+
',max(case [sd] when '''+sd+''' then jhccl else 0 end)['+sd+']'
from(select distinct sd from MRP_Plan where sd<>'当前') as a
SET @sql=@sql+'from MRP_Plan  WHERE Item_id in (select distinct Item_id from MRP_Plan where Item_id in
(select Item_id from Production where work_cen_name=
''中心1''))and MRP_ID=''MR002'' group  by Item_id  '
exec(@sql)
有没有办法把上面的代码改成通用的存储过程 work_cen_name和MRP_id作为输入参数?
1 回复
#2
遗忘的爱2013-01-18 11:07
程序代码:
Create procedure [dbo].[Work_MRP]
@id varchar(20),@name varchar(20)
as
DECLARE @@sql VARCHAR(1000)
SET @@sql='SELECT Item_id as '+'周数'select @@sql=@@sql+',max(case [sd] when '''+sd+''' then jhccl else 0 end)['+sd+']'
from(select distinct sd from MRP_Plan where sd<>'当前') as a
SET @@sql=@@sql+'from MRP_Plan  where Item_id in (select distinct Item_id from MRP_Plan where Item_id in
(select Item_id from Production where work_cen_name=
'''+@name+'''))and MRP_id='''+@id+''' group  by Item_id  '
exec (@@sql)
go
1