只写其中一段
declare @sql varchar(8000)
while (@@fetch_status=0)
begin
    select @sql='alter table ['+@bm+'] add constraint pk_code primary key (code)
    exec(@sql)
    fetch next into @bm
end

解决方案 »

  1.   

    CREATE PROCEDURE usp_create_pk 
    AS
    declare @bm varchar(7)
    declare cur_dmb cursor for select dmbname from _dmbnote
    open cur_dmb
    fetch cur_dmb into @bm
    if (@@fetch_status<>0)
    begin
    close cyr_dmb
    deallocate cur_dmb
    return
    end
    set nocount on
    while (@@fetch_status=0)
    begin
    exec('alter table '+@bm+' add constraint pk_code primary key (code)')
    fetch next into @bm
    end
    close cur_dmb
    deallocate cur_dmb
    return
      

  2.   

    CREATE PROCEDURE usp_create_pk 
    AS
    declare @bm varchar(7)
    declare cur_dmb cursor for select dmbname from _dmbnote
    open cur_dmb
    fetch cur_dmb into @bm
    if (@@fetch_status<>0)
    begin
    close cyr_dmb
    deallocate cur_dmb
    return
    end
    set nocount on
    while (@@fetch_status=0)
    begin
        exec('alter table '+@bm+' add constraint pk_code_'+@bm+' primary key (code)')
    --(主键名也不能与其它表的主键名重复)
    -- alter table XXX add constraint pk_code primary key (code)
    fetch next into @bm
    end
    close cur_dmb
    deallocate cur_dmb
    return