如果我在SQL中有以下表格,
Item Desc Type Location Quantity Unit_Price
A AAA X 1 0 20.00
B BBB Y 2 0 10.00
B CCC X 2 0 50.00
C DDD Z 1 0 150.00
C EEE Y 3 0 70.00
D FFF Z 3 0 65.00
以下Excel表格
Item Location Quantity
A 1 1
B 1 2
B 2 3
C 1 40
C 3 500
D 3 10
如何将这些数量插入从Excel表中读取的SQL表中?
答案 0 :(得分:3)
要使用T-SQL执行此操作,您可以详细遵循此tutorial,然后将数据拉入临时表,如以下SELECT…INTO
语句所示:
SELECT * INTO #ProductInfo
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml; HDR=YES; IMEX=1;
Database=C:\DataFiles\ProductData.xlsx',
[vProduct$]);
使用OPENROWSET
in-build函数检索Excel数据,其中第一个参数是提供者的名称,该提供者可以是两个提供者之一:
第二个OPENROWSET
参数定义由分号分隔的提供程序字符串,第一部分指定文件类型:
参数的第二部分指定文件的路径和文件名。第三个参数是我们要访问的电子表格的名称,附加美元符号($)并括在括号中,例如[Products $]。
将数据导入临时表#ProductInfo
后,您可以根据需要使用内部联接转换,过滤数据,然后更新“数量”字段:
UPDATE
p
SET
p.Quantity = temp.Quantity
FROM dbo.Product AS p
INNER JOIN #ProductInfo AS temp
ON temp.Item = p.Item
AND temp.Location = p.Location;
答案 1 :(得分:0)
SQL Server Management Studio可以通过其导入/导出功能执行此操作。我经常使用SSMS做这件事。
在SSMS中,右键单击数据库,然后选择“任务”|导入数据。
答案 2 :(得分:0)
首先需要将Excel工作表导入数据库。然后使用查询更新您的表格。
答案 3 :(得分:0)
您可以使用SQL Server Import and Export Wizard
首先将数据从excel导入临时表(它将创建新表,稍后可以删除)
在表格中包含数据后,您就可以更新现有数据
Here是一个简单的一步一步的方法
答案 4 :(得分:0)
右键单击数据库 - >任务 - >导入数据 - >向导将打开
单击Next(源向导将打开)并在DataSource下拉列表中选择“Microsoft Excel”选项并选择excel路径单击“下一步”按钮(Excel列和表列应相同,否则不会插入)
在向导中输入目的地详细信息,单击“下一步”
答案 5 :(得分:0)
尝试这个
Option Explicit
Public CN As ADODB.Connection
Dim Cod_Prod, Nombre, Existencia
Dim Fila, Final As Integer
Function Connect(Server As String, User As String, Pass As String, Database As String) As Boolean
Set CN = New ADODB.Connection
On Error Resume Next
With CN
.ConnectionString = "Provider=SQLOLEDB.1;" & _
"Password=" & Pass & ";" & _
"Persist Security Info=True;" & _
"User ID=" & User & ";" & _
"Initial Catalog=" & Database & ";" & _
"Data Source=" & Server
.Open
End With
If CN.State = 0 Then
Connect = False
Else
Connect = True
End If
End Function
Function Query()
Dim SQL As String
Dim RS As ADODB.Recordset
Dim Field As ADODB.Field
Dim Col As Long
Set RS = New ADODB.Recordset
Final = GetUltimoR(Hoja1)
For Fila = 2 To Final
Cod_Prod = Hoja1.Cells(Fila, 2)
Nombre = Hoja1.Cells(Fila, 3)
Existencia = Hoja1.Cells(Fila, 4)
SQL = "insert into productos values('" & Cod_Prod & "','" & Nombre & "'," & Existencia & ");"
RS.Open SQL, CN
Next
RS.Open "SELECT * FROM PRODUCTOS", CN
If RS.State Then
Col = 1
For Each Field In RS.Fields
Cells(1, Col) = Field.Name
Col = Col + 1
Next Field
Cells(2, 1).CopyFromRecordset RS
Set RS = Nothing
End If
End Function
Function Disconnect()
CN.Close
End Function
Public Sub run()
Dim SQL As String
Dim Connected As Boolean
Connected = Connect("192.168.0.12", "usuario1", "12345", "inventario")
If Connected Then
Call Query
Call Disconnect
Else
MsgBox "No podemos Conectarnos!"
End If
End Sub
Public Function GetUltimoR(Hoja As Worksheet) As Integer
GetUltimoR = GetNuevoR(Hoja) - 1
End Function
Public Function GetNuevoR(Hoja As Worksheet) As Integer
Dim Fila As Integer
Fila = 2
Do While Hoja.Cells(Fila, 2) <> ""
Fila = Fila + 1
Loop
GetNuevoR = Fila
End Function