Skip to content

Commit

Permalink
open sourcing scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Michelle Ufford committed Jun 19, 2015
1 parent e20ba65 commit 6712f8b
Show file tree
Hide file tree
Showing 16 changed files with 2,369 additions and 0 deletions.
123 changes: 123 additions & 0 deletions admin/dba_recompile_sp.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
Use dbaTools;
Go

If ObjectProperty(Object_ID('dbo.dba_recompile_sp'), N'IsProcedure') Is Null
Begin
Execute ('Create Procedure dbo.dba_recompile_sp As Print ''Hello World!''')
RaisError('Procedure dba_recompile_sp created.', 10, 1);
End;
Go

Set ANSI_Nulls On;
Set Quoted_Identifier On;
Go

Alter Procedure dbo.dba_recompile_sp

/* Declare Parameters */
@databaseName nvarchar(128) = Null /* Null = all databases */
, @tableName nvarchar(128) = Null /* Null = all tables */

As
/**********************************************************************************************************
NAME: dba_recompile_sp
SYNOPSIS: Recompiles all procs in a specific database or all procs; can recompile a specific table, too.
DEPENDENCIES: The following dependencies are required to execute this script:
- SQL Server 2005 or newer
AUTHOR: Michelle Ufford, http://sqlfool.com
CREATED: 2009-09-12
VERSION: 1.0
LICENSE: Apache License v2
----------------------------------------------------------------------------
DISCLAIMER:
This code and information are provided "AS IS" without warranty of any kind,
either expressed or implied, including but not limited to the implied
warranties or merchantability and/or fitness for a particular purpose.
----------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
-- DATE VERSION AUTHOR DESCRIPTION --
---------------------------------------------------------------------------------------------------------
20150619 1.0 Michelle Ufford Open Sourced on GitHub
**********************************************************************************************************/

Set NoCount On;
Set XACT_Abort On;
Set Ansi_Padding On;
Set Ansi_Warnings On;
Set ArithAbort On;
Set Concat_Null_Yields_Null On;
Set Numeric_RoundAbort Off;

Begin

/* Make sure the global temp tables do not already exist, i.e. failed execution */
If Exists(Select * From tempdb.sys.tables Where name = '###databaseList')
Drop Table #databaseList;

If Exists(Select * From tempdb.sys.tables Where name = '##tableList')
Drop Table tableList;

/* Declare Temp Tables */
Create Table ##databaseList
(
databaseName nvarchar(128)
, processed bit
);

Create Table ##tableList
(
databaseName nvarchar(128)
, tableName nvarchar(128)
, processed bit
);

Insert Into ##databaseList
Select name As databaseName
, 0 As processed
From sys.databases
Where name = IsNull(@databaseName, name);

While Exists(Select Top 1 databaseName From ##databaseList Where processed = 0)
Begin

Execute sp_msforeachdb 'Use ?;
Select name As tableName
Into ##tableList
From sys.tables
Where name = IsNull(@tableName, name);
Declare @tableName nvarchar(128) = (Select Top 1 tableName From #tableList);
While Exists(Select Top 1 * From #tableList)
Begin
Execute sp_recompile @tableName;
Delete From #tableList Where tableName = @tableName;
Select Top 1 @tableName = tableName From #tableList Order By tableName;
End;
Drop Table ##tableList;'

End

Set NoCount Off;
Return 0;
End
Go

Set Quoted_Identifier Off;
Go

If ObjectProperty(Object_ID('dbo.dba_recompile_sp'), N'IsProcedure') = 1
RaisError('Procedure dba_recompile_sp was successfully updated.', 10, 1);
Else
RaisError('Procedure dba_recompile_sp FAILED to create!', 16, 1);
Go
103 changes: 103 additions & 0 deletions dev/bcp_script_generator.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**********************************************************************************************************
NAME: bcp_script_generator.sql
SYNOPSIS: Generates bcp scripts using SQL Server metadata
DEPENDENCIES: The following dependencies are required to execute this script:
- SQL Server 2005 or newer
AUTHOR: Michelle Ufford, http://sqlfool.com
CREATED: 2012-05-17
VERSION: 1.0
LICENSE: Apache License v2
----------------------------------------------------------------------------
DISCLAIMER:
This code and information are provided "AS IS" without warranty of any kind,
either expressed or implied, including but not limited to the implied
warranties or merchantability and/or fitness for a particular purpose.
----------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
-- DATE VERSION AUTHOR DESCRIPTION --
---------------------------------------------------------------------------------------------------------
20150619 1.0 Michelle Ufford Open Sourced on GitHub
**********************************************************************************************************/

-- User-defined variables --

DECLARE @tableToBCP NVARCHAR(128) = 'sandbox.dbo.example_table'
, @Top VARCHAR(10) = NULL -- Leave NULL for all rows
, @Delimiter VARCHAR(4) = '|'
, @UseNULL BIT = 1
, @OverrideChar CHAR(1) = '~'
, @MaxDop CHAR(1) = '1'
, @Directory VARCHAR(256) = 'D:\dba\mufford\scripts';


-- Script-defined variables --

DECLARE @columnList TABLE (columnID INT);

DECLARE @bcpStatement NVARCHAR(MAX) = 'BCP "SELECT '
, @currentID INT
, @firstID INT;

INSERT INTO @columnList
SELECT column_id
FROM sys.columns
WHERE object_id = OBJECT_ID(@tableToBCP)
ORDER BY column_id;

IF @Top IS NOT NULL
SET @bcpStatement = @bcpStatement + 'TOP (' + @Top + ') ';

SELECT @firstID = MIN(columnID) FROM @columnList;

WHILE EXISTS(SELECT * FROM @columnList)
BEGIN

SELECT @currentID = MIN(columnID) FROM @columnList;

IF @currentID <> @firstID
SET @bcpStatement = @bcpStatement + ',';

SELECT @bcpStatement = @bcpStatement +
CASE
WHEN user_type_id IN (231, 167, 175, 239)
THEN 'CASE WHEN ' + name + ' = '''' THEN '
+ CASE
WHEN is_nullable = 1 THEN 'NULL'
ELSE '''' + REPLICATE(@OverrideChar, max_length) + ''''
END
+ ' WHEN ' + name + ' LIKE ''%' + @Delimiter + '%'''
+ ' OR ' + name + ' LIKE ''%'' + CHAR(9) + ''%''' -- tab
+ ' OR ' + name + ' LIKE ''%'' + CHAR(10) + ''%''' -- line feed
+ ' OR ' + name + ' LIKE ''%'' + CHAR(13) + ''%''' -- carriage return
+ ' THEN '
+ CASE
WHEN is_nullable = 1 THEN 'NULL'
ELSE '''' + REPLICATE(@OverrideChar, max_length) + ''''
END
+ ' ELSE ' + name + ' END'
ELSE name
END
FROM sys.columns
WHERE object_id = OBJECT_ID(@tableToBCP)
AND column_id = @currentID;

DELETE FROM @columnList WHERE columnID = @currentID;


END;

SET @bcpStatement = @bcpStatement + ' FROM ' + @tableToBCP
+ ' WITH (NOLOCK) OPTION (MAXDOP 1);" queryOut '
+ @Directory + REPLACE(@tableToBCP, '.', '_') + '.dat -S' + @@SERVERNAME
+ ' -T -t"' + @Delimiter + '" -c -C;'

SELECT @bcpStatement;
145 changes: 145 additions & 0 deletions dev/insert_statement_generator.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/**********************************************************************************************************
NAME: insert_statement_generator.sql
SYNOPSIS: Generates insert statements for Teradata using SQL Server metadata.
This is useful for easily migrating small tables (i.e. < 1000 rows)
from SQL Server to Teradata. DO NOT use on large tables.
DEPENDENCIES: The following dependencies are required to execute this script:
- SQL Server 2005 or newer
AUTHOR: Michelle Ufford, http://sqlfool.com
CREATED: 2012-07-26
VERSION: 1.0
LICENSE: Apache License v2
----------------------------------------------------------------------------
DISCLAIMER:
This code and information are provided "AS IS" without warranty of any kind,
either expressed or implied, including but not limited to the implied
warranties or merchantability and/or fitness for a particular purpose.
----------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
-- DATE VERSION AUTHOR DESCRIPTION --
---------------------------------------------------------------------------------------------------------
20150619 1.0 Michelle Ufford Open Sourced on GitHub
**********************************************************************************************************/

-- User-defined variables --
DECLARE
@tableName NVARCHAR(128) = 'dbo.example_table'
, @Top VARCHAR(10) = 1000 -- Leave NULL for all rows
, @Execute BIT = 1
, @GenerateSchema BIT = 1
, @GenerateTruncate BIT = 1
, @TeradataDatabase VARCHAR(30) = 'mufford'
, @TeradataTable VARCHAR(30) = NULL -- Will generate if you leave NULL

-- Script-defined variables --

DECLARE @columnList TABLE (columnID INT);
DECLARE @TeradataTableName VARCHAR(60);

IF @TeradataTable IS NULL
SET @TeradataTableName = @TeradataDatabase + '.tmp_' + SUBSTRING(@tableName,PATINDEX('%.%',@tableName)+1,26);
ELSE
SET @TeradataTableName = @TeradataDatabase + '.' + @TeradataTable;

DECLARE @insertStatement NVARCHAR(MAX) = '' --= 'SELECT '
, @columnStatement NVARCHAR(MAX) = 'INSERT INTO ' + @TeradataTableName + ' ('
, @schemaStatement NVARCHAR(MAX) = 'CREATE TABLE ' + @TeradataTableName + '('
, @currentID INT
, @firstID INT;

INSERT INTO @columnList
SELECT column_id
FROM sys.columns
WHERE object_id = OBJECT_ID(@tableName)
ORDER BY column_id;

SELECT @firstID = MIN(columnID) FROM @columnList;

WHILE EXISTS(SELECT * FROM @columnList)
BEGIN

SELECT @currentID = MIN(columnID) FROM @columnList;

IF @currentID <> @firstID
BEGIN
SELECT
@columnStatement = @columnStatement + ','
, @schemaStatement = @schemaStatement + ','
, @insertStatement = @insertStatement + '+'',''+';
END

SELECT @columnStatement = @columnStatement + '"' + SUBSTRING(name, 1, 30) + '"'
FROM sys.columns
WHERE object_id = OBJECT_ID(@tableName)
AND column_id = @currentID;

SELECT @schemaStatement = @schemaStatement + '"' + SUBSTRING(c.name, 1, 30) + '" '
+ CASE
WHEN t.name = 'BIT' THEN 'BYTEINT'
WHEN t.name = 'TINYINT' THEN 'SMALLINT'
WHEN t.name = 'UNIQUEIDENTIFIER' THEN 'CHAR(38)'
WHEN t.name = 'DATETIME' THEN 'TIMESTAMP(3)'
WHEN t.name = 'MONEY' THEN 'DECIMAL(18,4)'
WHEN t.name = 'XML' THEN 'CLOB'
WHEN t.name IN ('SMALLDATETIME', 'DATETIME2') THEN 'TIMESTAMP(0)'
WHEN t.name IN ('NVARCHAR','NCHAR')
THEN SUBSTRING(t.name, 2, 10) + '(' + CAST(c.max_length / 2 AS VARCHAR(4)) + ') CHARACTER SET UNICODE NOT CASESPECIFIC'
WHEN t.name IN ('VARCHAR','CHAR')
THEN t.name + '(' + CAST(c.max_length AS VARCHAR(4)) + ')'
ELSE t.name
END
+ CASE
WHEN c.is_nullable = 1 THEN ' NULL'
ELSE ' NOT NULL'
END
FROM sys.columns AS c
JOIN sys.types AS t
ON c.system_type_id = t.system_type_id
WHERE c.object_id = OBJECT_ID(@tableName)
AND c.column_id = @currentID;

SELECT DISTINCT @insertStatement = @insertStatement
+ 'CASE WHEN ' + QUOTENAME(c.name) + ' IS NULL THEN ''NULL'' ELSE ' +
+ CASE
WHEN t.name IN ('tinyint','smallint','int','real','float','bit','decimal','numeric','smallmoney','bigint') /* number-based columns */
THEN 'CAST(' + QUOTENAME(c.name) + ' AS VARCHAR(' + CAST(c.precision AS VARCHAR(10)) + '))'
WHEN t.name IN ('datetime', 'date', 'datetime2', 'smalldatetime') /* date-based columns */
THEN '''''''''+' + 'CONVERT(VARCHAR(23),' + QUOTENAME(c.name) + ',126)' + '+'''''''''
WHEN t.name IN ('uniqueidentifier') /* guid columns */
THEN '''''''''+' + 'CAST(' + QUOTENAME(c.name) + ' AS CHAR(36))' + '+'''''''''
WHEN t.name IN ('XML') /* xml columns */
THEN '''''''''+' + 'CAST(' + QUOTENAME(c.name) + ' AS VARCHAR(MAX))' + '+'''''''''
ELSE '''''''''+REPLACE(' + QUOTENAME(c.name) + ','''''''','''''''''''')+''''''''' --'''+''''''+''' /* character-based columns */
END
+ ' END '
FROM sys.columns AS c
JOIN sys.types AS t
ON c.system_type_id = t.system_type_id
WHERE c.object_id = OBJECT_ID(@tableName)
AND c.column_id = @currentID;

DELETE FROM @columnList WHERE columnID = @currentID;

END;

SET @insertStatement = 'SELECT ' + CASE WHEN @Top IS NOT NULL THEN 'TOP (' + @Top + ') ' ELSE '' END + '''' + @columnStatement + ') VALUES (''+' + @insertStatement + '+'');'' FROM ' + @tableName + ' WITH (NOLOCK);';

IF @GenerateSchema = 1
SELECT @schemaStatement + ');' AS 'Execute this statement in Teradata to create the table:'

IF @GenerateTruncate = 1
SELECT 'DELETE FROM ' + @TeradataTableName + ';' AS 'Execute this statement in Teradata to truncate the table:'

IF @Execute = 1
EXECUTE sp_executeSQL @insertStatement;
ELSE
SELECT @insertStatement AS 'Execute this statement in SQL Server to generate commands:';
Loading

0 comments on commit 6712f8b

Please sign in to comment.