0

环境:SQL Server 2008 R2。

这是一个由 APP 调用的存储过程,用于将数据从一组表从数据库复制到另一个数据库,其中它们都具有相同的模式。它似乎没有复制数据。当我尝试在查询分析器上手动运行它时。

exec dbo.sp_Copy_DB_Subset_Tables ('Server1\Instance','MainDB','Server1\Instance','MiniDB')

但是当我运行它时出现此错误:

消息 102,级别 15,状态 1,第 1 行
'Server1\Instance' 附近的语法不正确。

这是存储过程,我看不出这怎么会出错。数据库位于同一台服务器上。为什么我会收到错误消息?

CREATE  PROCEDURE [dbo].[sp_Copy_MYDB_Subset_Tables](
    @vSourceServer      varchar(255)
    ,@vSourceDatabase   varchar(255) = 'MYDB'
    ,@vDestinationServer    varchar(255)
    ,@vDestinationDatabase  varchar(255) = 'MYDB'
    ,@vIsServerOnDomain     BIT = 1 --
    ,@TargetDBUserName  varchar(255) = ''
    ,@TargetDBPassword  varchar(255) = ''
    )  
AS
BEGIN 
    Declare 
    @vSourceTable   varchar(255)
    ,@vSourceSelectQuery    varchar(255)
    ,@vDestinationTable     varchar(255)
    ,@vReturn               int 
    ,@vReturnMessage        varchar(max) 
    ,@vPeriodtoArchive      int
    ,@ColumnMappings        varchar(4000)


BEGIN TRY
    if (@vSourceServer is null or @vSourceServer = '')
        set @vSourceServer = @@servername

    if object_id('tempdb..#TempTableCopyList') is not null
        drop table #TempTableCopyList

    Create Table #TempTableCopyList
    (
        id [int] NOT NULL  primary key clustered
        ,TableName      varchar(100)
        ,ColumnMappings varchar(4000)
        ,DateCopied     datetime
    )

    insert into #TempTableCopyList
        Select id, TableName, ColumnMappings, DateCopied
        from dbo.fn_Get_MYDB_Subset_TableList()

    declare c cursor for 
    Select TableName, ColumnMappings 
        from #TempTableCopyList
           order by id desc
    open c

    fetch next from c into @vSourceTable, @ColumnMappings

    While @@fetch_status = 0 
    BEGIN
       print 'Start Copying table: ' + @vSourceTable + ' at ' + convert(varchar(30),getdate(),120)

       Set @vSourceSelectQuery = 'Select * from ' + @vSourceTable + ' with (nolock) '

       IF @vIsServerOnDomain = 0
        BEGIN
                exec master.dbo.usp_BulkCopy 
                     @vSourceServer
                    ,@vSourceDatabase
                    ,@vSourceSelectQuery
                    ,@vDestinationServer
                    ,@vDestinationDatabase
                    ,@vSourceTable
                    ,1
                    ,1
                    ,true
                    ,false
                    ,''
                    ,''
                    ,@TargetDBUserName  
                    ,@TargetDBPassword
                    ,@ColumnMappings
        END
        ELSE BEGIN

                exec master.dbo.usp_BulkCopy 
                     @vSourceServer
                    ,@vSourceDatabase
                    ,@vSourceSelectQuery
                    ,@vDestinationServer
                    ,@vDestinationDatabase
                    ,@vSourceTable
                    ,1
                    ,1
                    ,true
                    ,true
                    ,''
                    ,''
                    ,''
                    ,''
                    ,@ColumnMappings
        END
                UPDATE #TempTableCopyList
                    set DateCopied = GETDATE()
                WHERE TableName = @vSourceTable




        fetch next from c into @vSourceTable, @ColumnMappings

    END

    close c
    deallocate c
END TRY
BEGIN CATCH
    close c
    deallocate c
    DECLARE @ErrorMessage VARCHAR(MAX)
    SET @ErrorMessage = error_message()
    print @vSourceTable + '; '+ @vSourceServer+ '; '+  @vSourceDatabase+ '; '+ @vDestinationServer+ '; '+ @vDestinationDatabase+ '; '+ @vDestinationTable
    Print @ErrorMessage
    RAISERROR (@ErrorMessage, 0, 1)
END CATCH

    --INFORMATIONAL
    SELECT * FROM #TempTableCopyList

    drop table #TempTableCopyList

   return 
END
4

1 回答 1

3
exec dbo.sp_Copy_DB_Subset_Tables 'Server1\Instance','MainDB','Server1\Instance','MiniDB'

不带括号试试?

于 2013-10-22T13:10:22.433 回答