Skip to content

Commit 561dccc

Browse files
authored
Merge pull request #149 from flarestudiopl/reseed-quotename-fix
SqlServer reseed fix for objects with special character in name
2 parents 98f3789 + 122dd78 commit 561dccc

File tree

1 file changed

+39
-41
lines changed

1 file changed

+39
-41
lines changed

Respawn/SqlServerDbAdapter.cs

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ public string BuildRelationshipCommandText(RespawnerOptions options)
103103
sfk.name
104104
from
105105
sys.foreign_keys sfk
106-
inner join sys.objects so_pk on sfk.referenced_object_id = so_pk.object_id
107-
inner join sys.schemas pk_schema on so_pk.schema_id = pk_schema.schema_id
108-
inner join sys.objects so_fk on sfk.parent_object_id = so_fk.object_id
109-
inner join sys.schemas fk_schema on so_fk.schema_id = fk_schema.schema_id
106+
inner join sys.objects so_pk on sfk.referenced_object_id = so_pk.object_id
107+
inner join sys.schemas pk_schema on so_pk.schema_id = pk_schema.schema_id
108+
inner join sys.objects so_fk on sfk.parent_object_id = so_fk.object_id
109+
inner join sys.schemas fk_schema on so_fk.schema_id = fk_schema.schema_id
110110
where 1=1";
111111

112112
if (options.TablesToIgnore.Any())
@@ -203,43 +203,41 @@ public string BuildDeleteCommandText(GraphBuilder graph, RespawnerOptions option
203203

204204
public string BuildReseedSql(IEnumerable<Table> tablesToDelete)
205205
{
206-
string sql =
207-
"DECLARE @Schema sysname = N'' \n" +
208-
"DECLARE @TableName sysname = N'' \n" +
209-
"DECLARE @ColumnName sysname = N'' \n" +
210-
"DECLARE @DoReseed sql_variant = 0 \n" +
211-
"DECLARE @NewSeed bigint = 0 \n" +
212-
"DECLARE @IdentityInitialSeedValue int = 0 \n" +
213-
"DECLARE @SQL nvarchar(4000) = N'' \n" +
214-
" \n" +
215-
"-- find all non-system tables and load into a cursor \n" +
216-
"DECLARE IdentityTables CURSOR FAST_FORWARD \n" +
217-
"FOR \n" +
218-
" SELECT OBJECT_SCHEMA_NAME(t.object_id, db_id()) as schemaName, \n" +
219-
" t.name as tableName, \n" +
220-
" c.name as columnName, \n" +
221-
" ic.last_value, \n" +
222-
" IDENT_SEED(OBJECT_SCHEMA_NAME(t.object_id, db_id()) + '.' + t.name) as identityInitialSeedValue \n" +
223-
" FROM sys.tables t \n" +
224-
" JOIN sys.columns c ON t.object_id=c.object_id \n" +
225-
" JOIN sys.identity_columns ic on ic.object_id = c.object_id \n" +
226-
" WHERE c.is_identity = 1 \n" +
227-
$" AND OBJECT_SCHEMA_NAME(t.object_id, db_id()) + '.' + t.name in ('{string.Join("', '", tablesToDelete)}') \n" +
228-
"OPEN IdentityTables \n" +
229-
"FETCH NEXT FROM IdentityTables INTO @Schema, @TableName, @ColumnName, @DoReseed, @IdentityInitialSeedValue \n" +
230-
"WHILE @@FETCH_STATUS = 0 \n" +
231-
" BEGIN \n" +
232-
" -- reseed the identity only on tables that actually have had a value, otherwise next value will be off-by-one \n" +
233-
" -- https://stackoverflow.com/questions/472578/dbcc-checkident-sets-identity-to-0 \n" +
234-
" if (@DoReseed is not null) \n" +
235-
" SET @SQL = N'DBCC CHECKIDENT(''' + @Schema + '.' + @TableName + ''', RESEED, ' + Convert(varchar(max), @IdentityInitialSeedValue - 1) + ')' \n" +
236-
" else \n" +
237-
" SET @SQL = null \n" +
238-
" if (@sql is not null) EXECUTE (@SQL) \n" +
239-
" --Print isnull(@sql, @Schema + '.' + @TableName + ' null') \n" +
240-
" FETCH NEXT FROM IdentityTables INTO @Schema, @TableName, @ColumnName , @DoReseed, @IdentityInitialSeedValue \n" +
241-
" END \n" +
242-
" DEALLOCATE IdentityTables \n";
206+
string sql = $@"
207+
DECLARE @Schema sysname = N''
208+
DECLARE @TableName sysname = N''
209+
DECLARE @DoReseed sql_variant = 0
210+
DECLARE @NewSeed bigint = 0
211+
DECLARE @IdentityInitialSeedValue int = 0
212+
DECLARE @SQL nvarchar(4000) = N''
213+
214+
-- find all non-system tables and load into a cursor
215+
DECLARE IdentityTables CURSOR FAST_FORWARD
216+
FOR
217+
SELECT QUOTENAME(OBJECT_SCHEMA_NAME(t.object_id, db_id())) as schemaName,
218+
QUOTENAME(t.name) as tableName,
219+
ic.last_value,
220+
IDENT_SEED(QUOTENAME(OBJECT_SCHEMA_NAME(t.object_id, db_id())) + '.' + QUOTENAME(t.name)) as identityInitialSeedValue
221+
FROM sys.tables t
222+
JOIN sys.columns c ON t.object_id=c.object_id
223+
JOIN sys.identity_columns ic on ic.object_id = c.object_id
224+
WHERE c.is_identity = 1
225+
AND OBJECT_SCHEMA_NAME(t.object_id, db_id()) + '.' + t.name in ('{string.Join("', '", tablesToDelete)}')
226+
OPEN IdentityTables
227+
FETCH NEXT FROM IdentityTables INTO @Schema, @TableName, @DoReseed, @IdentityInitialSeedValue
228+
WHILE @@FETCH_STATUS = 0
229+
BEGIN
230+
-- reseed the identity only on tables that actually have had a value, otherwise next value will be off-by-one
231+
-- https://stackoverflow.com/questions/472578/dbcc-checkident-sets-identity-to-0
232+
if (@DoReseed is not null)
233+
SET @SQL = N'DBCC CHECKIDENT(''' + @Schema + '.' + @TableName + ''', RESEED, ' + Convert(varchar(max), @IdentityInitialSeedValue - 1) + ')'
234+
else
235+
SET @SQL = null
236+
if (@sql is not null) EXECUTE (@SQL)
237+
--Print isnull(@sql, @Schema + '.' + @TableName + ' null')
238+
FETCH NEXT FROM IdentityTables INTO @Schema, @TableName, @DoReseed, @IdentityInitialSeedValue
239+
END
240+
DEALLOCATE IdentityTables";
243241

244242
return sql;
245243
}

0 commit comments

Comments
 (0)