用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - sql代码库

table类型和游标的使用

2014-02-13 作者: 举报

[sql]代码库

/*
create type BOMVersion as table(
	pcode varchar(18),
	[month] int,
	code varchar(18),
	unit varchar(3),
	quantity decimal(18,3)
)
GO
*/

CREATE PROCEDURE BOMCompare
(
  @year1 as int,
  @month1 as int,
  @year2 as int,
  @month2 as int,
  @matnr as varchar(18)
)
AS
BEGIN
  DECLARE @bom1 BOMVersion
  DECLARE @bom2 BOMVersion
  DECLARE @pcode varchar(18)
  DECLARE @month int,
  DECLARE @code varchar(18),
  DECLARE @unit varchar(3),
  DECLARE @quan decimal(18,3)

  insert into #bomcompareresult
  SELECT
    bom1.*,
    NULL as month2,
    NULL as code2,
    NULL as unit2,
    NULL as quantity2
  FROM @bom2 as bom1
  
  DECLARE i CURSOR FOR SELECT * FROM @bom1 as bom1
  OPEN i
  
  FETCH NEXT FROM i INTO @pcode, @month, @code, @unit, @quan
  
  WHILE @@FETCH_STATUS = 0
  BEGIN
    update #bomcompareresult SET month2=@month, code2=@code, unit2=@unit, quantity2=@quan
    WHERE pcode = @pcode and code = @code
    
    if(@@ROWCOUNT = 0)
    BEGIN
      INSERT INTO #bomcompareresult values (@pcode, null, null, null, null, @month, @code, @unit, @quan)
    END
    
	FETCH NEXT FROM i INTO @pcode, @month, @code, @unit, @quan
  END
  
  SELECT * FROM #bomcompareresult ORDER pcode
END


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...