Skip to content

Instantly share code, notes, and snippets.

View MarkPryceMaherMSFT's full-sized avatar

Mark Pryce-Maher MarkPryceMaherMSFT

View GitHub Profile
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / query_extended_events.sql
Created December 11, 2025 16:54
query to look at extended events
SELECT
X.event_xml.value('(event/@name)[1]', 'varchar(50)') AS EventName,
X.event_xml.value('(event/action[@name="sql_text"]/value)[1]', 'nvarchar(max)') AS SQLText,
X.event_xml.value('(event/action[@name="database_name"]/value)[1]', 'varchar(128)') AS DatabaseName,
X.event_xml.value('(event/action[@name="username"]/value)[1]', 'varchar(128)') AS UserName,
X.event_xml.value('(event/@timestamp)[1]', 'datetime') AS EventTime
FROM sys.fn_xe_file_target_read_file('C:\XE\MonitorTableQueries*.xel', NULL, NULL, NULL) AS F
CROSS APPLY (SELECT CAST(F.event_data AS XML)) AS X(event_xml)
ORDER BY EventTime DESC;
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / monitoring_extended_events.sql
Last active December 11, 2025 16:52
Query to monitor the activity from Mirroring
-- Create Extended Events session
CREATE EVENT SESSION [MonitorTableQueries]
ON SERVER
ADD EVENT sqlserver.sql_statement_completed
(
ACTION (
sqlserver.sql_text,
sqlserver.database_name,
sqlserver.username
)
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / Clean up Extended Events.sql
Created November 18, 2025 19:33
Clean up evented events
-- Stop the Extended Events session if it is running
IF EXISTS (
SELECT 1
FROM sys.server_event_sessions
WHERE name = 'MonitorTableQueries'
)
BEGIN
ALTER EVENT SESSION [MonitorTableQueries] ON SERVER STATE = STOP;
PRINT 'MonitorTableQueries session stopped.';
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / MirroringEventTrace.sql
Created November 18, 2025 19:29
Script to setup an extended events trace for SQL Mirroring
-- Create Extended Events session
CREATE EVENT SESSION [MonitorTableQueries]
ON SERVER
ADD EVENT sqlserver.sql_statement_completed
(
ACTION (
sqlserver.sql_text,
sqlserver.database_name,
sqlserver.username
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / sp_help_change_feed.sql
Created October 9, 2025 19:34
Code to materialise the stored proc sp_help_change_feed
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[help_change_feed]') AND type in (N'U')) DROP TABLE [dbo].[help_change_feed]
create table help_change_feed
(
table_group_id uniqueidentifier ,
table_group_name nvarchar(140) ,
destination_location nvarchar(512) ,
destination_credential nvarchar(247) ,
destination_type nvarchar(247) ,
workspace_id nvarchar(247) ,
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / ConvertViews.py
Last active October 4, 2025 03:12
This code is for converting spark views to tsql views
%%pyspark
import sempy.fabric as fabric
import struct
import sqlalchemy
import pyodbc
import pandas as pd
from notebookutils import mssparkutils
#Function to Return sqlalchemt ODBC Engine, given a connection string and using Integrated AAD Auth to Fabric
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / mapping.json
Created September 29, 2025 11:19
json mapping
@json('
"translator": {
"type": "TabularTranslator",
"mappings": [
{
"source": {
"name": "t",
"type": "DateTime",
"physicalType": "datetime"
},
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / sync_everything.py
Created September 4, 2025 16:51
MD Sync all lakehouses
import requests
from notebookutils import mssparkutils
import sempy.fabric as fabric
from sempy.fabric.exceptions import FabricHTTPException, WorkspaceNotFoundException
import json
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, StringType
from datetime import datetime
# Get token
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / delta_publishing_test.py
Last active September 5, 2025 08:10
Test to see how long it takes delta log publishing to work
import pandas as pd
import struct
import sqlalchemy
import pyodbc
import notebookutils
import sempy.fabric as fabric
from sqlalchemy import text
import time
# -----------------------------------------------------------------------------
@MarkPryceMaherMSFT
MarkPryceMaherMSFT / migrate_views.py
Created August 11, 2025 13:11
View Migration Script: SQL Server ➜ Fabric Warehouse/Lakehouse -------------------------------------------------------------- This script copies one or more view definitions from a source SQL Server database into a Fabric Warehouse or Lakehouse SQL endpoint. Key Features: - Connects to SQL Server using SQLAlchemy/pyodbc. - Connects to Fabric usi…
"""
View Migration Script: SQL ServerFabric Warehouse/Lakehouse
--------------------------------------------------------------
This script copies one or more view definitions from a source SQL Server database
into a Fabric Warehouse or Lakehouse SQL endpoint.
Key Features:
- Connects to SQL Server using SQLAlchemy/pyodbc.
- Connects to Fabric using MSI authentication (access token).
- Reads view definitions from sys.views/sys.sql_modules in the source.