Copy Stored Procedures Between Servers

This procedure will migrate all your stored procedures located on on server to another via linked server. Just specify the linked server name, remote database, and local database. CREATE PROCEDURE [dba].[pr_refresh_create_procedures]     @linked_server VARCHAR(100),     @source_db VARCHAR(100),     @target_db VARCHAR(100) AS --EXEC [dba].[pr_refresh_create_procedures] 'LOCALSERVER', 'yourdbname', 'targetdb_dev' SET @source_db = '[' + @linked_server + '].[' + @source_db + ']'; DECLARE @SQL Nvarchar(MAX) DECLARE @Name VARCHAR(255) = @target_db IF OBJECT_ID(N'dbadmin.tmp.shelldb_copy_procedures') IS NOT NULL DROP TABLE dbadmin.tmp.shelldb_copy_procedures SET @SQL = 'SELECT p.name, m.Definition INTO dbadmin.tmp.shelldb_copy_procedures FROM ' + @source_db + '.sys.objects p WITH (NOLOCK) INNER JOIN ' + @source_db + '.sys.sql_modules m WITH (NOLOCK) ON p.object_id = m.object_id WHERE type IN (''FN'', ''IF'', ''TF'', ''P'', ''V'', ''TT'')' EXEC(@SQL) DECLARE c CURSOR FOR    SELECT Definition    FROM dbadmin.tmp.shelldb_copy_procedures OPEN c FETCH NEXT FROM c INTO @SQL WHILE @@FETCH_STATUS = 0 BEGIN    SET @SQL = REPLACE(@SQL,'''','''''')    SET @SQL = N'execute ' + QUOTENAME(@name) + N'.dbo.sp_executesql N''' + @SQL + ''''    EXEC(@SQL)    FETCH NEXT FROM c INTO @SQL END             CLOSE c DEALLOCATE c Continue reading ...

Copy Table Data Between Servers

This script will copy table data from one environment to another. This script uses a multithreaded clr procedure created by Alan Kaplan here. You can also change the parallel call below if you want to execute serially. CREATE PROCEDURE [dba].[pr_refresh_insert_table_data]     @linked_server VARCHAR(100),     @source_db VARCHAR(100),     @target_db VARCHAR(100) AS -- dba.pr_refresh_insert_table_data '10.10.60.58', 'yourdbname', 'targetdb' -- SELECT * FROM dbadmin.dbo.refresh_table_columns BEGIN     SET NOCOUNT ON;     DECLARE @SQL nvarchar(MAX);     DECLARE @SQLColumnList nvarchar(MAX);     DECLARE @SQLSelectColumnList nvarchar(MAX);     DECLARE @CRLF nvarchar(2);     DECLARE @Tab nvarchar(1);     DECLARE @RecordKeyName nvarchar(50);     DECLARE @TableID INT;     DECLARE @TableCount INT;     DECLARE @SOURCE VARCHAR(MAX) = '[' + @linked_server + '].[' + @source_db + ']'     DECLARE @TableName VARCHAR(255);     SET @TableName = ''; -- Comment out to use list technique below     DECLARE @TableList TABLE (         TableID INT IDENTITY(1, 1) NOT NULL,         TableName VARCHAR(255)         );         DECLARE @InsertData Generic_Tt_VarcharMax_5_Col     INSERT INTO @TableList(TableName)     SELECT DISTINCT TABLE_NAME     FROM DBADMIN.[tmp].[refresh_table_schema_list] tsl     WHERE process_data = 1     AND EXISTS (SELECT 1 FROM dbadmin.tmp.refresh_create_tables WHERE TABLE_NAME = tsl.TABLE_NAME)     SET @CRLF = CHAR(13) + CHAR(10);     SET @Tab = CHAR(9);       SET @TableCount = (SELECT COUNT(1) FROM @TableList);     SET @TableID = 1; DECLARE @has_identity INT     SET @TableID = 1;   […]

Copy Table DDL Between Servers

This script will allow you to specify a linked server name, a remote database on that linked server, and a local database name in which to copy all the tables from on server onto another. CREATE PROCEDURE [dba].[pr_refresh_create_tables]     @linked_server VARCHAR(100),     @source_db VARCHAR(100),     @target_db VARCHAR(100),     @debug_mode INT = 0 AS -- dba.pr_refresh_create_tables '10.10.60.58', 'yourdbname', 'targetdb', 0 -- SELECT * FROM dbadmin.dbo.refresh_table_columns BEGIN     DECLARE @SQL nvarchar(MAX);     DECLARE @SQLColumnList nvarchar(MAX);     DECLARE @SQLSelectColumnList nvarchar(MAX);     DECLARE @CRLF nvarchar(2);     DECLARE @Tab nvarchar(1);     DECLARE @RecordKeyName nvarchar(50);     DECLARE @RecordKeyDataType nvarchar(50);     DECLARE @TableID INT;     DECLARE @TableCount INT;     DECLARE @SOURCE VARCHAR(MAX) = '[' + @linked_server + '].[' + @source_db + ']'     DECLARE @TableName VARCHAR(255);     DECLARE @TableList TABLE (         TableID INT IDENTITY(1, 1) NOT NULL,         TableName VARCHAR(255)         );     IF OBJECT_ID(N'dbadmin.dbo.refresh_table_columns_tmp') IS NOT NULL DROP TABLE dbadmin.dbo.refresh_table_columns_tmp     IF OBJECT_ID(N'dbadmin.dbo.refresh_table_columns') IS NOT NULL DROP TABLE dbadmin.dbo.refresh_table_columns     IF OBJECT_ID(N'dbadmin.tmp.refresh_create_tables') IS NOT NULL DROP TABLE dbadmin.tmp.refresh_create_tables     CREATE TABLE dbadmin.dbo.refresh_table_columns_tmp (         RowID INT NOT NULL,         TableName VARCHAR(255),         ColumnName VARCHAR(255),         IsIdentity bit,         DataType VARCHAR(50),         IsDataTypeCast bit,         IsNullable bit,         IsLast bit       […] Continue reading ...

Toggle Jobs On and Off

This is a useful script if you want to turn a specific list of jobs that are currently enable to a disabled status and vice versa. Just specify the list of job names in the VALUES section below: USE msdb GO IF OBJECT_ID(N'tempdb..#jobs') IS NOT NULL BEGIN     DROP TABLE #jobs END CREATE TABLE #jobs (     job_name nvarchar(MAX), ) INSERT INTO #jobs(job_name) VALUES     ('Job Name 1 here'),     ('Job Name 2 here'),     ('Run index selection Job') IF OBJECT_ID(N'tempdb..#job_status') IS NOT NULL BEGIN     DROP TABLE #job_status END SELECT ID = IDENTITY(INT, 1,1),     [job_name], enabled INTO #job_status FROM [msdb].[dbo].[sysjobs] AS [sJOB] JOIN #jobs j     ON j.job_name = sJOB.name DECLARE @job_name nvarchar(MAX) = ''; DECLARE @enabled INT; DECLARE @iter INT = (SELECT MAX(ID) FROM #job_status); WHILE @iter > 0 BEGIN     SET @job_name = ''     SELECT @job_name = job_name,         @enabled = CASE WHEN [enabled] = 0 THEN 1 ELSE 0 END     FROM #job_status     WHERE ID = @iter;     EXEC msdb.dbo.sp_update_job @job_name = @job_name,    @enabled = @enabled ;     PRINT CAST(@job_name AS VARCHAR) + ':' + CAST(@enabled AS VARCHAR)     SET @iter -= 1; END

Find CPU from within SSMS

This is a great DMV if you don’t want to leave SSMS to see the CPU usage or if you don’t have access. What’s good about this method is it also shows you the history of the cpu usage. DECLARE @ts_now BIGINT SELECT @ts_now = cpu_ticks / (cpu_ticks/ms_ticks) FROM sys.dm_os_sys_info; SELECT record_id, dateadd(ms, -1 * (@ts_now - [TIMESTAMP]), GetDate()) AS EventTime, SQLProcessUtilization, SystemIdle, 100 - SystemIdle - SQLProcessUtilization AS OtherProcessUtilization FROM ( SELECT record.VALUE('(./Record/@id)[1]', 'int') AS record_id, record.VALUE('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]', 'int') AS SystemIdle, record.VALUE('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]', 'int') AS SQLProcessUtilization, TIMESTAMP FROM ( SELECT TIMESTAMP, CONVERT(xml, record) AS record FROM sys.dm_os_ring_buffers WHERE ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR' AND record LIKE '%<SystemHealth>%') AS x ) AS y ORDER BY record_id DESC Continue reading ...

Featured Articles

 Site Author