cursor你看看,没有测试过。declare @y_heth varchar(20),@y_chanpbh varchar(20)
declare myCursor cursor for SELECT A.y_heth, A.y_chanpbh
FROM smi_het A 
WHERE (not EXISTS
          (SELECT 1
         FROM tr_dind B
         WHERE A.y_heth = B.kehdd)) open myCursor
fetch next from myCursor into @y_heth,@y_chanpbh
while @@FETCH_STATUS = 0
   begin    
exec add_tr_dd @y_heth,@y_chanpbh
fetch next from myCursor into @y_heth,@y_chanpbh
   end
close myCursor
deallocate myCursor

解决方案 »

  1.   

    --用游标.
    declare tb cursor for
    SELECT A.y_heth, A.y_chanpbh
    FROM smi_het A 
    WHERE (not EXISTS
              (SELECT 1
             FROM tr_dind B
             WHERE A.y_heth = B.kehdd))declare @a varchar(5),@b varchar(8)
    open tb
    fetch next from tb into @a,@b
    while @@fetch_status=0
    begin
        exec add_tr_dd @a,@b
        fetch next from tb into @a,@b
    end
    close tb
    deallocate tb
    一条SQL语句及返回值如下:
      

  2.   

    declare  cursor_insert cursor for SELECT A.y_heth, A.y_chanpbh FROM smi_het A WHERE (not EXISTS  (SELECT 1 FROM tr_dind B WHERE A.y_heth = B.kehdd))
    declare @a varchar(100),@b varchar(100)
    open cursor_insert
    fetch cursor_insert into @a,@b
    while @@fetch_status=0
    begin
      exec add_tr_dd @a,@b
      fetch cursor_insert into @a,@b
    end
    close cursor_insert
    deallocate cursor_insert
      

  3.   

    用临时表DECLARE @i INT, @cnt INT
    declare @a varchar(100),@b varchar(100)SELECT IDENTITY(int, 1, 1) as id, A.y_heth, A.y_chanpbh
    INTO #tmp
    FROM smi_het A 
    WHERE (not EXISTS
              (SELECT 1
             FROM tr_dind B
             WHERE A.y_heth = B.kehdd))
    SET @cnt = @@ROWCOUNTSET @i = 1
    WHILE @i <= @cnt
    BEGIN
      SELECT @a = y_heth, @b = y_chanpbh FROM #tmp WHERE id = @i
      exec add_tr_dd @a, @b
    ENDDROP TABLE #tmp