try:
insert into yhye(CODE,iperiod,ccode_Name,dbill_date,md,mc,drye) 
select Y.code,b.iperiod,a.ccode_Name,b.dbill_date,SUM(b.md),SUM(b.mc),SUM(b.md-b.mc)  
FROM 
  ufdata_888_2004..code a,ufdata_888_2004..GL_accvouch b ,yhcode Y
where 
  a.ccode *=b.ccode and b.iperiod >=1 and b.iperiod <=12 and a.ccode like '10020%' and Y.ccode_name=a.ccode_Name

解决方案 »

  1.   

    1.得到编号可以这样
    --创建测试表
    CREATE TABLE yhcode
    (
    i_id                    INT IDENTITY(1,1) NOT NULL,
    code                   NVARCHAR(15)   not NULL,
    ccode_name         NVARCHAR(100)
    ) go--drop trigger t_insert
    --创建生成流水号的触发器
    create trigger t_insert on yhcode
    INSTEAD OF insert
    as
    declare @id varchar(18),@id1 int
    select * into #tb from inserted
    select @id=max(code) from yhcode
    if @id is null
    set @id1=0
    else
    Set @id1=cast(@id as int)update #tb set @id1=@id1+1
    ,code=right('0000'+cast(@id1 as varchar),4)
    insert into yhcode(code,ccode_name) select code,ccode_name from #tb
    go
    --插入数据,进行测试
    insert into yhcode(code,ccode_name)
    select '','aa'
    union all select '' ,'bb'
    union all select '','cc'不过要注意code字段,写入时要这样
    insert into yhcode(code,ccode_name)
    select '','aa' 才不会出错或是改code字段允许为空
    -----------------------------------
    否则出code字段不允许为空错误