Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 从SQL导出到Access_Sql Server_Ms Access_Export_Ssms - Fatal编程技术网

Sql server 从SQL导出到Access

Sql server 从SQL导出到Access,sql-server,ms-access,export,ssms,Sql Server,Ms Access,Export,Ssms,我有一个sql数据库,其中包含很多表和记录,但我只想导出某些表中的前N行。SQL导出向导可以工作,但会导出我不想执行的所有表数据,我只需要前N行 我怎样才能避开这件事?谢谢为每个表创建一个直接视图,选择前N个记录 然后导出视图。如果不想修改原始sql server数据库,可以创建第二个sql server数据库,该数据库仅包含@Gustav建议的视图 然后,这些视图引用原始sql server数据库的表 因此,您根本不必接触原始数据库。您应该能够将SQL Server中所有表中的数据转储到单独的

我有一个sql数据库,其中包含很多表和记录,但我只想导出某些表中的前N行。SQL导出向导可以工作,但会导出我不想执行的所有表数据,我只需要前N行

我怎样才能避开这件事?谢谢

为每个表创建一个直接视图,选择
前N个
记录


然后导出视图。

如果不想修改原始sql server数据库,可以创建第二个sql server数据库,该数据库仅包含@Gustav建议的视图

然后,这些视图引用原始sql server数据库的表


因此,您根本不必接触原始数据库。

您应该能够将SQL Server中所有表中的数据转储到单独的CSV文件中。以管理员权限打开PowerShell(搜索“PowerShell”,右键单击“PowerShell.exe”,然后单击“以管理员身份运行”)。在打开的windo中,运行以下代码

$server = "SERVERNAME\INSTANCE"
$database = "DATABASE_NAME"
$tablequery = "SELECT name from sys.tables"

#Delcare Connection Variables
$connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connectionString = [string]::Format($connectionTemplate, $server, $database)
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString

$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $tablequery
$command.Connection = $connection

#Load up the Tables in a dataset
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$connection.Close()



# Loop through all tables and export a CSV of the Table Data
foreach ($Row in $DataSet.Tables[0].Rows)
{
    $queryData = "SELECT * FROM [$($Row[0])]"

    #Specify the output location of your dump file
    $extractFile = "C:\mssql\export\$($Row[0]).csv"

    $command.CommandText = $queryData
    $command.Connection = $connection

    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $command
    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet)
    $connection.Close()

    $DataSet.Tables[0]  | Export-Csv $extractFile -NoTypeInformation
}

现在,在Access中,运行下面的VBA代码(遍历所有CSV文件并将每个文件导入到表中)


注意:您可以像我在这里做的那样链接到该文件,也可以导入该文件。如果导入数据,请确保在导入完成后进行压缩和修复。Access因导入大量数据后膨胀到相当大而臭名昭著。

我不想在sql数据库中添加任何内容。我试过使用临时表,但在SQL导出向导中似乎没有找到它们。您可以使用参数创建存储过程,然后通过MS Access Pass-Through查询调用该存储过程并保存查询。然后将此查询输出附加到access中的新表中。
Function DoImport()

 Dim strPathFile As String
 Dim strFile As String
 Dim strPath As String
 Dim strTable As String
 Dim blnHasFieldNames As Boolean

 ' Change this next line to True if the first row in CSV worksheet
 ' has field names
 blnHasFieldNames = True

 ' Replace C:\Documents\ with the real path to the folder that
 ' contains the CSV files
 strPath = "C:\your_path_here\"

 ' Replace tablename with the real name of the table into which
 ' the data are to be imported

 strFile = Dir(strPath & "*.csv")

 Do While Len(strFile) > 0
       strTable = Left(strFile, Len(strFile) - 4)
       strPathFile = strPath & strFile
       
       'DoCmd.TransferText acImportDelim, , strTable, strPathFile, blnHasFieldNames
       DoCmd.TransferText acLinkDelim, , strTable, strPathFile, blnHasFieldNames
       
 ' Uncomment out the next code step if you want to delete the
 ' EXCEL file after it's been imported
 '       Kill strPathFile

       strFile = Dir()
 Loop

End Function