How to copy stored procedure from one database to another database on same server dynamically -


i'm trying write script copy stored procedure database on server:

select @def = [definition]  [@from_db].sys.sql_modules  object_id = object_id('myprocedure')   exec(@def);  

however doesn't work, how can copy stored procedure 1 database another?

try this

declare @sql nvarchar(max) = ''     ,@targetdbname nvarchar(255) = 'targetdbname'  declare c cursor select definition sourcedbname.[sys].[procedures] p inner join sourcedbname.sys.sql_modules m on p.object_id = m.object_id  open c  fetch next c @sql  while @@fetch_status = 0 begin     set @sql = replace(@sql, '''', '''''')     set @sql = 'use [' + @targetdbname + ']; exec(''' + @sql + ''')'      exec sp_executesql @sql      fetch next     c     @sql end  close c  deallocate c 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -