功能: 可更新原來數(shù)據(jù)庫已經(jīng)存在的數(shù)據(jù),然后插入新數(shù)據(jù).
原理: 直接從上傳的excel表中讀取,存入臨時(shí)表,更新數(shù)據(jù)庫表中與臨時(shí)表中相同主鍵字段的數(shù)據(jù),再刪除臨時(shí)表中在數(shù)據(jù)庫表中已經(jīng)存在的記錄,再插入臨時(shí)表剩余的新數(shù)據(jù).
來源:同事.
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
--=======================================================
/*
funciton:補(bǔ)貼扣除導(dǎo)入
coder:zx
datetime:2009-10-14
[P_Import_Transportation] 0,'\\192.168.0.2\hglwpqvss\uploadFile\月度崗位補(bǔ)貼扣除表20091022150944_20091022031241.xls','2009-10-01','35242100'
*/
--=======================================================
ALTER proc [dbo].[P_Import_Transportation]
@UserID int, --操作人
@FilePath varchar(1000), --文件路徑
@DataTime datetime, --數(shù)據(jù)月份
@OrganizationalNum2 varchar(50), --二級單位
@rc int=null output,
@info varchar(100)=null output
as
begin
declare @WONum varchar(50)
select @WONum=WorkOrderNum from T_E_WorkOrder where and and AATID=18
if(@WONum is null)
begin
set @rc=-1
set @info='工單未建立'
return
end
declare @Count_Employee int,@Count_Excel int,@SQL varchar(8000),@Add int,@Update int
create table #Temp_TableAD
(序號 int identity(1,1),
三級部門 varchar(200),
Mis員工編號 varchar(50),
-- 員工編號 varchar(50),
員工姓名 varchar(50),
補(bǔ)貼交通費(fèi)員工類型 varchar(50),
交通費(fèi) decimal(18,2))
set @SQL='insert into #Temp_TableAD(三級部門,Mis員工編號,員工姓名,補(bǔ)貼交通費(fèi)員工類型,交通費(fèi))'+
'select 三級部門,Mis員工編號,員工姓名,補(bǔ)貼交通費(fèi)員工類型,isnull(交通費(fèi),0),'+
' from OpenDataSource( ''Microsoft.Jet.OLEDB.4.0'','+'''Excel 8.0; HDR=Yes; IMEX=1;'+
'Data ID=;Password=;'')...[交通費(fèi)信息$] WHERE isnull(員工編號,'''') <> '''''
print @SQL
exec(@SQL)
-- select * from #Temp_TableAD
--模板數(shù)據(jù)邏輯驗(yàn)證
if exists(select 1 from #Temp_TableAD group by 員工編號 having(count(1)>1))
begin
set @rc = -1
set @info='數(shù)據(jù)錯(cuò)誤,出現(xiàn)重復(fù)員工編號'
return
end
--二級單位本月份在職員工數(shù)
-- select @Count_Employee=count(1)
-- from T_E_Employee a
-- inner join T_H_EmployeeAttribution b on a.EmployeeNum=b.EmployeeNum and
-- where and a.DataState=1
--
-- --Excel中員工數(shù)
-- select @Count_Excel=count(1) from #Temp_TableAD
--print @Count_Employee
--print @Count_Excel
-- --員工數(shù)驗(yàn)證
-- if(@Count_Employee > @Count_Excel)
-- begin
-- set @rc = -2
-- set @info='存在未導(dǎo)入的員工'
-- return
-- end
/*數(shù)據(jù)導(dǎo)入*/
--更新已有數(shù)據(jù)
update T_E_EmployeeTransportation
set
Transportation = 交通費(fèi),
PTTYPE=補(bǔ)貼交通費(fèi)員工類型
from T_E_EmployeeAllowanceDeduction a ,#Temp_TableAD
where a.MisEmployeeNum = Mis員工編號 and
set @Update=@@rowcount
--刪除已更新數(shù)據(jù)
delete #Temp_TableAD
from T_E_EmployeeTransportation a ,#Temp_TableAD
where a.MisEmployeeNum = Mis員工編號 and
--新增數(shù)據(jù)
insert into dbo.T_E_EmployeeTransportation(DataTime,MisEmployeeNum,PTType,Transportation,OrganizationalNum2,WONum)
select @DataTime,Mis員工編號,補(bǔ)貼交通費(fèi)員工類型,交通費(fèi),@OrganizationalNum2,@WONum from #Temp_TableAD
set @Add=@@rowcount
set @rc=1
set @info='共導(dǎo)入 '+cast(@Add+@Update as varchar)+' 條數(shù)據(jù),其中 ' + cast(@Update as varchar) + ' 條更新; ' + cast(@Add as varchar) + ' 條新增'
print @info
end