将excel文件插入sql(选定的数据)

时间:2018-05-14 14:33:38

标签: sql sql-server

我有一个excel文件(目前看起来像附加图像)想要将它导入我的数据库。

enter image description here

目前我的查询看起来像这样

INSERT INTO [inSharePoint] (processingDate, [url], [sharepointName], [ownerEmail], [secondaryOwnerEmail], [lastUpdateDate], [totalQuotaInMB], [totalStorageInMB], [storageUsedInPercentage], [lockStatus], [siteType], [spVersion])
    VALUES ('{processingDate}', @url, @sharepointName, @ownerEmail, @secondaryOwnerEmail, @lastUpdateDate, @totalQuotaInMB, @totalStorageInMB, @storageUsedInPercentage, @lockStatus, @siteType, @spVersion)

但是,如果我想插入相同的文件,但唯一的区别是我的列[LockStatus],我只想要状态nolock插入而不是其他状态(没有只读和noaccess )

我真的很新sql希望有人能解释一下谢谢

2 个答案:

答案 0 :(得分:1)

正如@Daniel Marcus所提到的,如果有可用的话,你可以使用SSIS(SQL Server Integration Services)轻松完成这项工作。

在您当前的解决方案中,您似乎一行一行地处理Excel数据,在这种情况下,您需要使用IF语句检查NodeJS代码中的数据,并且仅在{{{ {1}} = LockStatus

或者,您可以使用mongo查询仅提取nolock = LockStatus的数据,并将该数据发送到SQL Server。我不熟悉查询mongo,所以不确定那会是什么样的。

答案 1 :(得分:0)

您可以使用VBA执行此操作!

Sub InsertARecord()
Dim cnt As ADODB.Connection
Dim rst As ADODB.Recordset
Dim stCon As String, stSQL As String
Set cnt = New ADODB.Connection
Set rst = New ADODB.Recordset

stCon = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=JOEY"
cnt.ConnectionString = stCon
stSQL = "INSERT INTO MyTable (FieldNames)"
stSQL = stSQL & "VALUES (ActualValues)"
stSQL = stSQL & "WHERE lockStatus = 'nolock'"

cnt.Open
rst.Open stSQL, cnt, adOpenStatic, adLockReadOnly, adCmdText

If CBool(rst.State And adStateOpen) = True Then rst.Close
Set rst = Nothing
If CBool(cnt.State And adStateOpen) = True Then cnt.Close
Set cnt = Nothing

End Sub

另请参阅此网址。

https://www.excel-sql-server.com/excel-sql-server-import-export-using-vba.htm#Introduction