简体   繁体   English

SQL Server:通过SQL查询导出数据?

[英]SQL Server: export data via SQL query?

I have FK and PK all over my db and table data needs to be specified in a certain order or else I get FK/PK insertion errors. 我在数据库中遍布FK和PK,并且需要按特定顺序指定表数据,否则会出现FK / PK插入错误。 I'm tired of executing the wizard again and again to transfer data one table at a time. 我已经厌倦了一次又一次地执行向导来一次一次传输一个表的数据。

In the SQL Server export data wizard there is an option to "Write a query to specify the data to transfer". 在SQL Server导出数据向导中,有一个选项“编写查询以指定要传输的数据”。 I'd like to write the query myself and specify the correct order. 我想自己编写查询并指定正确的顺序。

  1. Will this solve my problem? 这样可以解决我的问题吗?

  2. How do I do this? 我该怎么做呢? Can you provide a sample query (or link to one) 您能否提供示例查询(或链接到其中一个查询)

    • The databases are on two different servers - SQL Server 2008 on each ; 数据库位于两台不同的服务器上,每台服务器上均为SQL Server 2008。 The database names & permissions are the same ; 数据库名称和权限是相同的; each table name & col is the same ; 每个表的名称和颜色都是相同的; I need Identity Insert for each table. 我需要为每个表插入身份证明。

Disable foreign keys before importing, enable them after the import: 导入前禁用外键,导入后将其启用:

ALTER TABLE tablename NOCHECK CONSTRAINT ALL

ALTER TABLE tablename WITH CHECK CHECK CONSTRAINT ALL

Update: Thanks for the comments, I fixed the syntax. 更新:感谢您的评论,我修复了语法。

您可以始终保存该程序包,然后打开并编辑该程序包以按正确的顺序放置内容(您可能必须多次复制数据流并在它们之间放置依赖项)

You can use 3rd party tools to transfer a data; 您可以使用第三方工具来传输数据。 these tools disable/enable constraints automatically. 这些工具会自动禁用/启用约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM