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
Post a Comment