Note that there are some explanatory texts on larger screens.

plurals
  1. POSyntax error on the stored procedure
    primarykey
    data
    text
    <p>Environment: SQL Server 2008 R2.</p> <p>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. It doesn't seem to copy the data. when I try to run it manually on query analyser. </p> <pre><code>exec dbo.sp_Copy_DB_Subset_Tables ('Server1\Instance','MainDB','Server1\Instance','MiniDB') </code></pre> <p>But I get this error, when I run it:</p> <blockquote> <p>Msg 102, Level 15, State 1, Line 1<br> Incorrect syntax near 'Server1\Instance'.</p> </blockquote> <p>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?</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload