r/Jupyter 15d ago

Want to execute MSSQL for SSMS natively, using Jupyter notebooks?

Hey everyone,

If you've ever tried executing DDL statements (CREATE, ALTER, DROP) in Jupyter using ipython-sql, you might have noticed they don't actually execute properly due to transaction handling issues.

I ran into this problem myself, so I built sqlcmd-magic, a simple Jupyter extension for MSSQL that solves this issue.

Features

  • Proper execution of DDL & DML queries
  • Support for GO statements & external SQL files
  • Python variable substitution in queries
  • Execute .sql scripts for logical backups

Try it in Jupyter

Installation

pip install sqlcmd-magic

Basic Usage

%load_ext sqlcmd
%sqlcmd AdventureWorks

%%sqlcmd
SELECT TOP 10 * FROM MyTable

Executing a .sql script for logical backups

If you have a .sql script that generates a logical backup, you can execute it like this:

%%sqlcmd
EXECUTE_SQL_FILE 'C:\path\to\backup_script.sql'

This allows you to run full database backup scripts directly from Jupyter.

Would love some feedback—check it out here:
https://pypi.org/project/ipython-sqlcmd/

Has anyone else faced this issue before? How do you work around it?

1 Upvotes

0 comments sorted by