Commit 9ab76abb authored by dwentzel's avatar dwentzel
Browse files

MD3 upload

parent a8d63a95
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
/*
	--this is a special file in MD3
	--this will always run from the context of master and is the FIRST FILE run.  
	--this will always attempt to create the database you specify, if it does not already exist.  
	--sometimes this is a desired functionality, and sometimes it isn't.  If you don't like this behavior then simply DELETE
		the file.  
	--if you decide to use this file then you will need to modify it to fit the unique requirements of your project (drive layouts, 
		recovery mode, filegroups, etc)
	--You can always build the database yourself, in which case this script does nothing.  


DO NOT RENAME THIS FOLDER OR FILE
IF YOU DO YOU NEED TO CHANGE RunMD3.ps1

Note that this will also run as the first (or one of the first) files when MD3 runs, but in the context of the given db you call MD3 for.
So, this script runs twice.  You can therefore embed master only logic and db-specific logic here, just check db_name() first

*/
SET NOCOUNT ON 

--IF NOT EXISTS (SELECT 1 FROM master..sysdatabases WHERE NAME = 'NETNEW')
--BEGIN
--	PRINT 'Database was not found.  Creating...'	
--	DECLARE @SQL varchar(4000)

--	SELECT @SQL = 'CREATE DATABASE NETNEW;'
--	PRINT @SQL 
--	EXEC(@SQL)
--END
--ELSE
--BEGIN
--	PRINT 'Database exists.'
--END;
GO
+9 −0
Original line number Diff line number Diff line
/*

Place things in this folder/file that should occur FIRST before an install/upgrade is even attempted.  
Examples:  
	--disabling any triggers
	--taking backups
	--pausing distribution agents

*/
+3 −0
Original line number Diff line number Diff line
IF (SCHEMA_ID('MD3') IS NULL)
	EXEC('CREATE SCHEMA MD3;');
GO
 No newline at end of file
+32 −0
Original line number Diff line number Diff line
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'MD3.f_GetColumnDataType'))
   DROP FUNCTION MD3.f_GetColumnDataType
GO

CREATE FUNCTION MD3.f_GetColumnDataType (
    @SchemaName SYSNAME
   ,@TableName SYSNAME
   ,@ColumnName SYSNAME
   )
    RETURNS SYSNAME
    WITH RETURNS NULL ON NULL INPUT
AS

BEGIN

    DECLARE @ColumnDataType SYSNAME

    SELECT @ColumnDataType = TYPE_NAME(c.system_type_id)
    FROM sys.objects o
    JOIN sys.columns c 
      ON o.object_id = c.object_id
    JOIN sys.types t 
      ON c.system_type_id = t.system_type_id
    WHERE o.name = @TableName 
      AND c.name = @ColumnName 
      AND o.schema_id = schema_id(@SchemaName)
      AND o.type = 'U'

    RETURN @ColumnDataType

END
GO
+82 −0
Original line number Diff line number Diff line
if exists (select * from dbo.sysobjects where id = object_id(N'MD3.f_GetConstraints'))
drop function MD3.f_GetConstraints
GO

CREATE  function MD3.f_GetConstraints( 
    @SchemaName SYSNAME
   ,@TblName SYSNAME 

)
RETURNS @Table  TABLE   
(    
    TableName	varchar(255) NOT NULL,     
    SchemaName varchar(255) NOT NULL,
    ConstraintName varchar(255) NOT NULL,
    ConstraintExpression varchar(max) NOT NULL,
    ColumnName varchar(255) NULL,
    is_system_named BIT NOT NULL,  
    is_not_trusted	BIT NOT NULL,
    ConstraintType varchar(255) NOT NULL
    
)    
AS  
BEGIN

    DECLARE @FullTblName SYSNAME
	SELECT @FullTblName = @SchemaName + '.' + @TblName

		INSERT INTO @Table
		--column level constraints
        SELECT 
		    OBJECT_NAME(parent_object_id) AS TableName,
		    OBJECT_SCHEMA_NAME(parent_object_id) AS SchemaName,
		    OBJECT_NAME(cc.object_id) AS ConstraintName,
		    cc.definition AS ConstraintExpression,
		    cols.name AS ColumnName,
		    cc.is_system_named ,
		    cc.is_not_trusted,
		    'CHECK' AS ConstraintType
	    FROM sys.check_constraints(NOLOCK) cc
		JOIN sys.columns(NOLOCK) cols 
		    ON cc.parent_column_id = cols.column_id
		    AND cc.parent_object_id = cols.object_id
	     WHERE cc.parent_object_id = object_id(@FullTblName)	
	
	    UNION ALL 
		--table level constraints
	     SELECT 
		    OBJECT_NAME(parent_object_id) AS TableName,
		    OBJECT_SCHEMA_NAME(parent_object_id) AS SchemaName,
		    OBJECT_NAME(cc.object_id) AS ConstraintName,
		    cc.definition AS CheckConstraintExpression,
		    NULL AS ColumnName,
		    cc.is_system_named ,
		    cc.is_not_trusted,
		    'CHECK' AS ConstraintType
	    FROM sys.check_constraints(NOLOCK) cc
	    WHERE cc.parent_object_id = object_id(@FullTblName)	
			AND cc.parent_column_id = 0
		
	    UNION ALL 
	   --default constraints	       
	   SELECT 
		    OBJECT_NAME(parent_object_id) AS TableName,
		    OBJECT_SCHEMA_NAME(parent_object_id) AS SchemaName,
		    OBJECT_NAME(cc.object_id) AS ConstraintName,
		    cc.definition AS CheckConstraintExpression,
		    cols.name AS ColumnName,
		    cc.is_system_named ,
		    0,
		    'DEFAULT' AS ConstraintType
	    FROM sys.default_constraints(NOLOCK) cc
		JOIN sys.columns(NOLOCK) cols 
		    ON cc.parent_column_id = cols.column_id
		    AND cc.parent_object_id = cols.object_id
	     WHERE cc.parent_object_id = object_id(@FullTblName)	
    
  
   
   RETURN
  
END    
GO
Loading