我想在Access 2010表中插入多个值,但我似乎无法找到方法 MySQL有一个很好的方式:
INSERT INTO Production.UnitMeasure
VALUES
(N'FT2', N'Square Feet ', '20080923'),
(N'Y', N'Yards', '20080923'),
(N'Y3', N'Cubic Yards', '20080923');
这样的事情也可以在SQL Server中完成吗?
答案 0 :(得分:10)
正如marc_s指出的那样,对于SQL Server 2008及更高版本,您只需使用表值构造函数即可。对于以前的版本,您可以使用insert
和select...union all
,例如:
INSERT INTO Production.UnitMeasure
SELECT N'FT2',N'Square Feet ','20080923' union all
SELECT N'Y', N'Yards', '20080923' union all
SELECT N'Y3', N'Cubic Yards', '20080923'
(SQL Server中Table Value Constructor的特定文档。我找不到关于行值构造函数的具体单独文档,但这就是它们的内容)
答案 1 :(得分:6)
对于SQL-Server:是的,它可以完全像你写的那样。请确保列值的顺序与表中显示的顺序相同。另外:您必须为每个现有列提供一个值。
对于Access 2010:否。至少不是通过sql中的硬编码值,而是仅通过从表中选择多个记录(在相同或另一个数据库中)。另见Khepri答案中的链接。
答案 2 :(得分:5)
使用此确认工作查询:
INSERT INTO Product (Code,Name,IsActive,CreatedById,CreatedDate )
SELECT * FROM
(
SELECT '10001000' AS Code,
'Blackburn sunglasses' AS Name,
1 AS IsActive,
1 AS CreatedById,
'2/20/2015 12:23:00 AM' AS CreatedDate
FROM Product
UNION
SELECT '10005200' AS Code,
'30 panel football' AS Name,
1 AS IsActive,
1 AS CreatedById,
'2/20/2015 12:23:09 AM' AS CreatedDate
FROM Product
) ;
答案 3 :(得分:2)
SQL Server绝对允许这样:编辑: [从SQL Server 2008开始,谢谢Marc_s]
INSERT INTO [Table]
([COL1], [COL2])
VALUES
('1@1.com', 1),
('2@2.com', 2)
至于Access要求,我不是访问大师,但我发现this MSDN documentation显示了如何一次执行多个插入。
INSERT INTO target [(field1[, field2[, …]])] [IN externaldatabase]
SELECT [source.]field1[, field2[, …] FROM tableexpression
在此之后做一些粗略的阅读,如果您的所有值都是提前知道的,那么您可以使用表格中的“虚拟”。
答案 4 :(得分:1)
使用单个整数列创建一个名为OneRow的表。插入一行。
然后:
INSERT INTO Production.UnitMeasure
SELECT 'FT2', 'Square Feet ', '20080923' FROM OneRow
UNION ALL SELECT 'Y', 'Yards', '20080923' FROM OneRow
UNION ALL SELECT 'Y3', 'Cubic Yards', '20080923' FROM OneRow
您的确切语法适用于SQL Server 2008.对于早期使用上面的查询而没有FROM子句和没有帮助程序表。
答案 5 :(得分:1)
我知道回答晚了,但有几种方法今天仍然有用(此处未提及)。 一般有两种方法。
使用带有“Docmd.RunSQL”语句的 VBA 脚本进行循环。 - 这通常很慢,尤其是随着行数的增加,但很容易理解。
将您的“数据数组”复制到 excel 工作表中,然后使用数据库查询链接到 excel 文件(我的首选方法)-这样做的好处是它几乎与表已经在数据库并且不一定像以前的方法那样因记录数而变慢 - 但是当您有少量记录时,比上面的方法稍慢
Docmd 方法:
Option Compare Database
Option Base 1
'------- method created by Syed Noshahi --------
'https://www.linkedin.com/in/syed-n-928b2490/
Sub DoCmdRoutine()
Dim arr() As Variant ' create an unsized array ready for data
'--------------For the purposes of the Example, put some data in the array----------
ReDim arr(5, 5)
For i = LBound(arr) To UBound(arr)
For j = LBound(arr) To UBound(arr)
arr(i, j) = i * j
If i = LBound(arr) Then arr(i, j) = "col_" & arr(i, j) 'Append "col_" before the column names
Next
Next
'-------------Importing the data to a table in MS ACCESS----------------------------
sSQL = "INSERT INTO [#TableTemp] " ' change table to actual table name
DoCmd.SetWarnings False 'turn warnings off - no popups!
For i = 2 To UBound(arr) 'start at 2 assuming that the first columns are headers
vals = "" 'go through each column and copy the data to SQL string format
'replace any single quote with double quotes so it does not error importing into SQL
For j = 1 To UBound(arr, 2)
If IsDate(arr(i, j)) Then 'if a date, convert to a number and let access re-covert to date (best chance at success)
vals = vals & " cdate('" & CDbl(arr(i, j)) & "'),"
ElseIf IsNumeric(arr(i, j)) Then 'if a number put through as a number
vals = vals & arr(i, j) & ","
Else 'otherwise treat as a text value
vals = vals & Replace(arr(i, j), "'", "''", , , 1) & "',"
End If
Next
vals = " VALUES(" & Left(vals, Len(vals) - 1) & ")" 'put in correct sql format
DoCmd.RunSQL sSQL & vals 'Run the SQL statement and import into the database
Next
DoCmd.SetWarnings True 'turn warnings on
End Sub
Excel 链接方法:
Option Compare Database
Option Base 1
'------- method created by Syed Noshahi --------
'https://www.linkedin.com/in/syed-n-928b2490/
Sub ExcelLinkRoutine()
Dim arr() As Variant ' create an unsized array ready for data
Dim oExcel As Object ' Excel instance - late binding
' works with access 2007+, access 2003 has a different SQL syntax
'--------------For the purposes of the Example, put some data in the array----------
ReDim arr(5, 5)
For i = LBound(arr) To UBound(arr)
For j = LBound(arr) To UBound(arr)
arr(i, j) = i * j
If i = LBound(arr) Then arr(i, j) = "col_" & arr(i, j) 'Append "col_" before the column names
Next
Next
'----------------------------output the array to an excel file ---------------------
Set oExcel = CreateObject("Excel.Application")
oExcel.Workbooks.Add 1
Set wb = oExcel.ActiveWorkbook
'network file path & normal path examples below
'fileNameWithExtention = "\\networkpath\location\example999.xlsb" ' note that xlsb file format must be used
' other formats can be used by changing 'xlExcel12'
' ONLY change the path not the FILE NAME
fileNameWithExtention = "C:\Users\public\documents\example999.xlsb" ' same as above
checkFileExists = Dir(fileNameWithExtention)
If Len(checkFileExists) > 0 Then
'only delete the file if its called example999!
If checkFileExists = "example999.xlsb" Then
Kill fileNameWithExtention
End If
End If
With wb
.Sheets(1).Cells(1, 1).Resize(UBound(arr), UBound(arr, 2)).Value2 = arr()
.SaveAs fileNameWithExtention, 50 ' 50 means xlExcel12
.Close False
End With
Set wb = Nothing
Set oExcel = Nothing
'------------ Importing the data to a table in MS ACCESS-----------------------------
'NOTE1: The saved down excelfile MUST be named Sheet1
'NOTE2: if the file path contains special characters such as ,-'
' you may need find the correct way to input (or remove the special chars)
sSQL = "SELECT T1.* INTO [#TableTemp] " ' change table to actual table name
sSQL = sSQL & " FROM [Excel 12.0;HDR=YES;IMEX=2;ACCDB=YES;DATABASE=" & fileNameWithExtention & "].[Sheet1$] as T1;" ' linked table format
DoCmd.SetWarnings False 'turn warnings off - no popups!
DoCmd.RunSQL sSQL 'Run the SQL statement and import into the database
DoCmd.SetWarnings True 'turn warnings on
End Sub
输出:
Col_1 | Col_2 | Col_3 | Col_4 | Col_5 |
---|---|---|---|---|
2 | 4 | 6 | 8 | 10 |
3 | 6 | 9 | 12 | 15 |
4 | 8 | 12 | 16 | 20 |
5 | 10 | 15 | 20 | 25 |
答案 6 :(得分:0)
MS Access不允许从同一个sql窗口进行多次插入。如果您想 插入 ,请说表格中的10行,请说电影(mid,mname,mdirector,....) ,你需要 打开sql窗口,
很无聊。 相反,您可以通过执行以下操作从excel导入行:
excel中的整个数据集已加载到表“MOVIE”
中答案 7 :(得分:0)
我知道我在比赛中有点迟了,但是我想做你们在你的例子中提到的完全相同的事情。我试图使用Access将新的默认行列表插入到表/列表中,因为我有很多SQL经验,我试图以同样的方式进行,但是正如你的海报所指出的那样,它是不可能的做联盟等。
但是我只是想在这里发布回复,因为在您手动输入值的情况下(在这种情况下是字符串默认值),您只需在数据表视图中打开Access,从Excel复制数据即可将其粘贴到Access表中(或者在我的情况下,粘贴到SharePoint列表中)。你需要确保你的列完全排成一行,但是如果你要手动输入你的“插入”sql语句,那么将这些信息放入Excel电子表格应该不是什么大问题。
在我的情况下,我的表/列表只有一个列作为查找,所以我只是从notepad ++复制了列并将其粘贴到数据表视图中。
祝大家好运!答案 8 :(得分:0)
检查以下内容,
INSERT INTO [Customer] ([Id],[FirstName],[LastName],[City],[Country],[Phone])VALUES(1,'Maria','Anders','Berlin','Germany','030-0074321')
INSERT INTO [Customer] ([Id],[FirstName],[LastName],[City],[Country],[Phone])VALUES(2,'Ana','Trujillo','México D.F.','Mexico','(5) 555-4729')