日期:2014-05-18  浏览次数:20409 次

参照昨天一个朋友给的代码,可是出错了,请指教!
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)

消息 156,级别 15,状态 1,第 7 行
关键字 'select' 附近有语法错误。
消息 102,级别 15,状态 1,第 12 行
')' 附近有语法错误。

代码是照搬来的,我也读通了,但是就是要出错,怎么的呢

------解决方案--------------------
insert into InterestReceive(CIFNm,RepayType,RepayAmount)
SELECT CIFNm,RepayType,RepayAmount
from(....)


------解决方案--------------------
探讨
insert into InterestReceive(CIFNm,RepayType,RepayAmount)
SELECT CIFNmFROM
(
select CIFNm,ContractNm,LoanBlankDate,PeridNm,'PeriodCapital' as RepayType, PeriodCapital as RepayAmount from InterestLis……

------解决方案--------------------
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)