Friday 29 January 2010

SP_Tablespace_Growth

Grant select on v_$datafile,dba_free_space,v_$tablespace to user;

CREATE OR REPLACE

PROCEDURE

TABLESPACE_GROWTH

AS

stmt VARCHAR2(2000);

stmt2 VARCHAR2(2000);

stmt3 VARCHAR2(2000);

v_date varchar2(30);

h_exist NUMBER;


CURSOR get_size IS

--select * from test1;

SELECT UPPER(vts.NAME) AS TABLESPACE ,

UPPER(df.NAME) AS DATAFILE,

df.bytes / 1024 / 1024 AS allocated_mb,

ROUND (((df.bytes/ 1024 / 1024) - NVL (SUM (dfs.bytes) / 1024 / 1024, 0)),3) as used_mb

FROM v$datafile df,

dba_free_space dfs,

v$tablespace vts

WHERE df.file# = dfs.file_id(+)

AND vts.ts# = df.ts#

GROUP BY vts.NAME, df.NAME, df.bytes / 1024 / 1024

ORDER BY vts.NAME;


c1 get_size%rowtype;

BEGIN

--get desire v_date variable from sysdate

select to_char(sysdate,'DD-MON-YY@HH24:MI:SS') into v_date from dual;

dbms_output.put_line(v_date);

-- create column in table tbs_Growth

stmt := ' ALTER TABLE tbs_growth add "' || v_date || '" float ';

