把Excel表中的資料匯入資料庫

2021-04-12 22:49:30 字數 3115 閱讀 9322

【需求】

在實現競價**時,需要把excel表中的資料週期地有條件地匯入到**位表中。這裡結合資料庫作業實現。

【步驟】

1、生成乙個excel表

工作表名稱:website

包含的列:spid,spname,spiniprice,spincextent,spnowprice,spimage,spbidnum,spauctype,spbaucdt,speaucdt,spstat,buyerid,sitename,siteurl

部分截圖:

2、儲存過程

--1、涉及到的表:wl_siteposition(**位表),wl_comsite(競價**表),excel表(需要物理路徑)

--2、功能:若**位表不存在記錄,則從excel表中匯入;反之,把**位表中的記錄匯入到競價**表,

--同時,清空**位表,之後再把excel表中的資料匯入到表wl_siteposition;

create procedure wl_job_websiteauction as

declare @err1 int

declare @err2 int

declare @err3 int

declare @err4 int

declare @err5 int

declare @err6 int

set @err1 = 0

set @err2 = 0

set @err3 = 0

set @err4 = 0

set @err5 = 0

set @err6 = 0

set nocount on

--設定事務隔離級別

--set transaction isolation level serializable

--ole/db provider  不支援隔離層

begin tran --開始事務

--set xact_abort on --

if (select count(*) from wl_siteposition)>0

begin

--把資料從**位表中匯入到競價**表  

truncate table wl_comsite --清空競價**表

set @err1 = @@error

--把資料從**位表中匯入到競價**表中

insert into wl_comsite(csitename,csiteurl,csitevdt,spid,buyerid)

select sitename,siteurl,dateadd(day,7,speaucdt) as csitevdt,spid,buyerid

from wl_siteposition order by spid

set @err2 = @@error

truncate table wl_siteposition--清空**位表

set @err3 = @@error

--把資料從excel表匯入到**位表中

insert into wl_siteposition(spid,spname,spiniprice,spincextent,spnowprice,spimage,spbidnum,spauctype,spbaucdt,speaucdt,spstat,buyerid,sitename,siteurl)

select spid,spname,spiniprice,spincextent,spnowprice,spimage,spbidnum,spauctype,spbaucdt,speaucdt,spstat,buyerid,sitename,siteurl

fromopendatasource('microsoft.jet.oledb.4.0','data source="d:/work/我傲/競拍設計/websiteposition_data.xls";extended properties="excel 8.0";persist security info=false')...[website$]

set @err4 = @@error

endelse

begin

--把資料從excel表匯入到**位表中

insert into wl_siteposition(spid,spname,spiniprice,spincextent,spnowprice,spimage,spbidnum,spauctype,spbaucdt,speaucdt,spstat,buyerid,sitename,siteurl)

select spid,spname,spiniprice,spincextent,spnowprice,spimage,spbidnum,spauctype,spbaucdt,speaucdt,spstat,buyerid,sitename,siteurl

from opendatasource('microsoft.jet.oledb.4.0','data source="d:/work/我傲/競拍設計/websiteposition_data.xls";extended properties="excel 8.0";persist security info=false')...[website$]

set @err5 = @@error

--把資料從**位表中匯入到競價**表中

insert into wl_comsite(csitename,csiteurl,csitevdt,spid,buyerid)

select sitename,siteurl,dateadd(day,7,speaucdt) as csitevdt,spid,buyerid

from wl_siteposition order by spid

set @err6 = @@error

end

if @err1=0 and @err2=0 and @err3=0 and @err4=0 and @err5=0 and @err6=0

commit tran

else

rollback tran

set nocount off

go3、建立資料庫作業的截圖

把Excel表中資料匯入資料庫

這是第二次了,市場部那邊又來要求改資料。他們要改的是資料庫某張表中類似商品 等的資料,需要改的地方又多,我們上次是靠新來的兄弟乙個個給update進去的,這次老大去教了他們update語句,把煩人的皮球踢給他們了。但這樣乙個個更新很明顯不是辦法,我想通過excel直接把資料匯入資料庫應該是可行的吧,...

把DBGRID資料直接匯入EXCEL中的過程

忙了一天就忙了個excel匯出,以為很好了,雖然慢點,沒想到臨了還真整出乙個更好的匯入方法。可喜可賀。只可惜一天就這麼點收穫啊。大家看看把。直接從excel表顯示資料。要uses comobj private procedure copydbdatatoexcel target tdbgrid 定義...

PLSQL匯入Excel表中資料

pl sql 和sql sever匯入excel資料的原理類似,就是找到乙個匯入excel資料的功能項,按照步驟走就是了。下面是乙個些細節過程,希望對像我這樣的菜鳥有幫助。www.2cto.com 1 準備excel表。右擊資料表 選擇edit data.選擇資料,右擊,選擇copy to exce...