参照昨天一个朋友给的代码,可是出错了,请指教! insert into InterestReceive(CIFNm,RepayType,RepayAmount) SELECT CIFNmFROM ( select CIFNm,ContractNm,LoanBlankDate,PeridNm,'PeriodCapital' as RepayType, PeriodCapital as RepayAmount from InterestList union all select CIFNm,ContractNm,LoanBlankDate,PeridNm,'PeriodInterst' as RepayType,PeriodInterst as RepayAmount from InterestList union all select CIFNm,ContractNm,LoanBlankDate,PeridNm,'PeriodMangeCharge' as RepayType,PeriodMangeCharge as RepayAmount from InterestList ) T where LoanBlankDate=convert(varchar(10),GETDATE(),120) and not exists (select 1 from InterestReceive b where a.CIFNm=b.CIFNm and a.RepayType=b.RepayType and a.RepayAmount=b.RepayAmount)
------解决方案-------------------- insert into InterestReceive(CIFNm,RepayType,RepayAmount) SELECT CIFNm,RepayType,RepayAmount from(....)
------解决方案--------------------
------解决方案-------------------- SELECT CIFNmFROM -- 改成 SELECT CIFNm,RepayType,RepayAmount FROM -- 即查询的字段必须和插入表的字段一一匹配。
------解决方案--------------------
SQL code
insert into InterestReceive(CIFNm,RepayType,RepayAmount)
SELECT CIFNm,RepayType,RepayAmount
FROM
(
select CIFNm,ContractNm,LoanBlankDate,PeridNm,'PeriodCapital' as RepayType, PeriodCapital as RepayAmount from InterestList
union all
select CIFNm,ContractNm,LoanBlankDate,PeridNm,'PeriodInterst' as RepayType,PeriodInterst as RepayAmount from InterestList
union all
select CIFNm,ContractNm,LoanBlankDate,PeridNm,'PeriodMangeCharge' as RepayType,PeriodMangeCharge as RepayAmount from InterestList
) AS a
where LoanBlankDate = convert(varchar(10),GETDATE(),120)
and not exists (select 1 from InterestReceive AS b
where a.CIFNm=b.CIFNm and a.RepayType=b.RepayType and a.RepayAmount=b.RepayAmount)