微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何执行存储在 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) 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。