dbms_output.put_line('Executing ''' || stmt || '''');

EXECUTE IMMEDIATE stmt;

OPEN get_size;

loop

fetch get_size INTO c1 ;

exit WHEN get_size%NOTFOUND;


-- check datafile with tbs_growth datafile

select nvl((select 1 from tbs_growth where lower(datafile) = lower(c1.datafile) ) ,0) into h_exist from dual;

IF h_exist = 1

THEN

-- dbms_output.put_line ( v_date ||',' ||c1.datafile||','|| h_exist);

-- dbms_output.put_line( v_date ||'" ,"'||c1.tablespace ||'" ,"'||c1.datafile||'",'|| c1.allocated_mb || ',' ||c1.used_mb );

---update allocated_column and used_mb column with current data

stmt3 := ' Update TBS_GROWTH set allocated_mb=' || c1.allocated_mb || ',"' || v_date || '"='||c1.used_mb ||' where lower(DATAFILE)=lower('''||c1.datafile||''') ' ;

dbms_output.put_line('Executing ''' || stmt3 || '''');

EXECUTE IMMEDIATE stmt3;

ELSE

-- if not exist

--- Insert all data in new row and (used_mb with today's column)

stmt2 := ' Insert INTO TBS_GROWTH (tablespace,datafile,allocated_mb ,"' || v_date || '" ) values ('''||c1.tablespace ||''' ,'''||c1.datafile||''','|| c1.allocated_mb || ',' ||c1.used_mb||' ) ' ;

dbms_output.put_line('Executing ''' || stmt2 || '''');

EXECUTE IMMEDIATE stmt2;

end if;

END loop;

close get_size;

IF get_size%ISOPEN then

CLOSE get_size;

END IF;

END TABLESPACE_GROWTH;




Thursday 28 January 2010

Sp_DBGrowth

USE [DB_Growth]

GO

/****** Object: StoredProcedure [dbo].[Weekly_Growth] Script Date: 01/28/2010 16:48:24 ******/

SET
ANSI_NULLS
ON

GO

SET
QUOTED_IDENTIFIER
ON

GO

-- =============================================

-- Author:        <Author,,Name>

-- Create date: <Create Date,,>

-- Description:    <Description,,>

-- =============================================

CREATE
PROCEDURE [dbo].[Weekly_Growth]

    

    

AS

BEGIN

--use master

declare @PageSize varchar(10)

select @PageSize=v.low/1024.0

from
master..spt_values v

where v.number=1 and v.type='E'


 

select name as DatabaseName,
convert(float,null)
as Size

into #tem

From
sys.sysdatabases
where
dbid>4


 

declare @SQL varchar (8000)

set @SQL=''


 

while
exists
(select
*
from #tem where size is
null)

begin

select @SQL='update #tem set size=(select round(sum(size)*'+@PageSize+'/1024,2) From '+quotename(databasename)+'.dbo.sysfiles) where databasename='''+databasename+''''

from #tem

where size is
null

exec (@SQL)

end


 

--select * from #tem order by DatabaseName


 

--Add new column

DECLARE @today_text NVARCHAR(25)


 

--SELECT @today_text = CONVERT(NVARCHAR(20), GETDATE(), 112)

select @today_text =
CONVERT(NVARCHAR(20),
GETDATE(), 6)
+
'@'
+
CONVERT(NVARCHAR(20),DATEPART(HH,
GETDATE()))
+
':'
+
CONVERT(NVARCHAR(20),DATEPART(MINUTE,
GETDATE()))
+
':'
+
CONVERT(NVARCHAR(20),DATEPART(SECOND,
GETDATE()))
+
'(MB)'


 


 

exec ('ALTER TABLE DB_Growth.dbo.DBGrowth ADD "'
+ @today_text +
'" FLOAT'
)


 


 


 

DECLARE DB_Cursor CURSOR

FOR
SELECT databasename, size


FROM #tem


 


 

DECLARE @DB_name NVARCHAR(150)

DECLARE @size FLOAT

DECLARE @today_date NVARCHAR(25)


 

OPEN DB_Cursor


 

FETCH
NEXT
FROM DB_Cursor INTO @DB_name, @size


 


 

--SELECT @today_date = CONVERT(NVARCHAR(20), GETDATE(), 112)

select @today_date =
CONVERT(NVARCHAR(20),
GETDATE(), 6)
+
'@'
+
CONVERT(NVARCHAR(20),DATEPART(HH,
GETDATE()))
+
':'
+
CONVERT(NVARCHAR(20),DATEPART(MINUTE,
GETDATE()))
+
':'
+
CONVERT(NVARCHAR(20),DATEPART(SECOND,
GETDATE()))
+
'(MB)'


 


 

WHILE
@@FETCH_STATUS
= 0


BEGIN


 


-- USE DB_Growth_Test


IF (EXISTS(select [DB_NAME] from DB_Growth.dbo.DBGrowth where
LOWER([DB_NAME])
=
LOWER(@DB_name)
))


BEGIN


--Update new column and put the size for that db


exec ('UPDATE DB_Growth.dbo.DBGrowth SET ['
+ @today_date +
'] = CONVERT(NVARCHAR(10), '
+ @size +
') WHERE LOWER([DB_NAME]) = LOWER('''
+ @DB_name +
''')')


 


 


END


ELSE


 


BEGIN


----Create another row and put the size in the newly added column


exec ('INSERT DB_Growth.dbo.DBGrowth(DB_NAME, "'
+ @today_date +
'") VALUES( '''
+ @DB_name +
''', CONVERT(NVARCHAR(10), '
+ @size +
'))')


 


END


 


FETCH
NEXT
FROM DB_Cursor into @DB_name, @size


END

CLOSE DB_Cursor;

DEALLOCATE DB_Cursor;


 


 

drop
table #tem


 


 


 

END


 


 

Monday 11 January 2010

Install Required Linux Packages for Oracle RAC

Install Required Linux Packages for Oracle RAC

Install the following required Linux packages on both Oracle RAC nodes in the cluster.

After installing Enterprise Linux, the next step is to verify and install all packages (RPMs) required by both Oracle Clusterware and Oracle RAC. The Oracle Universal Installer (OUI) performs checks on your machine during installation to verify that it meets the appropriate operating system package requirements. To ensure that these checks complete successfully, verify the software requirements documented in this section before starting the Oracle installs.

Although many of the required packages for Oracle were installed during the Enterprise Linux installation, several will be missing either because they were considered optional within the package group or simply didn't exist in any package group!

The packages listed in this section (or later versions) are required for Oracle grid infrastructure 11g Release 2 and Oracle RAC 11g Release 2 running on the Enterprise Linux 5 platform.


 

32-bit (x86) Installations

Each of the packages listed above can be found on CD #1, CD #2, and CD #3 on the Enterprise Linux 5 - (x86) CDs. While it is possible to query each individual package to determine which ones are missing and need to be installed, an easier method is to run the rpm -Uvh PackageName command from the five CDs as follows. For packages that already exist and are up to date, the RPM command will simply ignore the install and print a warning message to the console that the package is already installed.

# From Enterprise Linux 5.4 (x86)- [CD #1]

mkdir -p /media/cdrom

mount -r /dev/cdrom /media/cdrom

cd /media/cdrom/Server

rpm -Uvh binutils-2.*

rpm -Uvh elfutils-libelf-0.*

rpm -Uvh glibc-2.*

rpm -Uvh glibc-common-2.*

rpm -Uvh kernel-headers-2.*

rpm -Uvh ksh-2*

rpm -Uvh libaio-0.*

rpm -Uvh libgcc-4.*

rpm -Uvh libstdc++-4.*

rpm -Uvh make-3.*

cd /

eject


 


 

# From Enterprise Linux 5.4 (x86) - [CD #2]

mount -r /dev/cdrom /media/cdrom

cd /media/cdrom/Server

rpm -Uvh elfutils-libelf-devel-*

rpm -Uvh gcc-4.*

rpm -Uvh gcc-c++-4.*

rpm -Uvh glibc-devel-2.*

rpm -Uvh glibc-headers-2.*

rpm -Uvh libgomp-4.*

rpm -Uvh libstdc++-devel-4.*

rpm -Uvh unixODBC-2.*

cd /

eject


 


 

# From Enterprise Linux 5.4 (x86) - [CD #3]

mount -r /dev/cdrom /media/cdrom

cd /media/cdrom/Server

rpm -Uvh compat-libstdc++-33*

rpm -Uvh libaio-devel-0.*

rpm -Uvh sysstat-7.*

rpm -Uvh unixODBC-devel-2.*

cd /

eject


 

64-bit (x86_64) Installations

Each of the packages listed above can be found on CD #1, CD #2, CD #3, and CD #4 on the Enterprise Linux 5 - (x86_64) CDs. While it is possible to query each individual package to determine which ones are missing and need to be installed, an easier method is to run the rpm -Uvh PackageName command from the six CDs as follows. For packages that already exist and are up to date, the RPM command will simply ignore the install and print a warning message to the console that the package is already installed.

# From Enterprise Linux 5.4 (x86_64)- [CD #1]

mkdir -p /media/cdrom

mount -r /dev/cdrom /media/cdrom

cd /media/cdrom/Server

rpm -Uvh binutils-2.*

rpm -Uvh elfutils-libelf-0.*

rpm -Uvh glibc-2.*

rpm -Uvh glibc-common-2.*

rpm -Uvh ksh-2*

rpm -Uvh libaio-0.*

rpm -Uvh libgcc-4.*

rpm -Uvh libstdc++-4.*

rpm -Uvh make-3.*

cd /

eject


 


 

# From Enterprise Linux 5.4 (x86_64) - [CD #2]

mount -r /dev/cdrom /media/cdrom

cd /media/cdrom/Server

rpm -Uvh elfutils-libelf-devel-*

rpm -Uvh gcc-4.*

rpm -Uvh gcc-c++-4.*

rpm -Uvh glibc-devel-2.*

rpm -Uvh glibc-headers-2.*

rpm -Uvh libstdc++-devel-4.*

rpm -Uvh unixODBC-2.*

cd /

eject


 


 

# From Enterprise Linux 5.4 (x86_64) - [CD #3]

mount -r /dev/cdrom /media/cdrom

cd /media/cdrom/Server

rpm -Uvh compat-libstdc++-33*

rpm -Uvh libaio-devel-0.*

rpm -Uvh unixODBC-devel-2.*

cd /

eject


 


 

# From Enterprise Linux 5.4 (x86_64) - [CD #4]

mount -r /dev/cdrom /media/cdrom

cd /media/cdrom/Server

rpm -Uvh sysstat-7.*

cd /

eject

 
 


 

Monday 4 January 2010

SQL Server System Objects in User Defined Databases

Problem
With the recent tips on data modeling (
SQL Server 2005 Exposed = Data Modeling Tools and 
SQL Server Data Modeling Tools), a natural question that arose is: where are all of the underlying objects stored in SQL Server?  As such, in this tip we will outline where the user defined objects in user defined databases for both SQL Server 2000 and 2005.

Solution
Below outlines the queries for the objects in the user databases in both SQL Server 2000 and 2005:

ID

Object Type

SQL Server 2000

SQL Server 2005

1

Data Models

Table = dtproperties

SELECT * 
FROM dbo.dtproperties
GO

Table = dbo.sysdiagrams

SELECT * 
FROM dbo.sysdiagrams;
GO

2

Tables

Table = sysobjects

SELECT * 
FROM dbo.sysobjects
WHERE xtype = 'u'
ORDER BY Name
GO

Table = sys.tables

SELECT * 
FROM sys.tables 
ORDER BY Name;
GO

3

Columns

Table = syscolumns

SELECT o.name, c.name
FROM dbo.syscolumns c
INNER JOIN dbo.sysobjects o
ON c.id = o.id
WHERE o.name = 'MyTableName'
ORDER BY c.colorder
GO

Table = sys.all_columns

SELECT OBJECT_NAME([Object_ID]) AS 'TableName', [Name] AS 'ColumnName', Column_ID 
FROM sys.all_columns 
ORDER BY TableName, Column_ID; 
GO

4

Primary Keys

Table = sysobjects

SELECT p.name, OBJECT_NAME(parent_obj) AS 'Table Name'
FROM dbo.sysobjects p
WHERE p.xtype = 'PK'
ORDER BY p.Name
GO

Table = sys.objects

SELECT OBJECT_NAME(o.parent_object_id) AS 'ParentObject', s.name AS 'Schema', o.Name AS 'PrimaryKey' 
FROM sys.objects o 
INNER JOIN sys.schemas s 
ON o.schema_id = s.schema_id 
WHERE o.Type = 'PK' 
ORDER BY o.Name;
GO

5

Foreign Keys

Table = sysforeignkeys

SELECT OBJECT_NAME(f.constid) AS 'ForeignKey', OBJECT_NAME(f.fkeyid) AS 'FKTable', c1.[name] AS 'FKColumnName', OBJECT_NAME(f.rkeyid) AS 'PKTable', c2.[name] AS 'PKColumnName' 
FROM sysforeignkeys f 
INNER JOIN syscolumns c1 
ON f.fkeyid = c1.[id] 
AND f.fkey = c1.colid 
INNER JOIN syscolumns c2 
ON f.rkeyid = c2.[id] 
AND f.rkey = c2.colid 
ORDER BY OBJECT_NAME(f.rkeyid) 
GO

Table = sys.foreign_key_columns

SELECT OBJECT_NAME(f.constraint_object_id) AS 'ForeignKey', OBJECT_NAME(f.parent_object_id) AS 'FKTable', c1.[name] AS 'FKColumnName', OBJECT_NAME(f.referenced_object_id) AS 'PKTable', c2.[name] AS 'PKColumnName' 
FROM sys.foreign_key_columns f 
INNER JOIN sys.all_columns c1 
ON f.parent_object_id = c1.[object_id] 
AND f.parent_column_id = c1.column_id 
INNER JOIN sys.all_columns c2 
ON f.referenced_object_id = c2.[object_id] 
AND f.referenced_column_id = c2.column_id ORDER BY OBJECT_NAME(f.referenced_object_id); 
GO

6

Constraints

Table = sysconstraints

SELECT o.[name] AS 'DefaultName', OBJECT_NAME(c.[id]) AS 'TableName', col.[name] AS 'ColumnName'
FROM dbo.sysconstraints c
INNER JOIN dbo.sysobjects o 
ON c.constid = o.[id] 
INNER JOIN dbo.syscolumns col 
ON col.[id] = c.colid 
ORDER BY o.[name]
GO

Table = sys.objects

SELECT OBJECT_NAME(o.parent_object_id) AS 'ParentObject', s.name AS 'Schema', o.Name AS 'PrimaryKey'
FROM sys.objects o 
INNER JOIN sys.schemas s 
ON o.schema_id = s.schema_id 
WHERE o.Type IN ('C', 'D', 'UQ') 
ORDER BY o.Name; 
GO

7

FileGroups\Partitions

Table = sysfilegroups

SELECT * 
FROM sysfilegroups
GO

Table = sys.data_spaces

SELECT * 
FROM sys.data_spaces;
GO

8

Stored Procedures

Table = sysobjects

SELECT o.[name], o.[id], o.xtype, c.[text] 
FROM dbo.sysobjects o 
INNER JOIN dbo.syscomments c 
ON o.[id] = c.[id] 
WHERE o.xtype = 'p' 
ORDER BY o.[Name]
GO

Table =  sys.objects

SELECT o.[Name], o.[object_id], o.[type], m.definition 
FROM sys.objects o 
INNER JOIN sys.sql_modules m 
ON o.object_id = m.object_id 
WHERE o.[type] = 'p' 
ORDER BY o.[Name];
GO

9

Functions

Table = sysobjects

SELECT o.[name], o.[id], o.xtype, c.[text]
FROM dbo.sysobjects o
INNER JOIN dbo.syscomments c
ON o.[id] = c.[id] 
WHERE o.xtype IN ('fn', 'if', 'tf')
ORDER BY o.[Name]
GO

Table = sys.objects

SELECT o.[Name], o.[object_id], o.[type], m.definition 
FROM sys.objects o 
INNER JOIN sys.sql_modules m 
ON o.object_id = m.object_id 
WHERE o.[type] IN ('fn', 'fs', 'ft', 'if', 'tf')ORDER BY o.[Name]; 
GO

10

Views

Table = sysobjects

SELECT o.[name], o.[id], o.xtype, c.[text]
FROM dbo.sysobjects o
INNER JOIN dbo.syscomments c
ON o.[id] = c.[id] 
WHERE o.xtype = 'v'
ORDER BY o.[Name]
GO

Table = sys.objects

SELECT o.[Name], o.[object_id], o.[type], m.definition 
FROM sys.objects o 
INNER JOIN sys.sql_modules m 
ON o.object_id = m.object_id 
WHERE o.[type] = 'V' 
ORDER BY o.[Name]; 
GO

SQL Server Instance Objects

Problem
When it comes time to review server related information from your SQL Server be sure to know how and where to access the information.  With SQL Server 2005 some of the resources have changed and new resources have popped up.  This tip outlines core server related information from SQL Server 2000 to 2005.

Solution
Mapping server information between SQL Server 2000 and 2005 is critical to ensure scripts are working properly when upgrading to SQL Server 2005. Below outlines the common server related objects.

ID

Information

SQL Server 2000

SQL Server 2005

1

System table\view with all server related information for the local, remote and linked servers

SELECT * 
FROM master.dbo.sysservers 
GO

SELECT * 
FROM master.sys.servers; 
GO
 

2

General SQL Server metrics for 1 server

sp_helpserver 'ProdSQL1'
GO
 

sp_helpserver 'ProdSQL1';
 

3

Listing of the server wide configurations

sp_configure
GO
 

EXEC sp_configure;

4

Configures server options for remote or linked servers

sp_serveroption 'ProdSQL1', 'collation compatible', TRUE
GO
 

sp_serveroption 'ProdSQL1', 'collation compatible', TRUE;

5

System function returning server information such as the collation, edition, instance name, security configuration, etc. for the SQL Server

SELECT SERVERPROPERTY('Edition')
GO
 

SELECT SERVERPROPERTY('Edition');

6

Approximately 30 connection related parameters primarily for ODBC

EXEC sp_server_info 
GO
 

EXEC sp_server_info;

7

Function to return the SQL Server instance name

SELECT @@SERVERNAME
GO
 

SELECT @@SERVERNAME;

8

System stored procedure to return the sort order and character set for the SQL Server

sp_helpsort
GO
 

EXEC sp_helpsort;

9

Listing of the active processes

sp_who2 active
GO
 

EXEC sp_who2 active;
 

10

System table\view with real time system processes in SQL Server whether the process (spid) is active, sleeping, etc.

SELECT * 
FROM master.dbo.sysprocesses
GO
 

SELECT * FROM sys.dm_exec_sessions;
GO

SELECT * FROM sys.dm_exec_connections;
GO
 

SQL SERVER 2000 Vs 2005

Problem
Accessing SQL Server system information is necessary for administrative scripts and very important when troubleshooting particular issues.  Unfortunately, in the transition from SQL Server 2000 to 2005, some of the objects that we have grown to rely on are no longer the recommended information source.  In this tip we will outline core sets of data that need to be retrieved for databases and map the objects from SQL Server 2000 to 2005.

Solution
The database information mapping between SQL Server 2000 and 2005 is critical to ensure scripts are working properly when upgrading to SQL Server 2005.  Below outlines the common database related objects.

ID

Information

SQL Server 2000

SQL Server 2005

1

Database system table\view - Source for all databases on the SQL Server to include the name, owner, creation date, etc.
 

SELECT * 
FROM master.dbo.sysdatabases
GO

SELECT * 
FROM sys.databases;
GO
 

2

Database files system table\view - Source for the currently connected database's file names, size, location, type (database or log), etc.
 

SELECT * 
FROM dbo.sysfiles
GO

SELECT * 
FROM sys.database_files; 
GO

3

Database files system table\view - Source for all database's file related information
 

SELECT * 
FROM master.dbo.sysaltfiles
GO
 

SELECT * 
FROM sys.master_files;
GO

4

IO statistics on database files - Returns the usage statistics on a per file basis

-- Single database file
SELECT * 
FROM :: fn_virtualfilestats(1, 1)
GO

-- All database files
SELECT * 
FROM sys.dm_io_virtual_file_stats(NULL, NULL);
GO
 

5

Database meta data - Returns the pertinent database name, size and remarks
 

EXEC master.dbo.sp_databases
GO

EXEC master.dbo.sp_databases;
GO

6

Database meta data - Fairly complete set of pertinent database information that can return data for all databases or 1 database
 

-- All databases
EXEC master.dbo.sp_helpdb
GO

-- Single database 
EXEC master.dbo.sp_helpdb 'Northwind'
GO
 

  

-- All databases 
EXEC master.dbo.sp_helpdb;
GO

-- Single database 
EXEC master.dbo.sp_helpdb 'AdventureWorks';
GO

  

7

Change database ownership - System stored procedure to change the database owner
 

EXEC sp_changedbowner sa
GO
 

EXEC sp_changedbowner sa; 
GO

8

Database ID to name translation - System function that will provide the database name when passed the database ID from the database system table
 

-- Returns the master database
SELECT DB_NAME(1)
GO

-- Returns the master database
SELECT DB_NAME(1);
GO

9

Database name to ID translation - System function that will provide the database ID when passed the database name from the database system table
 

-- Returns 1 
SELECT DB_ID('master')
GO

-- Returns 1 
SELECT DB_ID('master');
GO

10

Database status - System function that will return the value for 1 of ~25 database specific values

SELECT DATABASEPROPERTYEX('master', 'Status')
GO
 

SELECT DATABASEPROPERTYEX('master', 'Status');
GO