declare @tablename varchar(1000),@importnew nvarchar(1000),@addconstraint nvarchar(1000),@dropold nvarchar(1000),@dorename nvarchar(1000),@dname nvarchar(1000),@dquery nvarchar(1000)
declare tables cursor for select TABLE_NAME from INFORMATION_SCHEMA.TABLES
open tables
fetch next from tables into @tablename
while @@FETCH_STATUS = 0
begin
print 'processing ' + @tablename
if exists(select column_name from INFORMATION_SCHEMA.columns where table_name = @tablename and column_name = 'idold')
begin
set @dropold = 'alter table '+ @tablename +' drop column idold'
exec sp_executesql @dropold
end
if not exists(select column_name from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where TABLE_NAME = @tablename)
begin
if exists(select column_name from INFORMATION_SCHEMA.columns where table_name = @tablename and column_name = 'id')
begin
set @dorename = 'exec sp_rename "' + @tablename + '.id", "idold", "column"'
exec sp_executesql @dorename
set @importnew = 'select identity(numeric(19,0)) as id,* into ' + @tablename +'_tmp from ' + @tablename + ' where 1=0'
exec sp_executesql @importnew
set @dname = null
select @dname = coalesce(@dname+',','') + column_name from information_schema.columns where table_name=@tablename
print @dname
set @dquery = 'set identity_insert '+ @tablename + '_tmp on;insert into ' + @tablename + '_tmp (id, '+ @dname +' ) select idold,* from ' + @tablename + ';set identity_insert '+ @tablename + '_tmp off'
exec sp_executesql @dquery
end
else
begin
set @importnew = 'select identity(numeric(19,0)) as id,* into ' + @tablename +'_tmp from ' + @tablename
exec sp_executesql @importnew
end
set @addconstraint = 'alter table ' + @tablename + '_tmp add constraint ' + @tablename + '_pk primary key (id)'
exec sp_executesql @addconstraint
set @dropold = 'drop table ' + @tablename
exec sp_executesql @dropold
set @dorename = 'exec sp_rename ' + @tablename + '_tmp, ' + @tablename
exec sp_executesql @dorename
if exists(select column_name from INFORMATION_SCHEMA.columns where table_name = @tablename and column_name = 'idold')
begin
set @dropold = 'alter table '+ @tablename +' drop column idold'
exec sp_executesql @dropold
end
end
fetch next from tables into @tablename
end
It took a lot of googling to get that much. In that tiny piece of accumulated wisdom is how you would create a cursor so that you can loop over the items, how you would check whether a field exist or not in a particular table, does the table already have a primary key, how to rename column, how to insert value into an identity field by setting identity_insert to on, how to get a list of table fields and turn it into a comma separated string so that you can put that into another query. Phew.. But most importantly it allows me to import and export my ms sql 2008 database and know that this time the id field would still be intact and functioning (after running the script of course).
/me misses mysql, postgres and zodb... :(