如何执行存储在 Github 中的 SQL 个文件?

How can I execute SQL files stored in Github?

我有一个很好的 bash 脚本,它使用 az cli 生成 Azure SQL 服务器 (az sql server create) 和 SQL 数据库 (az sql db create).

我想用我在 Github.

中存储的一系列 .sql 文件中定义的表和列填充数据库

示例文件:

-- Create a new table called 'TEST_HDR' in schema 'dbo'
-- Drop the table if it already exists
IF OBJECT_ID('dbo.TEST_HDR', 'U') IS NOT NULL
DROP TABLE dbo.TEST_HDR
GO
-- Create the table in the specified schema
CREATE TABLE dbo.TEST_HDR
(
    tstID INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
    tstGUID [NVARCHAR](50),
    tstComments [NVARCHAR](2000),
    tstHdrCreated DATETIME2,
    tstHdrCreatedBy [NVARCHAR](255),
    tstHdrLastUpdated DATETIME2,
    tstHdrLastUpdatedBy [NVARCHAR](255),
    tstHdrDeleted [NVARCHAR](3),
    tstHdrDeletedBy [NVARCHAR](255),
    tstHdrVersionNum INT
);
GO

我使用哪些 bash(或其他脚本)命令从 Github 获取这些文件并针对 SQL 数据库执行它们?

假设您已经 sqlcmd 安装:

tmp=$(mktemp) && \
curl -sL https://raw.githubusercontent.com/path/to/your/TST_HDR.sql > ${tmp} && \
sqlcmd -S <servername>.database.windows.net -d <database> -U <user> -P <password> -i ${tmp}
mkdir (create directory)
cd (to the directory created, for the Github repository)
git clone (The address to the repository where your sql file is located)

确保端口在您连接的电脑和您连接的服务器上是可访问的。

sqlcmd -d (database) -i (filepath to the sql file, in the git repository) -P (password) -S (servername).database.windows.net -U (user)