出错问题
SQL code
INSERT INTO RAresultA41
([SegtID]
,[Segtname]
,[acctnobe]
,[acctname]
,[acctid]
,[acctname2]
,[Mtd201301]
,[Mtd201302]
,[Mtd201303]
,[Mtd201204]
,[Mtd201205]
,[Mtd201206]
,[Mtd201207]
,[Mtd201208]
,[Mtd201209]
,[Mtd201210]
,[Mtd201211]
,[Mtd201212]
)
exec CrossTab
'select b.acctid, a.SegtID, a.segtname, b.netpaid * -1 as netpaid, b.fiscaldateId, c.calendaryear,
c.calendarmonth, c.fromDate, c.fromDateString, a.acctseqno, a.acctname, a.acctNoBe
from d_acctsegtType a inner join F_AccountEntry b
on a.acctid = b.acctid
and a.companyid = b.companyid
inner join d_fiscaldate c
on b.fiscaldateId = c.fiscalDateId
and b.companyid = c.companyid
where (a.segtid >=23 and a.segtid <=49)
and a.CompanyID = 3
and c.fiscaldateid >= 1 ',
'fromDateString', -- pivotCol
'sum(netpaid)[]',
'SegtID,segtname,acctnobe,acctname,acctid',
'acctname',
'acctnobe'
Msg 8164, Level 16, State 1, Procedure CrossTab, Line 26
An INSERT EXEC statement cannot be nested.[/color[color=#008000]]----一个插入执行语句不可以嵌套。(74 row(s) affected)
上面语句能对相应的表插入数据,但出错的原因还是想知道
------解决方案--------------------EXEC 后面应该直接跟参数吧 你写的是查询语句。
------解决方案--------------------晕,你这什么语句?还有那个crosstab什么东西来的?
------解决方案--------------------这个是因为语法限制
所以报错
试试 OPENROWSET
EXEC('
INSERT INTO RAresultA41
SELECT a.*
FROM OPENROWSET(''MSDASQL'',''DRIVER={SQL Server}; SERVER=YourServerName; UID=sa; PWD=PASSWORD'', ''Exec CrossTab ''''select b.acctid, a.SegtID, a.segtname, b.netpaid * -1 as netpaid, b.fiscaldateId, c.calendaryear,
c.calendarmonth, c.fromDate, c.fromDateString, a.acctseqno, a.acctname, a.acctNoBe
from d_acctsegtType a inner join F_AccountEntry b
on a.acctid = b.acctid
and a.companyid = b.companyid
inner join d_fiscaldate c
on b.fiscaldateId = c.fiscalDateId
and b.companyid = c.companyid
where (a.segtid >=23 and a.segtid <=49)
and a.CompanyID = 3
and c.fiscaldateid >= 1 '''',
''''fromDateString '''', -- pivotCol
''''sum(netpaid)[] '''',
''''SegtID,segtname,acctnobe,acctname,acctid '''',
''''acctname '''',
''''acctnobe '''''') AS a;
')