VBA 將陣列上傳到資料表自定義函式

2021-10-10 12:42:05 字數 2324 閱讀 1565

'其中arr必須帶欄位,否則無法匹配上傳

'如果tracking_id為0,則不管tracking_id的上傳,如果不為0,則上傳到名為tracking_id的字段

'by dawei gao,2020-11-13

sub arraytodatatable(

byval arr as variant,

byval tablename as

string

, cnt as

object

,optional

object

=nothing

, _ optional

byval chunck_size as

long

=1000

,optional tracking_id as

long=0

)dim sql as

string

, rst as

object

dim ro as

long

, lstro as

long

dim col as

long

, lstcol as

long

dimfield

as variant

dim dic_fields as

object

set rst = createobject(

"adodb.recordset"

) lstro =

ubound

(arr,1)

lstcol =

ubound

(arr,2)

nothing

then

"scripting.dictionary"

)for col =

1to lstcol

1, col)

)= arr(

1, col)

next col

else

for col =

1to lstcol

if1, col)

)then

1, col)

)= arr(

1, col)

endif

next col

endif'

sql = stringformat("select

* from where 1

=2", tablename)

rst.open sql, cnt,1,

3set dic_fields = getfieldscollectionfromrecordset(rst)

'上傳資料

with rst

if tracking_id =

0then

for ro =

2to lstro

.addnew

for col =

1to lstcol

1, col)

)then

field

1, col)

)if dic_fields.exists(

field

)then

'資料庫中有這個欄位才上傳

.fields(

field

)= arr(ro, col)

endif

endif

next

if(ro mod chunck_size =1)

or(ro = lstro)

then

.update

endifnext ro

else

for ro =

2to lstro

.addnew

for col =

1to lstcol

1, col)

)then

field

1, col)

)if dic_fields.exists(

field

)then

'資料庫中有這個欄位才上傳

.fields(

field

)= arr(ro, col)

endif

endif

next

.fields(

"tracking_id"

)= tracking_id

if(ro mod chunck_size =1)

or(ro = lstro)

then

.update

endifnext ro

endif .close

endwith

endsub

建立自定義資料表(再示例)

原部落格已經有例子,這裡再示例,並採用強型別讀取資料表列資料。xml建立儲存讀取 讀取條件設定.writexml xmldocument.xml 儲存 讀取條件設定.dispose dataset newdataset new dataset new dataset newdataset.readx...

hive 自定義元資料表 Hive 元資料表含義

hive 元資料表含義 1 version 查詢版本資訊 fieldtypecomment ver id bigint 20 id主鍵 schema version varchar 127 hive版本 version comment varchar 255 版本說明 2 dbs 儲存hive中所有...

UE4 自定義結構體 類 資料表

自定義資料表 pragma once include coreminimal.h include engine userdefinedstruct.h include components canvaspanel.h include blueprint userwidget.h include ru...