Skip to content

Commit cbcbca0

Browse files
Bugfix during truncate table execution
1 parent ef46aa3 commit cbcbca0

File tree

6 files changed

+36
-2
lines changed

6 files changed

+36
-2
lines changed

Forms/MainBox.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ private List<IndexOp> GetIndexOperations(Index ix) {
633633
}
634634
}
635635

636-
if (ix.IsTable) {
636+
if (ix.IsTable && !ix.IsFKs && ((ix.IsPartitioned && Settings.ServerInfo.MajorVersion >= ServerVersion.Sql2016) || !ix.IsPartitioned)) {
637637
i.Add(IndexOp.TRUNCATE_TABLE);
638638
}
639639

Properties/Resources.Designer.cs

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Properties/Resources.resx

+3
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,7 @@
409409
<data name="IsTable" xml:space="preserve">
410410
<value>IsTable</value>
411411
</data>
412+
<data name="IsFKs" xml:space="preserve">
413+
<value>IsFKs</value>
414+
</data>
412415
</root>

Server/Index.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class Index {
5050
public bool IsAllowOnlineRebuild { get; set; }
5151
public bool IsAllowCompression { get; set; }
5252
public bool IsTable { get; set; }
53+
public bool IsFKs { get; set; }
5354
public bool IsColumnstore => (IndexType == IndexType.CLUSTERED_COLUMNSTORE || IndexType == IndexType.NONCLUSTERED_COLUMNSTORE);
5455

5556
public string Error { get; set; }
@@ -172,7 +173,9 @@ public string GetQuery() {
172173
break;
173174

174175
case IndexOp.TRUNCATE_TABLE:
175-
sql = $"TRUNCATE TABLE {objectName};";
176+
sql = IsPartitioned
177+
? $"TRUNCATE TABLE {objectName} WITH (PARTITIONS ({partition}));"
178+
: $"TRUNCATE TABLE {objectName};";
176179
break;
177180

178181
case IndexOp.UPDATE_STATISTICS_SAMPLE:

Server/Query.cs

+18
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,22 @@ ObjectID INT NOT NULL
255255
DECLARE @MINUTE INT
256256
SET @MINUTE = DATEDIFF(MINUTE, GETUTCDATE(), GETDATE())
257257
258+
IF OBJECT_ID('tempdb.dbo.#FKs') IS NOT NULL
259+
DROP TABLE #FKs
260+
261+
CREATE TABLE #FKs (ObjectID INT PRIMARY KEY)
262+
INSERT INTO #FKs
263+
SELECT i.ObjectID
264+
FROM (
265+
SELECT DISTINCT ObjectID
266+
FROM #Indexes
267+
) i
268+
WHERE EXISTS(
269+
SELECT *
270+
FROM sys.foreign_keys f WITH(NOLOCK)
271+
WHERE f.[referenced_object_id] = i.ObjectID
272+
)
273+
258274
SELECT i.ObjectID
259275
, i.IndexID
260276
, i.IndexName
@@ -284,6 +300,7 @@ SELECT i.ObjectID
284300
, CreateDate = DATEADD(MINUTE, -@MINUTE, o.[create_date])
285301
, ModifyDate = DATEADD(MINUTE, -@MINUTE, o.[modify_date])
286302
, IsTable = CAST(CASE WHEN o.[type] = 'U' THEN 1 ELSE 0 END AS BIT)
303+
, IsFKs = CAST(CASE WHEN fk.ObjectID IS NULL THEN 0 ELSE 1 END AS BIT)
287304
, i.IsUnique
288305
, i.IsPK
289306
, i.FillFactorValue
@@ -296,6 +313,7 @@ SELECT i.ObjectID
296313
FROM #Indexes i
297314
JOIN sys.objects o WITH(NOLOCK) ON o.[object_id] = i.ObjectID
298315
JOIN sys.schemas s WITH(NOLOCK) ON s.[schema_id] = o.[schema_id]
316+
LEFT JOIN #FKs fk ON fk.ObjectID = i.ObjectID
299317
LEFT JOIN #Stats ss ON ss.ObjectID = i.ObjectID AND ss.IndexID = i.IndexID
300318
LEFT JOIN #AggColumns a ON a.ObjectID = i.ObjectID AND a.IndexID = i.IndexID
301319
LEFT JOIN #Sparse p ON p.ObjectID = i.ObjectID

Server/QueryEngine.cs

+1
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ public static List<Index> GetIndexes(SqlConnection connection) {
238238
Fragmentation = _.Field<double?>(Resources.Fragmentation),
239239
PageSpaceUsed = _.Field<double?>(Resources.PageSpaceUsed),
240240
IsTable = _.Field<bool>(Resources.IsTable),
241+
IsFKs = _.Field<bool>(Resources.IsFKs),
241242
IsAllowReorganize = _.Field<bool>(Resources.IsAllowPageLocks) && indexType != IndexType.HEAP,
242243
IsAllowOnlineRebuild = isOnlineRebuild,
243244
IsAllowCompression = Settings.ServerInfo.IsCompressionAvailable && !_.Field<bool>(Resources.IsSparse),

0 commit comments

Comments
 (0)