Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute SQL files stored in Github?

I have a nice bash script that uses az cli to generate an Azure SQL Server (az sql server create) and SQL database (az sql db create).

I'd like to populate the database with tables and columns I have defined in a series of .sql files stored in Github.

Example file:

  • Filename: TST_HDR.sql
  • File contents:
-- 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

Which bash (or other scripting) commands do I use to get these files from Github and execute them against the SQL database?

like image 408
SeaDude Avatar asked Sep 20 '25 08:09

SeaDude


2 Answers

Assuming you have sqlcmd installed:

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}
like image 154
Christian Vorhemus Avatar answered Sep 22 '25 06:09

Christian Vorhemus


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)

Make sure the ports are accessible on the pc you are connecting from and on the server you are connecting to.

sqlcmd -d (database) -i (filepath to the sql file, in the git repository) -P (password) -S (servername).database.windows.net -U (user) 
like image 36
Shaqil Ismail Avatar answered Sep 22 '25 08:09

Shaqil Ismail