日期:2014-05-16 浏览次数:20477 次
CREATE proc Hr_GetAllEmpl @year nvarchar(20)='0',@month nvarchar(20)='0',@Fields nvarchar(4000)='*',@OrderBy nvarchar(100)='',@Filter nvarchar(200)='' as
declare @command nvarchar(4000),@empl_New nvarchar(2000),@command2 nvarchar(4000)
if @year='0' or @month='0' or @Fields='*'--使用默認方法處理查詢數據
begin
if @Fields='*'
set @empl_New='service_empl_department,basic_depart_name,service_empl_part,basic_deppart_name,service_empl_index,service_empl_code,service_empl_name'
else
set @empl_New=@Fields
set @command='select '+@empl_New+' from V_AllEmpl'
end
else--根據年月方法處理查詢數據
begin
set @empl_New='case when isnull(p.admini_Code,'''')='''' or '''+@year+'-'+@month+'-1''<dateadd(d,day(v.service_empl_indate)*(-1)+1,v.service_empl_indate) then v.service_empl_code else (case when p.admini_date>v.service_empl_indate then p.admini_Code else p.admini_NewCode end) end as service_empl_code'
set @command='select '+replace(@Fields,'service_empl_code',@empl_New)+' from v_allempl v
left join (
select y.Admini_index,y.Admini_date,y.admini_Code,y.admini_NewCode
from Admini_poschang y with(nolock) , (select Admini_index,min(Admini_date) as sd from Admini_poschang with(nolock) where Admini_date >= dateadd(mm,1,'''+@year+'-'+@month+'-1'') group by Admini_index) l
where y.Admini_index = l.Admini_index and y.Admini_date = l.sd
) p on v.service_empl_index=p.admini_index'
end
if isnull(@Filter,'')<>''
set @command=@command+char(13)+char(10)+' where '+@Filter
if isnull(@OrderBy,'')<>''
set @command=@command+char(13)+char(10)+' order by '+@OrderBy
--print @command
exec (@command)
GO
exec data_20120604.dbo.Hr_GetAllEmpl @year=0,@month=5,@Fields='*',@OrderBy='service_empl_code',@Filter='service_empl_zc=''01'''
select * from openrowset('sqloledb','192.168.100.6';'sa';'','exec data_20120604.dbo.Hr_GetAllEmpl @year=0,@month=5,@Fields=''*'',@OrderBy=''service_empl_code'',@Filter=''service_empl_zc=''''01''''''')
--错误信息:
Server: Msg 7355, Level 16, State 1, Line 1
OLE DB Provider 'sqloledb' 對資料行提供了不一致的中繼資料 (Metadata)。名稱在執行階段已經變更。
OLE DB 錯誤追蹤 [Non-interface error: OLE DB provider returned different names for a column: ProviderName='sqloledb', CompileTimeColumnName='service_empl_index', RunTimeColumnName='service_empl_department', Rowset=exec data_20120604.dbo.Hr_GetAllEmpl @year=0,@month=5,@Fields='*',@OrderBy='service_empl_code',@Filter='service_empl_zc=''01''']。