Maison  >  Article  >  base de données  >  SqlServer数据库迁移

SqlServer数据库迁移

WBOY
WBOYoriginal
2016-06-07 15:13:441053parcourir

1. 迁移 索引: --导出 数据库 的所有Index脚本 ----只生成索引脚本,不需要统计信息------------------ declare @tbname sysname ,@type char(1) set @tbname='';--表名,空返回所有表索引 set @type='1' ;--是否显示聚集索引,1显示聚集索引,2不显示聚集索

1.迁移索引:

--导出数据库的所有Index脚本
----只生成索引脚本,不需要统计信息------------------
declare @tbname sysname ,@type char(1)
set @tbname='';--表名,空返回所有表索引
set @type='1' ;--是否显示聚集索引,1显示聚集索引,2不显示聚集索引
with t as (
select rank() over (order by b.name,a.name,c.name) as id,c.index_id,
b.name as schema_name,a.name as table_name,c.fill_factor,c.is_padded,
c.name as ix_name,c.type,e.name as column_name,d.index_column_id,c.is_primary_key,
d.is_included_column,f.name as filegroup_name,c.is_unique,c.ignore_dup_key,
d.is_descending_key as is_descending_key,c.allow_row_locks,c.allow_page_locks
from sys.tables as a
inner join sys.schemas as b on a.schema_id=b.schema_id and a.is_ms_shipped=0
inner join sys.indexes as c on a.object_id=c.object_id
inner join sys.index_columns as d on d.object_id=c.object_id and d.index_id=c.index_id
inner join sys.columns as e on e.object_id=d.object_id and e.column_id=d.column_id
inner join sys.data_spaces as f on f.data_space_id=c.data_space_id
where a.object_id like '%'+isnull(ltrim(object_id(@tbname)),'')+'%'
and c.is_hypothetical=0 and is_disabled=0 and c.type>=@type
)

