2

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

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:

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)

1 Answer 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:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.