You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
IF ISNULL(@TargetDatabaseName, DB_NAME())IS NOT NULL
354
+
ANDISNULL(@TargetSchemaName, N'dbo')IS NOT NULL
353
355
AND @TargetTableName IS NOT NULL
354
356
AND @TargetColumnName IS NOT NULL
355
357
BEGIN
@@ -359,15 +361,6 @@ BEGIN
359
361
/* Add this after the existing parameter validations */
360
362
IF @TargetSessionType = N'table'
361
363
BEGIN
362
-
IF @TargetDatabaseName IS NULL
363
-
OR @TargetSchemaName IS NULL
364
-
OR @TargetTableName IS NULL
365
-
OR @TargetColumnName IS NULL
366
-
BEGIN
367
-
RAISERROR(N'When using a table as a source, you must specify @TargetDatabaseName, @TargetSchemaName, @TargetTableName, and @TargetColumnName.', 11, 1) WITH NOWAIT;
368
-
RETURN;
369
-
END;
370
-
371
364
IF @TargetDatabaseName IS NULL
372
365
BEGIN
373
366
SET @TargetDatabaseName = DB_NAME();
@@ -377,6 +370,16 @@ BEGIN
377
370
BEGIN
378
371
SET @TargetSchemaName = N'dbo';
379
372
END;
373
+
374
+
IF @TargetTableName IS NULL
375
+
OR @TargetColumnName IS NULL
376
+
BEGIN
377
+
RAISERROR(N'
378
+
When using a table as a source, you must specify @TargetTableName, and @TargetColumnName.
379
+
When @TargetDatabaseName or @TargetSchemaName is NULL, they default to DB_NAME() AND dbo',
380
+
11, 1) WITH NOWAIT;
381
+
RETURN;
382
+
END;
380
383
381
384
/* Check if target database exists */
382
385
IF NOT EXISTS
@@ -1257,8 +1260,42 @@ BEGIN
1257
1260
SET @d =CONVERT(varchar(40), GETDATE(), 109);
1258
1261
RAISERROR('Inserting to #deadlock_data from table source %s', 0, 1, @d) WITH NOWAIT;
1259
1262
1263
+
/*
1264
+
First, we need to heck the XML structure.
1265
+
Depending on the data source, the XML could
1266
+
contain either the /event or /deadlock nodes.
1267
+
When the /event nodes are not present, there
1268
+
is no @name attribute to evaluate.
1269
+
*/
1270
+
1271
+
SELECT
1272
+
@extract_sql = N'
1273
+
SELECT TOP (1)
1274
+
@xe = xe.e.exist(''.''),
1275
+
@xd = xd.e.exist(''.'')
1276
+
FROM [master].[dbo].[bpr] AS x
1277
+
OUTER APPLY x.[bpr].nodes(''/event'') AS xe(e)
1278
+
OUTER APPLY x.[bpr].nodes(''/deadlock'') AS xd(e)
1279
+
OPTION(RECOMPILE);
1280
+
';
1281
+
1282
+
IF @Debug =1BEGIN PRINT @extract_sql; END;
1283
+
1284
+
EXECUTE sys.sp_executesql
1285
+
@extract_sql,
1286
+
N'
1287
+
@xe bit OUTPUT,
1288
+
@xd bit OUTPUT
1289
+
',
1290
+
@xe OUTPUT,
1291
+
@xd OUTPUT;
1292
+
1293
+
1260
1294
/* Build dynamic SQL to extract the XML */
1261
-
SET @extract_sql = N'
1295
+
IF @xe =1
1296
+
AND @xd IS NULL
1297
+
BEGIN
1298
+
SET @extract_sql = N'
1262
1299
SELECT
1263
1300
deadlock_xml = '+
1264
1301
QUOTENAME(@TargetColumnName) +
@@ -1281,21 +1318,60 @@ BEGIN
1281
1318
OR e.x.exist(''@name[ .= "database_xml_deadlock_report"]'') = 1
1282
1319
OR e.x.exist(''@name[ .= "xml_deadlock_report_filtered"]'') = 1
1283
1320
)';
1321
+
END;
1322
+
1323
+
IF @xe IS NULL
1324
+
AND @xd =1
1325
+
BEGIN
1326
+
SET @extract_sql = N'
1327
+
SELECT
1328
+
deadlock_xml = '+
1329
+
QUOTENAME(@TargetColumnName) +
1330
+
N'
1331
+
FROM '+
1332
+
QUOTENAME(@TargetDatabaseName) +
1333
+
N'.'+
1334
+
QUOTENAME(@TargetSchemaName) +
1335
+
N'.'+
1336
+
QUOTENAME(@TargetTableName) +
1337
+
N' AS x
1338
+
LEFT JOIN #t AS t
1339
+
ON 1 = 1
1340
+
CROSS APPLY x.'+
1341
+
QUOTENAME(@TargetColumnName) +
1342
+
N'.nodes(''/deadlock'') AS e(x)
1343
+
WHERE 1 = 1';
1344
+
END;
1284
1345
1285
1346
/* Add timestamp filtering if specified */
1286
-
IF @TargetTimestampColumnName IS NOT NULL
1347
+
IF @TargetTimestampColumnName IS NOT NULL
1287
1348
BEGIN
1288
1349
SET @extract_sql = @extract_sql + N'
1289
1350
AND x.'+ QUOTENAME(@TargetTimestampColumnName) + N' >= @StartDate
1290
1351
AND x.'+ QUOTENAME(@TargetTimestampColumnName) + N' < @EndDate';
1291
1352
END;
1292
1353
1293
-
/* If no timestamp column but date filtering is needed, handle XML-based filtering */
1294
-
IF @TargetTimestampColumnName IS NULL
1354
+
/* If no timestamp column but date filtering is needed, handle XML-based filtering when possible */
1355
+
IF @TargetTimestampColumnName IS NULL
1356
+
AND @xe =1
1357
+
AND @xd IS NULL
1295
1358
BEGIN
1296
1359
SET @extract_sql = @extract_sql + N'
1297
1360
AND e.x.exist(''@timestamp[. >= sql:variable("@StartDate") and . < sql:variable("@EndDate")]'') = 1';
1298
1361
END;
1362
+
1363
+
/*Woof*/
1364
+
IF @TargetTimestampColumnName IS NULL
1365
+
AND @xe IS NULL
1366
+
AND @xd =1
1367
+
BEGIN
1368
+
SET @extract_sql = @extract_sql + N'
1369
+
AND e.x.exist(''(/deadlock/process-list/process/@lasttranstarted)[. >= sql:variable("@StartDate") and . < sql:variable("@EndDate")]'') = 1';
0 commit comments