select k1.table_name,k1.ix_name,k1.sqlscript  from
(select distinct a.schema_name,a.table_name,a.ix_name,
case a.type when 1 then 'clustered' when 2 then 'nonclustered' else '' end as index_type,
case a.is_primary_key when 0 then 'no' else 'yes' end as is_primary_key,
m.ix_index_column_name,isnull(m.ix_index_include_column_name,'') as ix_index_include_column_name,
a.filegroup_name,replace('create '+ case when is_unique=1 then 'unique ' else '' end
+case when a.type=1 then 'clustered' else 'nonclustered' end  +' index '
+a.ix_name+' on '+a.schema_name+'.'+a.table_name+'('+m.ix_index_column_name+')'+
case when m.ix_index_include_column_name is null then '' else 'include('+m.ix_index_include_column_name+')'end
+ case when fill_factor>0 or ignore_dup_key=1 or is_padded=1 or allow_row_locks=0 or allow_page_locks=0 then 'with(' else '' end
+ case when fill_factor>0 then ',fillfactor='+rtrim(fill_factor) else '' end
+ case when is_padded=1 then ',pad_index=on' else '' end
+ case when ignore_dup_key=1 then ',ignore_dup_key=on' else '' end
+ case when allow_row_locks=0 then ',allow_row_locks=off' else '' end
+ case when allow_page_locks=0 then ',allow_page_locks=off' else '' end
+ case when fill_factor>0 or ignore_dup_key=1 or is_padded=1 or allow_row_locks=0 or allow_page_locks=0 then ')' else '' end,'with(,','with(')
 + ' with (online=on)' as sqlscript
from t as a
outer apply 

    select ix_index_column_name= stuff(replace(replace( 
            ( 
                select case when b.is_descending_key =1 then column_name + ' desc' else column_name end as column_name 
              from t as b where a.id=b.id and is_included_column=0 order by index_column_id for xml auto 
            ), '', ''), 1, 1, '') 
           ,ix_index_include_column_name= stuff(replace(replace( 
            ( 
                select column_name from t as b where a.id=b.id and is_included_column=1 
                order by index_column_id for xml auto 
            ), '', ''), 1, 1, '') 
)m 
)k1
order by k1.table_name,k1.ix_name
 
2.导出用户:

USE master
GO
IF OBJECT_ID ('sp_hexadecimal') IS NOT NULL
  DROP PROCEDURE sp_hexadecimal
GO
CREATE PROCEDURE sp_hexadecimal
    @binvalue varbinary(256),
    @hexvalue varchar (514) OUTPUT
AS
DECLARE @charvalue varchar (514)
DECLARE @i int
DECLARE @length int
DECLARE @hexstring char(16)
SELECT @charvalue = '0x'
SELECT @i = 1
SELECT @length = DATALENGTH (@binvalue)
SELECT @hexstring = '0123456789ABCDEF'
WHILE (@i BEGIN
  DECLARE @tempint int
  DECLARE @firstint int
  DECLARE @secondint int
  SELECT @tempint = CONVERT(int, SUBSTRING(@binvalue,@i,1))
  SELECT @firstint = FLOOR(@tempint/16)
  SELECT @secondint = @tempint - (@firstint*16)
  SELECT @charvalue = @charvalue +
    SUBSTRING(@hexstring, @firstint+1, 1) +
    SUBSTRING(@hexstring, @secondint+1, 1)
  SELECT @i = @i + 1
END

SELECT @hexvalue = @charvalue
GO
 
IF OBJECT_ID ('sp_help_revlogin') IS NOT NULL
  DROP PROCEDURE sp_help_revlogin
GO
CREATE PROCEDURE sp_help_revlogin @login_name sysname = NULL AS
DECLARE @name sysname
DECLARE @type varchar (1)
DECLARE @hasaccess int
DECLARE @denylogin int
DECLARE @is_disabled int
DECLARE @PWD_varbinary  varbinary (256)
DECLARE @PWD_string  varchar (514)
DECLARE @SID_varbinary varbinary (85)
DECLARE @SID_string varchar (514)
DECLARE @tmpstr  varchar (1024)
DECLARE @is_policy_checked varchar (3)
DECLARE @is_expiration_checked varchar (3)

DECLARE @defaultdb sysname
 
IF (@login_name IS NULL)
  DECLARE login_curs CURSOR FOR

      SELECT p.sid, p.name, p.type, p.is_disabled, p.default_database_name, l.hasaccess, l.denylogin FROM
sys.server_principals p LEFT JOIN sys.syslogins l
      ON ( l.name = p.name ) WHERE p.type IN ( 'S', 'G', 'U' ) AND p.name 'sa'
ELSE
  DECLARE login_curs CURSOR FOR


      SELECT p.sid, p.name, p.type, p.is_disabled, p.default_database_name, l.hasaccess, l.denylogin FROM
sys.server_principals p LEFT JOIN sys.syslogins l
      ON ( l.name = p.name ) WHERE p.type IN ( 'S', 'G', 'U' ) AND p.name = @login_name
OPEN login_curs

FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin
IF (@@fetch_status = -1)
BEGIN
  PRINT 'No login(s) found.'
  CLOSE login_curs
  DEALLOCATE login_curs
  RETURN -1
END
SET @tmpstr = ''
PRINT @tmpstr
PRINT ''
WHILE (@@fetch_status -1)
BEGIN
  IF (@@fetch_status -2)
  BEGIN
    PRINT ''
    SET @tmpstr = '-- Login: ' + @name
    PRINT @tmpstr
    IF (@type IN ( 'G', 'U'))
    BEGIN -- NT authenticated account/group

      SET @tmpstr = 'CREATE LOGIN ' + QUOTENAME( @name ) + ' FROM WINDOWS WITH DEFAULT_DATABASE = [' + @defaultdb + ']'
    END
    ELSE BEGIN -- SQL Server authentication
        -- obtain password and sid
            SET @PWD_varbinary = CAST( LOGINPROPERTY( @name, 'PasswordHash' ) AS varbinary (256) )
        EXEC sp_hexadecimal @PWD_varbinary, @PWD_string OUT
        EXEC sp_hexadecimal @SID_varbinary,@SID_string OUT
 
        -- obtain password policy state
        SELECT @is_policy_checked = CASE is_policy_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END FROM sys.sql_logins WHERE name = @name
        SELECT @is_expiration_checked = CASE is_expiration_checked WHEN 1 THEN 'ON' WHEN 0 THEN 'OFF' ELSE NULL END FROM sys.sql_logins WHERE name = @name
 
            SET @tmpstr = 'CREATE LOGIN ' + QUOTENAME( @name ) + ' WITH PASSWORD = ' + @PWD_string + ' HASHED, SID = ' + @SID_string + ', DEFAULT_DATABASE = [' + @defaultdb + ']'

        IF ( @is_policy_checked IS NOT NULL )
        BEGIN
          SET @tmpstr = @tmpstr + ', CHECK_POLICY = ' + @is_policy_checked
        END
        IF ( @is_expiration_checked IS NOT NULL )
        BEGIN
          SET @tmpstr = @tmpstr + ', CHECK_EXPIRATION = ' + @is_expiration_checked
        END
    END
    IF (@denylogin = 1)
    BEGIN -- login is denied access
      SET @tmpstr = @tmpstr + '; DENY CONNECT SQL TO ' + QUOTENAME( @name )
    END
    ELSE IF (@hasaccess = 0)
    BEGIN -- login exists but does not have access
      SET @tmpstr = @tmpstr + '; REVOKE CONNECT SQL TO ' + QUOTENAME( @name )
    END
    IF (@is_disabled = 1)
    BEGIN -- login is disabled
      SET @tmpstr = @tmpstr + '; ALTER LOGIN ' + QUOTENAME( @name ) + ' DISABLE'
    END
    PRINT @tmpstr
  END

  FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin
   END
CLOSE login_curs
DEALLOCATE login_curs
RETURN 0
GO
这段代码会在“master”数据库中创建两个存储过程。两个存储过程分别命名为“sp_hexadecimal”存储过程和“sp_help_revlogin”存储过程。

接下来运行sp_help_revlogin:exec sp_help_revlogin


3.给用户赋予对象权限:

--给数据库中的对象赋权
select 'grant '+ (case when type='U'  then ' select,insert,update,delete '
                       when type='V'  then ' select,insert,update,delete '
                       else ' exec ' end)
       +' on ['+name+'] to appconn'
 from sys.objects where is_ms_shipped=0  and type in('P','U','FN','V')


4.查询特定用户的权限,导出赋权脚本:

--原库用户权限导出
select       object_name(a.id)   as   objectname,  
        user_name(a.uid)   as   usename,  
        case   b.issqlrole   when   1   then   'Group   '  
              else   'User'  
        end   as   Role,  
        case   a.protecttype   when   205   then   'Grant'  
              when   204   then   'Grant'  
              when   206   then   'Deny'  
              else   'Revoke'  
        end   as   ProtectType,  
        case   a.[action]   when   26     then   'REFERENCES'  
                  when   178   then   'CREATE   FUNCTION'  
                  when   193   then   'SELECT'  
                  when   195   then   'INSERT'  
                  when   196   then   'DELETE'  
                  when   197   then   'UPDATE'  
                  when   198   then   'CREATE   TABLE'  
                  when   203   then   'CREATE   DATABASE'  
                  when   207   then   'CREATE   VIEW'  
                  when   222   then   'CREATE   PROCEDURE'  
                  when   224   then   'EXECUTE'  
                  when   228   then   'BACKUP   DATABASE'  
                  when   233   then   'CREATE   DEFAULT'  
                  when   235   then   'BACKUP   LOG'  
                  when   236   then   'CREATE   RULE'  
                  else   '0'  end   as   [Action],  
          user_name(a.grantor)   as   Grantor  
      from   sysprotects   a   inner   join   sysusers   b   on   a.uid=b.uid  
      where   exists   (select   1   from     sysobjects  
                    where   [name]=object_name(a.id)   and   xtype   'S'   )  
      and   (   exists   (select   1   from   sysmembers  
          where   groupuid=a.uid   and   memberuid=user_id(@username))  
      or   a.uid=user_id(@username))  
      order   by   object_name(a.id)  


--生成赋权语句
select protecttype+' '+action+' on '+ objectname+' to '+ usename
from (
select       object_name(a.id)   as   objectname,  
        user_name(a.uid)   as   usename,  
        case   b.issqlrole   when   1   then   'Group   '  
              else   'User'  
        end   as   Role,  
        case   a.protecttype   when   205   then   'Grant'  
              when   204   then   'Grant'  
              when   206   then   'Deny'  
              else   'Revoke'  
        end   as   ProtectType,  
        case   a.[action]   when   26     then   'REFERENCES'  
                  when   178   then   'CREATE   FUNCTION'  
                  when   193   then   'SELECT'  
                  when   195   then   'INSERT'  
                  when   196   then   'DELETE'  
                  when   197   then   'UPDATE'  
                  when   198   then   'CREATE   TABLE'  
                  when   203   then   'CREATE   DATABASE'  
                  when   207   then   'CREATE   VIEW'  
                  when   222   then   'CREATE   PROCEDURE'  
                  when   224   then   'EXECUTE'  
                  when   228   then   'BACKUP   DATABASE'  
                  when   233   then   'CREATE   DEFAULT'  
                  when   235   then   'BACKUP   LOG'  
                  when   236   then   'CREATE   RULE'  
                  else   '0'  end   as   [Action],  
          user_name(a.grantor)   as   Grantor  
      from   sysprotects   a   inner   join   sysusers   b   on   a.uid=b.uid  
      where   exists   (select   1   from     sysobjects  
                    where   [name]=object_name(a.id)   and   xtype   'S'   )  
      and   (   exists   (select   1   from   sysmembers  
          where   groupuid=a.uid   and   memberuid=user_id(@username))  
      or   a.uid=user_id(@username))  
      --order   by   object_name(a.id) 
) a


 

飞扬过海

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn