简体   繁体   English

存储过程中的语法错误

[英]Syntax error on the stored procedure

Environment: SQL Server 2008 R2. 环境:SQL Server 2008 R2。

Here is a stored procedure that is being called by an APP to copy data from a set of tables from database to another database where they both have the same schema. 这是一个存储过程,APP会调用该存储过程将一组表中的数据从数据库复制到另一个具有相同架构的数据库。 It doesn't seem to copy the data. 它似乎没有复制数据。 when I try to run it manually on query analyser. 当我尝试在查询分析器上手动运行它时。

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

But I get this error, when I run it: 但是当我运行它时,我得到了这个错误:

Msg 102, Level 15, State 1, Line 1 Msg 102,第15级,状态1,第1行
Incorrect syntax near 'Server1\\Instance'. “ Server1 \\ Instance”附近的语法不正确。

Here is the stored procedure, I can't see how this could error. 这是存储过程,我看不到这怎么可能出错。 The databases are on the same server. 数据库位于同一服务器上。 Why am I getting the error? 为什么会出现错误?

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
exec dbo.sp_Copy_DB_Subset_Tables 'Server1\Instance','MainDB','Server1\Instance','MiniDB'

尝试使用括号吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM