So I know how to do this for all my tables:
DECLARE @month VARCHAR(9)
DECLARE @db VARCHAR(10)
DECLARE @sql NVARCHAR(max)
SET @month = (SELECT Datename(month, Getdate()))
SET @db = 'MyDatabase'
SET @sql = 'SELECT * INTO ' + @db + '.dbo.MyTable_' + @month
+ ' FROM ' + @db + '.dbo.MyTable'
PRINT @sql
EXEC Sp_executesql @sql
But I can’t fit several large SPs into @sql variables and even if I could apparently:
CREATE/ALTER PROCEDURE’ does not allow specifying the database name as a prefix to the object name
Is there a way to script an SP transfer without its entire text?
If so, how can I get the database name in a variable for USE [dbname]
I know with sqlmcd mode I can do something like
:setvar dbname "MyDatabase"
USE $(dbname)
but I would need to have the dbname as a variable also like
DECLARE @db VARCHAR(10)
SET @db = (SELECT Datename(year, Getdate()))
:setvar dbname @db
USE $(dbname)
So, the goal is to archive off a bunch of SPs each Month into a database, with new db for each year without having to ever manually change the script.
I’m going to go out on a limb and assume you are trying to archive the source code of your stored procedures each month:
The
definitioncolumn in the table will contain the stored procedure code.