将NAV数据库结构与SQL中的数据一起复制到另一个数据库

时间:2015-03-31 13:05:59

标签: sql sql-server-2012 navision

我需要使用SQL Query将NAV数据库中的公司复制到不同数据库中的另一家公司。我使用下面的代码实现了这一点,但如果源或目标中的结构不同,我的代码将失败。请帮我修改它,这样我就可以从Source中复制Structure和Data,并替换Destination中的Data和Structure。 代码如下。

    -- =============================================
    -- Author:        Prajeesh 
    -- Create date:   31.03.2015
    -- Description:    Function for copying comany from one database to another
    -- =============================================
    Create PROCEDURE [dbo].[sp_NAVCopyCompany_v2] 
        @sourcecompany varchar(max), 
        @targetdb varchar(max),
        @targetcompany varchar(max)
    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
        -- Insert statements for procedure here
        declare @tablename varchar(1000)
        declare @columns varchar(max)
        declare @columnname varchar (max)
        declare @targettable varchar (max)
        declare @isidentity int
        declare @sqlcommand nvarchar (max) = 'select name from '+@targetdb+'.sys.all_objects where type=''U'' and object_id>0 and name like '''+@sourcecompany+'$%'''
        declare @sqlcommandIdentity nvarchar (max)
        declare @tablevar table(name varchar(300))
        declare @columntablevar table(COLUMN_NAME varchar(300))
        declare @identitytablevar table(C int)
        insert into @tablevar(name) exec sp_executesql  @sqlcommand
        DECLARE table_cursor CURSOR for
          select name from @tablevar
        OPEN table_cursor

        FETCH NEXT FROM table_cursor 
        INTO @tablename
        WHILE @@FETCH_STATUS = 0
        BEGIN
            --RAISERROR (@tablename, 0, 1) WITH NOWAIT 
               set @sqlcommand = 'SELECT COLUMN_NAME FROM '+@targetdb+'.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '''+@tablename+''' and COLUMN_NAME <> ''timestamp'''
            DELETE from @columntablevar
            insert into @columntablevar(COLUMN_NAME) exec sp_executesql  @sqlcommand
            DECLARE column_cursor CURSOR for select COLUMN_NAME from @columntablevar

            select @columns=''
            OPEN column_cursor
            FETCH NEXT from column_cursor
            INTO @columnname
            WHILE @@FETCH_STATUS=0
            BEGIN
                SELECT @columns=@columns+',['+@columnname+']'
                FETCH NEXT from column_cursor
                INTO @columnname
            END
            CLOSE column_cursor;
            DEALLOCATE column_cursor;
            select @columns = SUBSTRING(@columns,2,LEN(@columns)-1)
            --RAISERROR (@columns, 0, 1) WITH NOWAIT 
            select @targettable= @targetdb+'.dbo.['+@targetcompany+SUBSTRING(@tablename,LEN(@sourcecompany)+1,LEN(@tablename)-LEN(@sourcecompany)+1)+']'
            --
            select @isidentity=COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =@tablename AND COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
            --
            set @sqlcommandIdentity = 'SELECT COUNT(*) as C FROM '+@targetdb+'.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ='''+@tablename+''' AND COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, ''IsIdentity'') = 1'
            DELETE from @identitytablevar
            insert into @identitytablevar(C) exec sp_executesql  @sqlcommandIdentity
            select @isidentity=SUM(C) FROM @identitytablevar

            RAISERROR (@targettable, 0, 1) WITH NOWAIT 
            set @sqlcommand = ''
            IF (@isidentity>0)
              set @sqlcommand = @sqlcommand + 'SET IDENTITY_INSERT '+@targettable+' ON;'

            set @sqlcommand = @sqlcommand + 'delete from '+@targettable+';'
            set @sqlcommand = @sqlcommand + 'insert into '+@targettable+ ' ('+ @columns + ')'
            + '    select '+@columns
            + '       from ['+@tablename+']' 

            IF (@isidentity>0)
              set @sqlcommand = @sqlcommand + ';SET IDENTITY_INSERT '+@targettable+' OFF'
            --RAISERROR (@sqlcommand, 0, 1) WITH NOWAIT 
            exec sp_executesql @sqlcommand
            FETCH NEXT FROM table_cursor 
            INTO @tablename
        END 
        CLOSE table_cursor;
        DEALLOCATE table_cursor;
    END

查询被称为

use SourceDatabase
exec sp_NAVCopyCompany_v2 'Source Company_','Destination Database','Destination Company'

0 个答案:

没有答案