script for insert sequences

Hello,

I try to do a script for transfer the sequences from a db to another db. Ihave these errors :
Msg 102, Level 15, State 1, Procedure vcf_transfer_sequences, Line 50
Incorrect syntax near '+ @Dbsource + '.
Msg 137, Level 15, State 2, Procedure vcf_transfer_sequences, Line 56
Must declare the scalar variable "@Schsource".

Do you have a solution ?
CREATE PROCEDURE [dbo].[move_sequences]
@Srvsource varchar (30),
@Dbsource varchar(30),
@DbDest varchar(30),
@SchDest varchar(30),
@Pwd varchar (30)

AS

EXEC master.dbo.sp_addlinkedserver @server = @Srvsource, @srvproduct=N'SQL Server'
 /* For security reasons the linked server remote logins password is changed with ######## */

declare @sql1 nvarchar(max);
set @sql1 = 'EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname =N''' + @Srvsource + ''', @useself=N''False'',@locallogin=NULL,@rmtuser=''sa'',@rmtpassword=''' +@Pwd + ''' '
exec sp_executesql @sql1

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'collation compatible', @optvalue=N'false'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'data access', @optvalue=N'true'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'dist', @optvalue=N'false'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'pub', @optvalue=N'false'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'rpc', @optvalue=N'true'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'rpc out', @optvalue=N'true'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'sub', @optvalue=N'false'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'connect timeout', @optvalue=N'0'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'collation name', @optvalue=null

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'lazy schema validation', @optvalue=N'false'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'query timeout', @optvalue=N'0'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'use remote collation', @optvalue=N'true'

declare @sql varchar(max)
set @sql = 'INSERT INTO ['+@DbDest+ '].'+ [dbo].[SEQUENCE]
           ([name]
           ,[usr]
           ,[value]
                  )

select [name], [usr], [value] FROM ['+@Srvsource + '].'+ @Dbsource + '.'[dbo].[SS_SEQUENCE] where [usr] = '@Schsource''
execute (@sql)
print (@sql)

UPDATE [dbo].[SEQUENCE]
SET [usr] = @Schdest
where [usr] = @Schsource

IF  EXISTS (SELECT srv.name FROM sys.servers srv WHERE srv.server_id != 0
AND srv.name = @Srvsource)
EXEC master.dbo.sp_dropserver @server=@Srvsource, @droplogins='droplogins'

Thanks

bibi
bibi92Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bibi92Author Commented:
Hello,

I have another error with this script :

ALTER PROCEDURE [dbo].[move_sequences]
@Srvsource varchar (30),
@Dbsource varchar(30),
@Schsource varchar(30),
@SchDest varchar(30),
@DbDest varchar(30),
@Pwd varchar (30)

AS


EXEC master.dbo.sp_addlinkedserver @server = @Srvsource, @srvproduct=N'SQL Server'
 /* For security reasons the linked server remote logins password is changed with ######## */

declare @sql1 nvarchar(max);
set @sql1 = 'EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname =N''' + @Srvsource + ''', @useself=N''False'',@locallogin=NULL,@rmtuser=''sa'',@rmtpassword=''' +@Pwd + ''' '
exec sp_executesql @sql1

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'collation compatible', @optvalue=N'false'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'data access', @optvalue=N'true'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'dist', @optvalue=N'false'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'pub', @optvalue=N'false'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'rpc', @optvalue=N'true'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'rpc out', @optvalue=N'true'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'sub', @optvalue=N'false'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'connect timeout', @optvalue=N'0'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'collation name', @optvalue=null

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'lazy schema validation', @optvalue=N'false'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'query timeout', @optvalue=N'0'

EXEC master.dbo.sp_serveroption @server=@Srvsource, @optname=N'use remote collation', @optvalue=N'true'

declare @sql varchar(max)
set @sql = 'INSERT INTO ['+@DbDest+ '].'+ [dbo].[SEQUENCE]
           ([name]
           ,[usr]
           ,[value]
                  ) select [name], [usr], [value]

set @sql=@sql+' FROM ['+@Srvsource + '].['+ @Dbsource + '].dbo.SEQUENCE'
set @sql=@sql+' where [usr] = '''+@Schsource+''' execute (@sql)'

execute (@sql)
print (@sql)

UPDATE [dbo].[SEQUENCE]
SET [usr] = '@Schdest'
where [usr] = '@Schsource'

IF  EXISTS (SELECT srv.name FROM sys.servers srv WHERE srv.server_id != 0
AND srv.name = @Srvsource)
EXEC master.dbo.sp_dropserver @server=@Srvsource, @droplogins='droplogins'

Regards

bibi
0
Tim HumphriesDirectorCommented:
In the update statement:

UPDATE [dbo].[SEQUENCE]
SET [usr] = '@Schdest'
where [usr] = '@Schsource'

you have your variable references in quotes. Remove them.

Were there any other errors?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BodestoneCommented:
here:
set @sql=@sql+' FROM ['+@Srvsource + '].['+ @Dbsource + '].dbo.SEQUENCE'
set @sql=@sql+' where [usr] = '''+@Schsource+''' execute (@sql)'

Why do you have execute(@sql) as part of the  @sql variable?
--here

Open in new window

0
bibi92Author Commented:
Thanks a lot
bibi
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.