我们正在编写一个新的应用程序,在进行测试时,我们将需要一堆虚拟数据。我已经通过使用MS Access将excel文件转储到相关表中添加了这些数据。

每隔一段时间,我们想要“刷新”相关的表,这意味着将它们全部删除,重新创建它们,并运行已保存的MS Access追加查询。

第一部分(删除和重新创建)是一个简单的sql脚本,但最后一部分让我畏缩。我想要一个单独的设置脚本,它有一堆insert来重新生成虚拟数据。

我现在有数据在表格里。从该数据集自动生成一个大的INSERT语句列表的最佳方法是什么?

我能想到的唯一方法是将表保存到excel表格中,然后编写excel公式为每一行创建一个INSERT,这肯定不是最好的方法。

我使用2008 Management Studio连接到SQL Server 2005数据库。


当前回答

这也可以使用Visual Studio完成(至少在2013年版本开始)。

在VS 2013中,也可以过滤insert语句所基于的行列表,据我所知,这在SSMS中是不可能的。

请执行以下步骤:

Open the "SQL Server Object Explorer" window (menu: /View/SQL Server Object Explorer) Open / expand the database and its tables Right click on the table and choose "View data" from context menu This will display the data in the main area Optional step: Click on the filter icon "Sort and filter data set" (the fourth icon from the left on the row above the result) and apply some filter to one or more columns Click on the "Script" or "Script to File" icons (the icons on the right of the top row, they look like little sheets of paper)

这将为活动窗口或文件的所选表创建(有条件的)插入语句。

Visual Studio 2013:滤镜和脚本

其他回答

为什么不只是在使用数据之前备份数据,然后在需要刷新数据时进行恢复呢?

如果必须生成插入,请尝试:http://vyaskn.tripod.com/code.htm#inserts

Azure Data Studio -简单的数据脚本扩展。

插入…它生成每一行的UNION ALL SELECT。

Azure Data Studio UI是基于VSCode的,我发现它是直观和快速的,包括这个OSS扩展。

生产数据库中有数据吗?如果是这样,您可以通过DTS设置数据的周期刷新。我们每周都在周末进行测试,每周都有干净、真实的测试数据是非常好的。

如果你还没有生产,那么你应该创建一个他们想要的数据库(新鲜的)。然后,复制该数据库并使用新创建的数据库作为您的测试环境。当你想要一个干净的版本时,只需再复制一遍干净的版本,就可以了。

为了获得带有过滤记录的INSERT语句(WHERE QUERY),您可以

右击表脚本表>创建到>新的查询 用temp_table重命名 现在运行 选择到temp_table从你的第一个表,现在你的标准在这里

这样你的临时表将只有你想要的记录, 现在通过运行由@Mike Ritacco with DATA ONLY解释的脚本向导,您将得到确切的插入语句。

这也可以使用Visual Studio完成(至少在2013年版本开始)。

在VS 2013中,也可以过滤insert语句所基于的行列表,据我所知,这在SSMS中是不可能的。

请执行以下步骤:

Open the "SQL Server Object Explorer" window (menu: /View/SQL Server Object Explorer) Open / expand the database and its tables Right click on the table and choose "View data" from context menu This will display the data in the main area Optional step: Click on the filter icon "Sort and filter data set" (the fourth icon from the left on the row above the result) and apply some filter to one or more columns Click on the "Script" or "Script to File" icons (the icons on the right of the top row, they look like little sheets of paper)

这将为活动窗口或文件的所选表创建(有条件的)插入语句。

Visual Studio 2013:滤镜和脚本