存储过程如下:
alter procedure d_sel_bj_xcl
@filter varchar(2000),@sortstr varchar(2000),@iskw bit
as
set @sortstr='spzyflid,spkw,spid'
declare @sql varchar(8000)
set @sql='
select spid,ckid,spmc,spgg,czmc,bzmc,cdmc,zlmc,spjldw,spkw,'
if @iskw=1
set @[email protected]+'kwh,'
set @[email protected]+'
sum(case when a.crklbfx=1 then sl else -sl end) as spxcl from (
select a.*,b.spzyflid,b.spmc,b.spgg,cz.czmc,bz.bzmc,cd.cdmc,zl.zlmc,b.spjldw,
spkw=case when patindex(''%([0-9][0-9][0-9][0-9])'',b.spmc)>0
then right(spmc,6) else '''' end,
c.crklbfx from bjlsb a
join crklb c on a.crlb=c.crklbid
left join sp b on a.spid=b.spid
left join cz on b.spczid=cz.czid
left join bz on b.spbzid=bz.bzid
left join zl on b.spzlid=zl.zlid
left join cd on b.spcdid=cd.cdid
where a.iszf is null '
if len(@filter)>0
set @[email protected]+' and '[email protected]
set @[email protected]+') a
group by spid,spzyflid,ckid,spmc,spgg,czmc,bzmc,cdmc,zlmc,spjldw,spkw'
if @iskw=1
set @[email protected]+',kwh'
if len(@sortstr)>0
set @[email protected]+' order by '[email protected]
exec(@sql)
调用的过程如下:
select * from
openrowset('sqloledb',
'jszg';'sa';'6162900',
'execute zgerp..d_sel_bj_xcl ''ckid=''''45'''''','''',0') as a
结果为:
服务器: 消息 7355,级别 16,状态 1,行 1
ole db 提供程序 'sqloledb' 为列提供的元数据不一致。执行时更改了名称。
ole db 错误跟踪[non-interface error: ole db provider returned different names for a column: providername='sqloledb', compiletimecolumnname='kwh', runtimecolumnname='spxcl', rowset=execute zgerp..d_sel_bj_xcl 'ckid=''45''','',0]。
仔细看一下就可以看到kwh列的存在,如果使用库位管理,那么,库位号就是一个汇总的依据,但查询时设为0,这和编译产生的列不同,编译产生所有的列,所以就出现了这个错误,实在想不出什么好办法,就将查询时将0设为1,反正不使用库位来管理,得出的数是不受影响的:)