为 SQL Server 表自动生成 INSERT 语句的最佳方法是什么?
我们正在编写一个新的应用程序,在测试时,我们将需要一堆虚拟数据。 我通过使用 MS Access 将 excel 文件转储到相关表中来添加该数据。
我们经常想要“刷新”相关表,这意味着将它们全部删除,重新创建它们,然后运行保存的 MS Access 追加查询。
第一部分(删除和重新创建)是一个简单的 SQL 脚本,但最后一部分让我感到畏缩。 我想要一个包含大量 INSERT 的设置脚本来重新生成虚拟数据。
我现在有表格中的数据。 从该数据集自动生成大量 INSERT 语句的最佳方法是什么?
我能想到的唯一方法是将表保存到 Excel 工作表,然后编写 Excel 公式为每一行创建 INSERT,这肯定不是最好的方法。
我正在使用 2008 Management Studio 连接到 SQL Server 2005 数据库。
We are writing a new application, and while testing, we will need a bunch of dummy data. I've added that data by using MS Access to dump excel files into the relevant tables.
Every so often, we want to "refresh" the relevant tables, which means dropping them all, re-creating them, and running a saved MS Access append query.
The first part (dropping & re-creating) is an easy sql script, but the last part makes me cringe. I want a single setup script that has a bunch of INSERTs to regenerate the dummy data.
I have the data in the tables now. What is the best way to automatically generate a big list of INSERT statements from that dataset?
The only way I can think of doing it is to save the table to an excel sheet and then write an excel formula to create an INSERT for every row, which is surely not the best way.
I'm using the 2008 Management Studio to connect to a SQL Server 2005 database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
Microsoft 应该宣传 SSMS 2008 的此功能。您正在寻找的功能内置于生成脚本实用程序中,但该功能默认情况下处于关闭状态,在编写表脚本时必须启用。
这是为表中的所有数据生成
INSERT
语句的快速运行,无需使用 SQL Management Studio 2008 的脚本或加载项:然后,您将获得直接从 SSMS 获取数据的
CREATE TABLE
语句和所有INSERT
语句。Microsoft should advertise this functionality of SSMS 2008. The feature you are looking for is built into the Generate Script utility, but the functionality is turned off by default and must be enabled when scripting a table.
This is a quick run through to generate the
INSERT
statements for all of the data in your table, using no scripts or add-ins to SQL Management Studio 2008:You will then get the
CREATE TABLE
statement and all of theINSERT
statements for the data straight out of SSMS.我们使用此存储过程 - 它允许您定位特定表并使用 where 子句。 您可以在此处找到文本。
例如,它允许您执行以下操作:
从链接复制的源代码:
We use this stored procedure - it allows you to target specific tables, and use where clauses. You can find the text here.
For example, it lets you do this:
Source code copied from link:
这也可以使用
Visual Studio
来完成(至少在 2013 版本以后)。在 VS 2013 中,还可以过滤插入语句所基于的行列表,据我所知,这在 SSMS 中是不可能的。
执行以下步骤:
这将为选定的表创建(有条件的)插入语句到活动窗口或文件。
Visual Studio 2013 的“筛选器”和“脚本”按钮:
This can be done using
Visual Studio
too (at least in version 2013 onwards).In VS 2013 it is also possible to filter the list of rows the inserts statement are based on, this is something not possible in SSMS as for as I know.
Perform the following steps:
This will create the (conditional) insert statements for the selected table to the active window or file.
The "Filter" and "Script" buttons Visual Studio 2013:
正如 @Mike Ritacco 所提到的,但针对 SSMS 2008 R2 进行了更新
然后您将直接从 SSMS 中获取数据的所有 INSERT 语句。
编辑2016-10-25 SQL Server 2016/SSMS 13.0.15900.1
右键单击数据库名称
选择任务> 生成脚本
根据您的设置,介绍页面可能会显示或不显示
选择“选择特定数据库对象”,
展开树视图并检查相关表
单击“下一步”
单击“高级”
在“常规”部分下,为“要处理的数据类型”选择适当的选项
脚本'
单击“确定”
选择是否要将输出发送至新查询、剪贴板或
文件
单击“下一步”两次
您的脚本已根据您上面选择的设置准备就绪
单击“完成”
As mentioned by @Mike Ritacco but updated for SSMS 2008 R2
You will then get all of the INSERT statements for the data straight out of SSMS.
EDIT 2016-10-25 SQL Server 2016/SSMS 13.0.15900.1
Right click on the database name
Choose Tasks > Generate scripts
Depending on your settings the intro page may show or not
Choose 'Select specific database objects',
Expand the tree view and check the relevant tables
Click Next
Click Advanced
Under General section, choose the appropriate option for 'Types of data to
script'
Click OK
Pick whether you want the output to go to a new query, the clipboard or a
file
Click Next twice
Your script is prepared in accordance with the settings you picked above
Click Finish
您可以使用 SSMS 工具包(适用于 SQL Server 2005 和 2008)。 它具有生成插入语句的功能。
http://www.ssmstoolspack.com/
You can use SSMS Tools Pack (available for SQL Server 2005 and 2008). It comes with a feature for generating insert statements.
http://www.ssmstoolspack.com/
我正在使用 SSMS 2008 版本 10.0.5500.0。 在此版本中,作为“生成脚本”向导的一部分,而不是“高级”按钮,而是出现以下屏幕。 在本例中,我只想插入数据而不需要创建语句,因此我必须更改两个圈出的属性
I'm using SSMS 2008 version 10.0.5500.0. In this version as part of the Generate Scripts wizard, instead of an Advanced button, there is the screen below. In this case, I wanted just the data inserted and no create statements, so I had to change the two circled properties
如果您需要编程访问,那么您可以使用开源存储过程“GenerateInsert”。
INSERT 语句生成器
作为一个简单快速的示例,生成 INSERT 语句表
AdventureWorks.Person.AddressType
执行以下语句:这将生成以下脚本:
If you need a programmatic access, then you can use an open source stored procedure `GenerateInsert.
INSERT statement(s) generator
Just as a simple and quick example, to generate INSERT statements for a table
AdventureWorks.Person.AddressType
execute following statements:This will generate the following script:
sp_generate_inserts 的第一个链接非常酷,这是一个非常简单的版本:
在我的系统上,我得到以下结果:
The first link to sp_generate_inserts is pretty cool, here is a really simple version:
On my system, I get this result:
我对这个问题的贡献是,Powershell INSERT 脚本生成器可以让您编写多个表的脚本,而无需使用繁琐的 SSMS GUI。 非常适合将“种子”数据快速持久保存到源代码控制中。
默认情况下,生成的 INSERT 脚本将是与该脚本位于同一文件夹下的“SeedData.sql”。
您将需要安装 SQL Server 管理对象程序集,如果您安装了 SSMS,则应该安装该程序集。
My contribution to the problem, a Powershell INSERT script generator that lets you script multiple tables without having to use the cumbersome SSMS GUI. Great for rapidly persisting "seed" data into source control.
By default, the INSERT script generated will be "SeedData.sql" under the same folder as the script.
You will need the SQL Server Management Objects assemblies installed, which should be there if you have SSMS installed.
不要使用插入,请使用 BCP
Don't use inserts, use BCP
GenerateData 是一个很棒的工具。 由于源代码可供您使用,因此对其进行调整也非常容易。 一些不错的功能:
GenerateData is an amazing tool for this. It's also very easy to make tweaks to it because the source code is available to you. A few nice features:
也许您可以尝试 SQL Server 发布向导
http://www.microsoft .com/downloads/details.aspx?FamilyId=56E5B1C5-BF17-42E0-A410-371A838E570A&displaylang=en
它有一个向导,可以帮助您编写插入语句的脚本。
Perhaps you can try the SQL Server Publishing Wizard
http://www.microsoft.com/downloads/details.aspx?FamilyId=56E5B1C5-BF17-42E0-A410-371A838E570A&displaylang=en
It has a wizard that helps you script insert statements.
为了获得带有过滤记录的 INSERT 语句(WHERE QUERY),您可以
RIGHT CLICK TABLE SCRIPT TABLE AS > 创建到> 新查询
使用 TEMP_TABLE 重命名
现在运行
从您的第一个表中选择 TEMP_TABLE,现在您的条件在这里
这样你的临时表将只包含你想要的记录,
现在,按照@Mike Ritacco的解释,通过仅使用数据运行脚本向导,您将获得准确的插入语句。
In order to get INSERT statement with filtered record(WHERE QUERY) you can
RIGHT CLICK TABLE SCRIPT TABLE AS > CREATE TO > NEW QUERY
RENAME IT WITH TEMP_TABLE
NOW RUN
SELECT INTO TEMP_TABLE FROM YOURFIRST TABLE WHERE NOW YOUR CRITERIA HERE
In this way you temp table will have only the record that you want ,
Now by running script wizard as explained by @Mike Ritacco with DATA ONLY you will get the exact insert statements.
我使用 sqlite 来做到这一点。 我发现它对于创建临时/测试数据库非常非常有用。
sqlite3 foo.sqlite .dump > > foo_as_a_bunch_of_inserts.sql
I use sqlite to do this. I find it very, very useful for creating scratch/test databases.
sqlite3 foo.sqlite .dump > foo_as_a_bunch_of_inserts.sql
您可以使用我几年前编写的这个简单且免费的应用程序生成INSERT或MERGE语句:
数据脚本编写器(Windows 桌面应用程序)
另外,我最近写了一篇关于这些工具的博客文章以及利用 SSDT 来部署包含数据的数据库的方法。 了解更多:
编写脚本并部署数据库数据SSDT项目
You can generate INSERT or MERGE statement with this simple and free application I wrote a few years ago:
Data Script Writer (Desktop Application for Windows)
Also, I wrote a blog post about these tools recently and approach to leveraging SSDT for a deployment database with data. Find out more:
Script and deploy the data for database from SSDT project
我制作了一个简单易用的实用程序,希望您喜欢。
如果生成的 INSERT 语句被截断,请检查 Management Studio 选项中结果的限制文本长度:
Tools > 选项
、查询结果> SQL服务器> 结果到网格
,“检索到的最大字符数”下的“非 XML 数据”值。I made a simple to use utility, hope you enjoy.
If the generated INSERT statements are being truncated, check the limit text length of the results on the Management Studio Options:
Tools > Options
,Query Results > SQL Server > Results to Grid
, "Non XML data" value under "Maximum Characters Retrieved".上面有许多用于生成插入语句的好脚本,但我尝试了自己的一个脚本,以使其尽可能用户友好,并且还能够执行 UPDATE 语句。 + 将结果打包为可按日期存储的 .sql 文件。
它将带有 WHERE 子句的普通 SELECT 语句作为输入,然后输出 Insert 语句和 update 语句的列表。 它们一起形成一种 IF NOT EXISTS () INSERT ELSE UPDATE 当存在需要从最终 INSERT/UPDATE 语句中排除的不可更新列时,它也很方便。
下面的脚本可以做的另一件事是:它甚至可以处理与其他表的 INNER JOIN 作为存储过程的输入语句。 它可以作为一个穷人的发布管理工具,在您整天键入 sql SELECT 语句时触手可及。
原始帖子:在 SQL Server 中生成 UPDATE 语句具体表格
There are many good scripts above for generating insert statements, but I attempted one of my own to make it as user friendly as possible and to also be able to do UPDATE statements. + package the result ready for .sql files that can be stored by date.
It takes as input your normal SELECT statement with WHERE clause, then outputs a list of Insert statements and update statements. Together they form a sort of IF NOT EXISTS () INSERT ELSE UPDATE It is handy too when there are non-updatable columns that need exclusion from the final INSERT/UPDATE statement.
Another thing that below script can do is: it can even handle INNER JOINs with other tables as input statement for the stored proc. It can be handy as a poor man's Release management tool that sits right at your finger tips where you are typing the sql SELECT statements all day.
original post : Generate UPDATE statement in SQL Server for specific table
为什么不在使用数据之前备份数据,然后在需要刷新时恢复呢?
如果您必须生成插入,请尝试: http://vyaskn.tripod.com/code.htm#inserts
why not just backup the data before your work with it, then restore when you want it to be refreshed?
if you must generate inserts try: http://vyaskn.tripod.com/code.htm#inserts
如果您更愿意使用 Google 表格,请使用 SeekWell 将表格发送到表格,然后插入计划中的行,因为它们被添加到工作表中。
请参阅此处了解操作步骤步骤流程 ,或在此处观看该功能的视频演示。
If you'd rather use Google Sheets, use SeekWell to Send the table to a Sheet, then insert rows on a schedule, as they're added to the Sheet.
See here for the step by step process , or watch a video demo of the feature here.
您的生产数据库中还有数据吗? 如果是这样,您可以通过 DTS 设置数据的周期刷新。 我们每周都会在周末进行测试,每周都能获得干净、真实的数据进行测试真是太好了。
如果您还没有生产,那么您应该创建一个他们想要的数据库(新鲜的)。 然后,复制该数据库并使用新创建的数据库作为测试环境。 当您想要干净版本时,只需再次复制干净版本即可鲍勃是你的叔叔。
Do you have data in a production database yet? If so, you could setup a period refresh of the data via DTS. We do ours weekly on the weekends and it is very nice to have clean, real data every week for our testing.
If you don't have production yet, then you should create a database that is they want you want it (fresh). Then, duplicate that database and use that newly created database as your test environment. When you want the clean version, simply duplicate your clean one again and Bob's your uncle.
Azure Data Studio - 简单数据脚本编写器扩展。
在
INSERT ...
之后,它会为每行生成UNION ALL SELECT
。Azure Data Studio UI 基于 VSCode,我发现它直观且快速,包括这个 OSS 扩展。
Azure Data Studio - Simple Data Scripter extension.
After
INSERT ...
it generatesUNION ALL SELECT
per row.Azure Data Studio UI is based on VSCode, and I find it to be intuitive and fast, including this OSS extension.
不确定我是否正确理解你的问题。
如果您在 MS-Access 中有数据,并且希望将其移动到 SQL Server - 您可以使用 DTS。
而且,我想您可以使用 SQL 探查器来查看所有正在执行的 INSERT 语句。
Not sure, if I understand your question correctly.
If you have data in MS-Access, which you want to move it to SQL Server - you could use DTS.
And, I guess you could use SQL profiler to see all the INSERT statements going by, I suppose.
我对此也进行了很多研究,但我无法得到具体的解决方案。 目前我采用的方法是从SQL Server Managment studio复制excel中的内容,然后将数据导入到Oracle-TOAD中,然后生成插入语句
I have also researched lot on this, but I could not get the concrete solution for this. Currently the approach I follow is copy the contents in excel from SQL Server Managment studio and then import the data into Oracle-TOAD and then generate the insert statements
您可以根据需要使其变得困难或简单。 我更喜欢后者。
如果您已经有一个数据集,您所要做的就是在 MS Access 中添加一个操作查询:
insert into Table1("column list here") select "column list here" from Table2
注意:Table2 可以是一个查询,而 Table1 和 Table2可以是 MS Access 表或 Sql Server 表
You can make it as difficult or as simple as you want. I prefer the latter.
If you already have a dataset, all you have to do is add an action query in MS Access:
insert into Table1("column list here") select "column list here" from Table2
NOTE: Table2 could be a query and Table1 and Table2 could be an MS Access table or Sql Server table