简体   繁体   English

使用 SQL Server 中的表的创建和更改语句生成所有脚本?

[英]Generate All scripts with Create and Alter statements for Table in SQL Server?

I want to generate all scripts in my database.我想在我的数据库中生成所有脚本。 As you know, clicking "Generate Scripts", it outputs all tables, all stored procedures etc. with CREATE statements.如您所知,单击“生成脚本”,它将使用CREATE语句输出所有表、所有存储过程等。

How can I get an ALTER statement for recently changed tables and CREATE statement for recently added tables, at the same time?如何同时获取最近更改的表的ALTER语句和最近添加的表的CREATE语句?

As an example, A, B, C, D tables were created before.例如,之前创建了 A、B、C、D 表。 Then I add a new table as E and I changed a column name in C. When I click "Generate Scripts", it must output:然后我添加一个新表作为 E 并更改了 C 中的列名。当我单击“生成脚本”时,它必须输出:

IF NOT EXISTS ....
CREATE TABLE A
....

IF NOT EXISTS ....
CREATE TABLE B
....

IF NOT EXISTS ....
CREATE TABLE C
....

IF NOT EXISTS ....
CREATE TABLE D
....

IF NOT EXISTS ....
CREATE TABLE E
....

ALTER TABLE C
MODIFY COLUMN XX...

like this.像这样。

生成脚本

Generate Scripts will just give you the create statements for the various objects in your database.生成脚本只会为您提供数据库中各种对象的创建语句。 It sounds like you're looking for a source control system, which would be able to generate CREATE/ALTER statements depending on changes from what is in source control.听起来您正在寻找一个源代码管理系统,它能够根据源代码管理中的内容的更改生成 CREATE/ALTER 语句。

Use SSMS to generate your scripts into a new query window, then in SSMS in that new query window do a find and replace using a RegEx search.使用 SSMS 将脚本生成到新的查询窗口中,然后在该新查询窗口中的 SSMS 中使用 RegEx 搜索进行查找和替换。

So if your script contains Functions click Ctrl+H then:因此,如果您的脚本包含函数,请单击Ctrl+H,然后:

Search item: (create.*function)搜索项:(创建。*功能)

Replacement item: create or alter function替换项:创建或更改功能

Then replace as needed然后根据需要更换

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

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