如何从MS SQL数据库(Microsoft SQL Server)中的其他.sql文件运行.sql文件?

时间:2023-02-05 02:50:00

I have some .sql-files with creates tables (MS SQL Database):

我有一些带创建表的.sql文件(MS SQL数据库):

table_1.sql:

IF OBJECT_ID (N'my_schema.table1', N'U') IS NOT NULL
DROP TABLE my_schema.table1;

CREATE TABLE my_schema.table1(
  id int IDENTITY PRIMARY KEY,
  nameTable1 varchar(255) NOT NULL UNIQUE
);

and table_2.sql:

IF OBJECT_ID (N'my_schema.table2', N'U') IS NOT NULL
DROP TABLE my_schema.table2;

CREATE TABLE my_schema.table2(
 id int IDENTITY PRIMARY KEY,
 nameTable2 varchar(255) NOT NULL UNIQUE
);

so, and I want run these two .sql-files in third file: run_all_tables.sql, How can to run table_1.sql and table_2.sql via run_all_tables.sql, I think it should be similar:

所以,我想在第三个文件中运行这两个.sql文件:run_all_tables.sql,如何通过run_all_tables.sql运行table_1.sql和table_2.sql,我认为应该是类似的:

run_all_tables.sql:

BEGIN;
\i table_1.sql
\i table_2.sql
COMMIT;

What must be in run_all_tables.sql for to run table_1.sql and table_2.sql? If you have MS SQL Database (Microsoft SQL Server)

run_all_tables.sql中必须有什么才能运行table_1.sql和table_2.sql?如果您有MS SQL数据库(Microsoft SQL Server)

1 个解决方案

#1


1  

You use SQLCMD to execute .sql files sequentially. Putting the files in a single folder called Scripts, you would create the run_all_tables.sql file like so:

您使用SQLCMD顺序执行.sql文件。将文件放在名为Scripts的单个文件夹中,您将创建run_all_tables.sql文件,如下所示:

PRINT 'CREATING TABLES'

:r c:\Scripts\table_1.sql
:r c:\Scripts\table_2.sql

PRINT 'TABLE CREATION IS COMPLETE'

After creating that, you call it from command line, connecting to the database server.

创建后,从命令行调用它,连接到数据库服务器。

SQLCMD -S Servername\Instancename -d DatabaseName -i c:\Scripts\run_all_tables.sql

#1


1  

You use SQLCMD to execute .sql files sequentially. Putting the files in a single folder called Scripts, you would create the run_all_tables.sql file like so:

您使用SQLCMD顺序执行.sql文件。将文件放在名为Scripts的单个文件夹中,您将创建run_all_tables.sql文件,如下所示:

PRINT 'CREATING TABLES'

:r c:\Scripts\table_1.sql
:r c:\Scripts\table_2.sql

PRINT 'TABLE CREATION IS COMPLETE'

After creating that, you call it from command line, connecting to the database server.

创建后,从命令行调用它,连接到数据库服务器。

SQLCMD -S Servername\Instancename -d DatabaseName -i c:\Scripts\run_all_tables.sql