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

问题描述

我有一个很好的 bash 脚本,它使用 az cli 生成 Azure sql Server (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)

确保在您连接的 PC 和您要连接的服务器上可以访问这些端口。

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