Search This Blog

Tuesday, September 29, 2009

Export Data From SQL Server 2005 To Microsoft Excel Datasheet

Hi Friends,

In our day to day working we write queries get there output and then struggle to copy it in EXCEL sheet. Here by I am providing a small query which uses OPENROWSET funtion to fetch data from SQL Server 2005 table to a Microsoft Excel Sheet.

/* First -> Enable Ad Hoc Distributed Queries */

Enable Ad Hoc Distributed Queries. Run following code in SQL Server Management Studio – Query Editor.

EXEC sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'Ad Hoc Distributed Queries', 1;
GO
RECONFIGURE;
GO
/* Second -> Create an Excel Sheet */

  1. Create Excel Spreadsheet in root directory c:\Contact.xls (Make sure you name it Contact.xls).
  2. Open spreadsheet, on the first tab of Sheet1, create two columns with FirstName, LastName.
  3. Run following code in SQL Server Management Studio – Query Editor.
USE [AdventureWorks];
GO
INSERT INTO OPENROWSET ('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=c:\contact.xls;',
'SELECT * FROM [Sheet1$]')SELECT TOP 5 FirstName, LastNameFROM Person.Contact
GO
Open Blocking.xls spreadsheet you will see first five records of the Person.Contact inserted into the first two columns.
Make sure your spreadsheet is closed during this operation. If you want to change name of your Spreadsheet name or Sheet1 name to desired name you can do that but ofcourse you have to mention those names in above .
Regards
GURSETHI

Monday, September 28, 2009

Get SQL Server Inventory

Hi All,
Here by I am presending a small Procedure which will collect all necessary information about from a connected instance. We can use this procedure to get SQL Server Details in our organisation.

Currently this will work for SQL 2005 & SQL 2008 instances only. Soon will post for SQL 2000.

*********************************
* SQL Server Inventory
* By Gurpreet Sethi
* Will Run On SQL Server 2005
*********************************

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[SQLInventory] as
/*First -> Declaring all the variables*/

declare @image_path varchar(100)
declare @startup_type int
declare @startuptype varchar(100)
declare @start_username varchar(100)
declare @instance_name varchar(100)
declare @system_instance_name varchar(100)
declare @log_directory varchar(100)
declare @key varchar(1000)
declare @registry_key varchar(100)
declare @registry_key1 varchar(300)
declare @registry_key2 varchar(300)
declare @IpAddress varchar(20)
declare @domain varchar(50)
declare @cluster int

/*Second -> Setting the instance name to Default or named*/

SET @instance_name = coalesce(convert(nvarchar(100), serverproperty('InstanceName')),'MSSQLSERVER');
if @instance_name!='MSSQLSERVER'
set @instance_name='MSSQL$'+@instance_name

/*Third -> getting the system instance name as we were using the service instance name
(Service is : MSSQL$ and system is MSSQL.)*/

EXEC master.dbo.xp_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\Microsoft SQL Server\Instance Names\SQL', @instance_name, @system_instance_name output;

/*Fourth -> Setting the variables, getting system SQL Server name And setting the instance name to default or named*/

set @key=N'SYSTEM\CurrentControlSet\Services\' +@instance_name;
SET @registry_key = N'Software\Microsoft\Microsoft SQL Server\' + @system_instance_name + '\MSSQLServer\Parameters';
if @registry_key is NULL
set @instance_name=coalesce(convert(nvarchar(100), serverproperty('InstanceName')),'MSSQLSERVER');
EXEC master.dbo.xp_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\Microsoft SQL Server\Instance Names\SQL', @instance_name, @system_instance_name output;
SET @registry_key = N'Software\Microsoft\Microsoft SQL Server\' + @system_instance_name + '\MSSQLServer\Parameters';
SET @registry_key1 = N'Software\Microsoft\Microsoft SQL Server\' + @system_instance_name + '\MSSQLServer\supersocketnetlib\TCP\IP1';
SET @registry_key2 = N'SYSTEM\ControlSet001\Services\Tcpip\Parameters\';

/*Fifth -> Getting the values of SQL Binaries path , Startup type,Startup service account name Errorlog directory and IP address*/

EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@key,@value_name='ImagePath',@value=@image_path OUTPUT
EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@key,@value_name='Start',@value=@startup_type OUTPUT
EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@key,@value_name='ObjectName',@value=@start_username OUTPUT
EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@registry_key,@value_name='SQLArg1',@value=@log_directory OUTPUT
EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@registry_key1,@value_name='IpAddress',@value=@IpAddress OUTPUT
EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@registry_key2,@value_name='Domain',@value=@domain OUTPUT
set @startuptype= (select 'Start Up Mode' =
CASE
WHEN @startup_type=2 then 'AUTOMATIC'
WHEN @startup_type=3 then 'MANUAL'
WHEN @startup_type=4 then 'Disabled'
END)
declare @Out varchar(8000)
SELECT @Out = COALESCE(@Out+'' ,'') + Nodename
from sys.dm_os_cluster_nodes

/*Sixth -> Get Server Details*/

SELECT
@domain as 'Domain',
serverproperty ('ComputerNamePhysicalNetBIOS') as 'Machine Name',
CPU_COUNT as 'CPU Count',
hyperthread_ratio as 'Hyper Thread Ratio',
CPU_COUNT/hyperthread_ratio as 'Physical CPU',
physical_memory_in_bytes / 1048576 as 'Physical Memory (MB)',
virtual_memory_in_bytes / 1048576 as 'virtual_mem_MB',
max_workers_count as 'Max Worker Thread Count',
os_error_mode as 'OS Error Mode',
os_priority_class as 'OS Priority Class',
serverproperty ('Instancename') as 'Instance Name',
@Ipaddress as 'IP Address',
@image_path as 'Binaries Path',
@log_directory as 'Error Logs Location',
@start_username as 'Statrtup User',
@Startuptype as 'startup_type',
serverproperty('productlevel') as ServicePack,
serverproperty('edition') as edition,
serverproperty('productversion') as version,
serverproperty('collation') as collation,
serverproperty('Isclustered') as IS_Clustered,
@out as 'Cluster Nodes',
serverproperty('IsFullTextInstalled') as IS_FullText
From
sys.dm_os_sys_info

/*Seventh -> Get Database Details Secondly */

SELECT
serverproperty ('ComputerNamePhysicalNetBIOS') as machine,
serverproperty ('Instancename') as instance_name,
(SELECT 'file_type' =
CASE
WHEN s.groupid <> 0 THEN 'data'
WHEN s.groupid = 0 THEN 'log'
END) AS 'file_type'
, d.database_id as 'DB_ID'
, d.name AS 'db_name'
, s.name AS 'logical_file_name'
, s.filename AS 'physical_file_name'
, (s.size * 8 / 1024) AS 'file_size(MB)' -- file size in MB
,d.compatibility_level
, DATABASEPROPERTYEX (d.name,'Recovery') as Recovert_Model
, DATABASEPROPERTYEX (d.name,'Status') as Database_Status
,d.is_published as Published
,d.is_subscribed as Subscribed
,d.is_distributor as Distributor
,m.mirroring_state
FROM
sys.databases d INNER JOIN sys.sysaltfiles s
ON
d.database_id=s.dbid
INNER JOIN sys.database_mirroring m
ON
d.database_id=m.database_id
ORDER BY
d.name
/* Eigth -> Select backup Details */

select distinct
b.machine_name as 'Server Name',
b.server_name as 'Instance Name',
b.database_name,
d.database_id ,
CASE b.[type]
WHEN 'D' THEN 'Full'
WHEN 'I' THEN 'Differential'
WHEN 'L' THEN 'Transaction Log'
END as BackupType
from sys.databases d inner join msdb.dbo.backupset b
On b.database_name =d.name
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
EXEC [dbo].[SQLINVENTORY]

Regards
GURSETHI
&
ABHAYC

Tuesday, September 22, 2009

Manually Add Full Text Search Resource For A SQL Server 2005 Failover Cluster

STEPS TO MANUALLY ADD FTS AS RESOURCE FOR A SQL 2005 FAILOVER CLUSTER
1. Open the SQL Server Configuration Manager (Start -> Programs -> SQL Server 2005 -> Configuration Tools)
2. Check to see that the Full-text service for your instance of SQL Server is actually there. If not, then go ahead with the
installation. Start the Full-text service in Configuration Manager.
3. In the Cluster Administrator, right-click on 'Resources' and choose New -> Resource
4. In the New Resource dialog, choose 'Generic Service' for the type and give it a name like SQL FULLTEXT. Any name is fine.
Choose the desired group for the resource.
5. In the next screen choose the nodes that the Full-Text resource can run on. These should be the same as your SQL Server
resource.
6. In the next screen don't do anything. Full-text search doesn't need any dependencies.
7. In the Generic Service Parameters screen type msftesql$ in the 'Service Name' box, where
is the Virtual Server name for the SQL Server. You can verify this name by opening Services.msc (Start -> Run -> type
'Services.msc' -> OK) and right-clicking on the SQL ServerFulltext Search service and choosing properities. Us the Service
Name in the properties dialog as the Service Name in the New Resource dialog.
8. In the 'Startup Parameters' box, enter the parameters shown in the service property dialog
(Start -> Run -> type 'Services.msc' -> OK -> right-click on SQL ServerFulltext Search service -> choose 'Properties) at the end
of the path in the 'Path to executable' box. For example: -s:MSSQL.2 -f:INST2
9. In the next screen don't do anything. Leave the 'Registry Replication' information blank.
10. Click finish. The Full-text resource should be visible in your SQL Server group in the Cluster Administrator.
11. Test by bringing the Full-text resource online and failing it over to the other node.

I Hope this will help you.
Regards
GURSETHI

SQL 2005 Patch Fails with Error 1635 "Unable to Install Windows Installer MSP"

SQL Server 2005 Patch Fails to install with an error "Unable to install Windows Installer MSP file"

We have seen several issues where installation of Service Pack/Hotfix/GDR/CU fails to install on one or more components of SQL Server 2005. In this blog , I have tried to include few troubleshooting tips and tricks for these kinds of installation failures.
Whenever a hotfix installation fails, we start troubleshooting with the log file usually located at%ProgramFiles%\Microsoft SQL Server\90\Setup Bootstrap\LOG\Hotfix\Summary.txtI am demonstrating an example to troubleshoot an issue where KB953752 failed to install .

First , go to %ProgramFiles%\Microsoft SQL Server\90\Setup Bootstrap\LOG\Hotfix\ and see Summary.txt :
----------------------------------------------------------------------------------
Product Installation Status

Product : SQL Server Database Services 2005 (MSSQLSERVER)
Product Version (Previous): 3042
Product Version (Final) :
Status : Failure
Log File : C:\Program Files\Microsoft SQL Server\90\Setup Bootstrap\LOG\Hotfix\SQL9_Hotfix_KB953752_sqlrun_sql.msp.log
Error Number : 1635
Error Description : Unable to install Windows Installer MSP file
----------------------------------------------------------------------------------

Above error is for SQL Server Database Services 2005 , so in this example I will display troubleshooting steps for Database Engine only. We can get the similar kind of error for Analysis Services (SQLRUN_AS.SQL), Reporting Services (SQLRUN_RS.SQL), Notification Services (SQLRUN_NS.SQL) and Client Tools (SQLRUN_TOOLS.SQL).

To troubleshoot this issue, I like to analyze verbose log which is usually located at
%ProgramFiles%\Microsoft SQL Server\90\Setup Bootstrap\LOG\Hotfix\SQL9_Hotfix_KBnnnnnn_sqlrun_sql.msp.log .
[Where KBnnnnnn is a placeholder for the hotfix number which is failing to install. In our example, it is SQL9_Hotfix_KB953752_sqlrun_sql.msp.log .]

Partial Contents of SQL9_Hotfix_KB953752_sqlrun_sql.msp.log :
=== Verbose logging started: 9/15/2009 0:56:20 Build type: SHIP UNICODE 3.01.4000.4042 Calling process: c:\560c299829b765ffbb1a9492e89e2e\hotfix.exe ===
MSI (c) (80:04) [00:56:20:292]: Resetting cached policy values
MSI (c) (80:04) [00:56:20:292]: Machine policy value 'Debug' is 0
MSI (c) (80:04) [00:56:20:292]: ******* RunEngine:
******* Product: {130A3BE1-85CC-4135-8EA7-5A724EE6CE2C}
******* Action:
******* CommandLine: **********
MSI (c) (80:04) [00:56:20:308]: Client-side and UI is none or basic: Running entire install on the server.
MSI (c) (80:04) [00:56:20:308]: Grabbed execution mutex.
MSI (c) (80:04) [00:56:20:323]: Cloaking enabled.
MSI (c) (80:04) [00:56:20:323]: Attempting to enable all disabled priveleges before calling Install on Server
MSI (c) (80:04) [00:56:20:354]: Incrementing counter to disable shutdown. Counter after increment: 0
MSI (s) (38:B8) [00:56:20:401]: Grabbed execution mutex.
MSI (s) (38:BC) [00:56:20:401]: Resetting cached policy values
MSI (s) (38:BC) [00:56:20:401]: Machine policy value 'Debug' is 0
MSI (s) (38:BC) [00:56:20:401]: ******* RunEngine:
******* Product: {130A3BE1-85CC-4135-8EA7-5A724EE6CE2C}
******* Action:
******* CommandLine: **********
MSI (s) (38:BC) [00:56:20:401]: Machine policy value 'DisableUserInstalls' is 0
MSI (s) (38:BC) [00:56:20:401]: Adding MSIINSTANCEGUID to command line.
MSI (s) (38:BC) [00:56:20:417]: Warning: Local cached package 'C:\WINDOWS\Installer\5307c.msi' is missing.
MSI (s) (38:BC) [00:56:20:417]: User policy value 'SearchOrder' is 'nmu'
MSI (s) (38:BC) [00:56:20:417]: User policy value 'DisableMedia' is 0
MSI (s) (38:BC) [00:56:20:417]: Machine policy value 'AllowLockdownMedia' is 0
MSI (s) (38:BC) [00:56:20:417]: SOURCEMGMT: Media enabled only if package is safe.
MSI (s) (38:BC) [00:56:20:417]: SOURCEMGMT: Looking for sourcelist for product {130A3BE1-85CC-4135-8EA7-5A724EE6CE2C}
:
:
:
:
:
MSI (s) (38:BC) [00:57:27:434]: Opening existing patch 'c:\WINDOWS\Installer\8e91f857.msp'.
MSI (s) (38:BC) [00:57:27:450]: Note: 1: 2203 2: c:\WINDOWS\Installer\8e91f857.msp 3: -2147287038
MSI (s) (38:BC) [00:57:27:450]: Couldn't find local patch 'c:\WINDOWS\Installer\8e91f857.msp'. Looking for it at its source.MSI (s) (38:BC) [00:57:27:450]: Resolving Patch source.
MSI (s) (38:BC) [00:57:27:481]: User policy value 'SearchOrder' is 'nmu'
MSI (s) (38:BC) [00:57:27:481]: SOURCEMGMT: Media enabled only if package is safe.
MSI (s) (38:BC) [00:57:27:481]: SOURCEMGMT: Looking for sourcelist for product {4A35EF4A-D868-4B15-A84D-3E8925AA9558}MSI (s) (38:BC) [00:57:27:512]: SOURCEMGMT: Adding {4A35EF4A-D868-4B15-A84D-3E8925AA9558}; to potential sourcelist list (pcode;disk;relpath).
MSI (s) (38:BC) [00:57:27:512]: SOURCEMGMT: Now checking product {4A35EF4A-D868-4B15-A84D-3E8925AA9558}
MSI (s) (38:BC) [00:57:27:512]: SOURCEMGMT: Media is enabled for product.
MSI (s) (38:BC) [00:57:27:512]: SOURCEMGMT: Attempting to use LastUsedSource from source list.
MSI (s) (38:BC) [00:57:27:512]: SOURCEMGMT: Trying source c:\8bfd1e16f50b137a6a8397c0\HotFixSQL\Files\.
MSI (s) (38:BC) [00:57:27:528]: Note: 1: 2203 2: C:\8bfd1e16f50b137a6a8397c0\HotFixSQL\Files\sqlrun_sql.msp 3: -2147287037
MSI (s) (38:BC) [00:57:27:528]: SOURCEMGMT: Source is invalid due to missing/inaccessible package. MSI (s) (38:BC) [00:57:27:528]: Note: 1: 1706 2: -2147483647 3: sqlrun_sql.msp
MSI (s) (38:BC) [00:57:27:528]: SOURCEMGMT: Processing net source list.
MSI (s) (38:BC) [00:57:27:528]: Note: 1: 1706 2: -2147483647 3: sqlrun_sql.msp
MSI (s) (38:BC) [00:57:27:528]: SOURCEMGMT: Processing media source list.
MSI (s) (38:BC) [00:57:28:715]: SOURCEMGMT: Resolved source to: 'sqlrun_sql.msp'
MSI (s) (38:BC) [00:57:59:606]: Note: 1: 1314 2: sqlrun_sql.msp
MSI (s) (38:BC) [00:57:59:606]: Unable to create a temp copy of patch 'sqlrun_sql.msp'.
MSI (s) (38:BC) [00:57:59:638]: Note: 1: 1708
MSI (s) (38:BC) [00:57:59:638]: Note: 1: 2729
MSI (s) (38:BC) [00:57:59:685]: Note: 1: 2729
MSI (s) (38:BC) [00:57:59:685]: Product: Microsoft SQL Server 2005 -- Installation failed.
MSI (s) (38:BC) [00:57:59:981]: MainEngineThread is returning 1635
This patch package could not be opened. Verify that the patch package exists and that you can access it, or contact the application vendor to verify that this is a valid Windows Installer patch package.C:\Scratch\SQL Svr Standard Edtn 2005 Win32 English Disk Kit MVL MVLS Only CD DVD\1\Setup\SqlRun_SQL.msi
MSI (c) (80:04) [00:58:00:091]: Decrementing counter to disable shutdown. If counter >= 0, shutdown will be denied. Counter after decrement: -1
MSI (c) (80:04) [00:58:00:138]: MainEngineThread is returning 1635
=== Verbose logging stopped: 9/15/2009 0:58:00 ===

1. The very first error I see in the above log is Local cached package 'C:\WINDOWS\Installer\b7bac95c.msi' is missing. This file b7bac95c.msi is the cached msi file for sqlrun_sql.msi i.e. the main installation (RTM) of an instance of SQL Server Database Engine. During installation of RTM product, ..\Servers\Setup\SqlRun_SQL.msi is cached to %windir%\Installer folder for future use such as un-installation etc. Cached file has a different name which is a randomized alphanumeric name generated by the installer , this is because if you have multiple instances then multiple ‘Sqlrun_sql.msi’ has to be cached and hence a name conflict may occur. Anyways, if you know the GUID (ProductCode) of the instance , you can find the name of this cached file , go to
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\\InstallProperties
In the right pane , look for the value of "LocalPackage" .

2. Now that we knew 'C:\WINDOWS\Installer\b7bac95c.msi' is missing , we need to put it back to C:\Windows\Installer. To do so , locate your main installation setup of SQL Server (viz. CD media or setup folder copied on local disk) , copy Servers\Setup\SqlRun_SQL.msi and paste it into C:\Windows\Installer , rename it to the cached filename , in our case we have to rename it to b7bac95c.msi. Double check that C:\WINDOWS\Installer\b7bac95c.msi is available now.

3. Let’s look for the next error in the log file and that is Couldn't find local patch 'C:\WINDOWS\Installer\8e91f857.msp'. Looking for it at its source .The filename 8e91f857.msp employs it is a MSP file means a patch , not a main product. I want to make it clear here that If we apply any patch , installer looks for the cached files of previously applied patche(s) as well as the cached msi file. So this cached msp file is also missing and we need to put it back. But the challenge is how to find which patch is this ? It is explained in following steps.

4. In the following lines in log file, we see : Looking for sourcelist for product {4A35EF4A-D868-4B15-A84D-3E8925AA9558} means the PatchCode for missing msp file 8e91f857.msp is {4A35EF4A-D868-4B15-A84D-3E8925AA9558}. Also, from ******* Product: {130A3BE1-85CC-4135-8EA7-5A724EE6CE2C} , we know the ProductCode i.e. GUID is {130A3BE1-85CC-4135-8EA7-5A724EE6CE2C} .
So we have below info till this point :
GUID = {130A3BE1-85CC-4135-8EA7-5A724EE6CE2C}
PatchCode = {4A35EF4A-D868-4B15-A84D-3E8925AA9558}
The GUID and PatchCode are stored in registry in encrypted format. Search* (See Search Hints below) them ,open regedit , go to
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\\Patches\
Here is a catch, values for "GUID" & "PatchCode" will be in there reverse order like below:GUID = {1EB3A031CC585314E87AA527E46EECC2} PatchCode = {A4FE53A4-868D-51B4-8AD4-E39852AA5985)
In the right pane , see the value of "DisplayName" . In our case , it was like below :
DisplayName = Service Pack 2 for SQL Server Database Services 2005 ENU (KB921896)
So DisplayName explains that {4A35EF4A-D868-4B15-A84D-3E8925AA9558} is Patch Code for KB921896 i.e. SP2 .

5. Recall Step 3 , we found the missing msp file i.e. 8e91f857.msp is for KB921896 . In order to put his cached msp file back , download KB921896 , extract it at a command prompt using switch /extract , go to extracted directory of KB921896 and copy ..\ hotfixsql\files\sqlrun_sql.msp , paste it in C:\Windows\Installer and rename it to 8e91f857.msp . Double check that 'C:\WINDOWS\Installer\8e91f857.msp' exists now.

6. Now that we have made available both the missing files (msi and msp) , apply the patch (in our case KB953752) again which failed to install .It should succeed now. However , in case if it fails again, that may be due to the fact that it might be looking for some other patch which might have been applied previously and is missing now. If so, failure installation will again generate verbose log , open the verbose log (like SQL9_Hotfix_KB953752_sqlrun_sql.msp.log) , look at 15-20 lines above the bottom of the log file , you may find Couldn't find local patch , put the appropriate cached msp file back to C:\Windpws\Installer , rename it appropriately and re-try the setup.
[It’s expected that log file after performing above steps won’t complain for ‘*.msi file is missing’ anymore .]

7. For other failing components like AS, RS, NS, Tools, IS etc please follow the troubleshooting method as explained in step1 to step6 looking at the appropriate verbose log file. Please note that GUID, msp filename and msp file path in the extracted folder would be different for different components. Choose carefully.

What is MSP ?
--------------------
A MSP (Microsoft Software Patch) is basically a set of transforms (modifications) against a baseline MSI (Microsoft Software Installer) file. When a MSP is installed, instead of updating the contents of the cached MSI (modifying it according to the transforms included in the MSP), Windows Installer simply caches the MSP. Then each time the MSI is run, the cached MSP is applied on the fly again. The reason it works this way is to simplify the authoring and building of MSPs, so that each MSP can be built only against RTM and not have to know about all the possible MSPs that have been released since then. For example, when SP2 is applied the SP1 patch is obsolete and Windows Installer just applies SP2 directly against the cached contents of the RTM product.

I Hope this will help.
Regards
GURSETHI

Wednesday, September 16, 2009

What Events C2 Audit Records?

Hi Friends,

We have option called C2 Audit in side SQL Server but do we know what actually got recorded using this. So breaking the mystry walls here are those things:

The following auditable events are provided for C2 certification of SQL Server:


  • End User Activity (for example, all SQL commands, logins, and logouts)

  • DBA Activity (Data Definition Language statements other than Grant/Revoke/Deny and security events, Configuration [Database or Server])

  • Security Events (Grant/Revoke/Deny, login/user/role add/remove/configure)

  • Utility Events (Backup/Restore/ Bulk Insert/BCP/DBCC commands)

  • Server Events (Shutdown, Pause, Start)

Auditing registers shutdown operations if performed from within SQL Server. However, if the server is
shutdown from the operating system or manually, the auditing event will not be
traced except as a

request from the operating system.

The Windows NT 4.0 Service Control Manager does not notify SQL Server who started a service—
just
that the service is being started. Consequently, you must audit service control actions in Windows
NT
4.0 to get a complete audit trail of SQL Server activity.

  • Audit Events (Start Audit, Stop Audit)
    Each audit event record contains (at a minimum):


Date and time of each event (start time of the event).
Windows NT 4.0 Domain name of the user who caused the event to occur.
Windows NT 4.0 UserName of the user who caused the event to occur.
The Windows NT 4.0 Security ID (SID) of the user who caused the event to occur.
Type of event (Event Class and Subclass).
Success or failure of the event.
Server name of the SQL Server.
Origin of the request (Windows NT 4.0 client computer name).
Name of the application the user is running.
Server process id (SPID) of the user's SQL Server connection.
For more information on these events, see "Security Audit Event Category." For information on how to enable auditing, see the "Setting the Audit Trace Option," which follows, and "Auditing SQL Server Activity"in SQL Server Books Online.
Please follow below link and download a good informational document file : SQL 2000 C2 Admin and user guide.http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=71C146F3-9907-40CD-BABF-3506ECD33254

Regards
GURSETHI

Template Viewer - A Good One

Hi Guys,

There are times when we are in middle of doing something and need syntax for a statement which we want to execute. What we do during this time. If Books On Line is there good else need to search on GOOGLE.

Now there is a path that we can follow. Have a look.

This Template Viewer is a one stop shop for commands which we use daily and that also grouped.














Regards
Gursethi

Saturday, September 12, 2009

Database Inventory

Hello Guys,

This script is dedicated to those of my colleagues who inspire me to work on it as I am not able to see the pain in which they are going thru while collecting server information that they are supporting.

Note: I am Still in process of making changes in this and will keep on updating this
post in future.


Special Thanks : A Special Thanks to ABHY CHAUDHARY my dear buddy who helped me a lot
while work on this.

Alter procedure SQLInventory as
declare @image_path varchar(100)
declare @startup_type int
declare @startuptype varchar(100)
declare @start_username varchar(100)
declare @instance_name varchar(100)
declare @system_instance_name varchar(100)
declare @log_directory varchar(100)
declare @key varchar(1000)
declare @registry_key varchar(100)

SET @instance_name = coalesce(convert(nvarchar(100), serverproperty('InstanceName')),'MSSQLSERVER');
if @instance_name!='MSSQLSERVER'
set @instance_name='MSSQL$'+@instance_name

EXEC master.dbo.xp_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\Microsoft SQL Server\Instance Names\SQL', @instance_name, @system_instance_name output;

set @key=N'SYSTEM\CurrentControlSet\Services\' +@instance_name;

SET @registry_key = N'Software\Microsoft\Microsoft SQL Server\' + @system_instance_name + '\MSSQLServer\Parameters';
if @registry_key is NULL
set @instance_name=coalesce(convert(nvarchar(100), serverproperty('InstanceName')),'MSSQLSERVER');
EXEC master.dbo.xp_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\Microsoft SQL Server\Instance Names\SQL', @instance_name, @system_instance_name output;
SET @registry_key = N'Software\Microsoft\Microsoft SQL Server\' + @system_instance_name + '\MSSQLServer\Parameters';

EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@key,@value_name='ImagePath',@value=@image_path OUTPUT
EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@key,@value_name='Start',@value=@startup_type OUTPUT
EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@key,@value_name='ObjectName',@value=@start_username OUTPUT
EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE',@registry_key,@value_name='SQLArg1',@value=@log_directory OUTPUT
set @startuptype= (select 'Start Up Mode' =
CASE
WHEN @startup_type=2 then 'AUTOMATIC'
WHEN @startup_type=3 then 'MANUAL'
WHEN @startup_type=4 then 'Disabled'
END)

SELECT @image_path as 'Binaries Path'
,@start_username as 'Statrtup User'
,@log_directory as 'Error Logs Location'
,@Startuptype
,serverproperty ('ComputerNamePhysicalNetBIOS') as machine
,serverproperty ('Instancename')
,(SELECT 'file_type' =
CASE
WHEN s.groupid <> 0 THEN 'data'
WHEN s.groupid = 0 THEN 'log'
END) AS 'file_type'
, d.database_id as 'DB_ID'
, d.name AS 'db_name'
, s.name AS 'logical_file_name'
, s.filename AS 'physical_file_name'
, (s.size * 8 / 1024) AS 'file_size(MB)' -- file size in MB
,d.compatibility_level
, DATABASEPROPERTYEX (d.name,'Recovery') as Recovert_Model
, DATABASEPROPERTYEX (d.name,'Status') as Database_Status
,serverproperty('productlevel') as ServicePack
,serverproperty('edition') as edition
,serverproperty('productversion') as version
,serverproperty('collation') as collation
,serverproperty('Isclustered') as IS_Clustered
,serverproperty('IsFullTextInstalled') as IS_FullText
,d.is_published as Published
,d.is_subscribed as Subscribed
,d.is_distributor as Distributor
,m.mirroring_state
,substring(k.physical_device_name ,1,59) as backup_directory,
CASE l.[type]
WHEN 'D' THEN 'Full'
WHEN 'I' THEN 'Differential'
WHEN 'L' THEN 'Transaction Log'
END as BackupType

FROM
sys.databases d INNER JOIN sys.sysaltfiles s
ON
d.database_id=s.dbid
INNER JOIN sys.database_mirroring m
ON
d.database_id=m.database_id
INNER JOIN msdb.dbo.backupset l
ON
l.database_name=d.name
INNER JOIN msdb.dbo.backupmediafamily k
ON
l.media_set_id=k.media_set_id
ORDER BY
d.name

Friday, April 3, 2009

Error message when you open SQL Server Configuration Manager

Couple of times when we tried to start SQL Server Configuration Manager it fails with errors
like below

"Cannot connect to WMI provier. You do not have permission or the server is unreachable.
Note that you can only manager SQL Server 2005 servers with SQL server Configuration
Manager. Invalid class [0x80041010]"

Reason : -
The reason behind this error is Sql Server Setup had failed to register the following .MOF file:C:\Program Files (x86)\Microsoft SQL Server\90\Shared\sqlmgmprovider.mof . This sqlmgmprovider.mof should have been registered on your machine during Sql server setup.
Solution :-

We would do what Sql server setup should have done.
1. Run cmd to get command prompt.
2. Run the below command,
a) C:\ Program Files (x86)\Microsoft SQL Server\90\Shared>mofcomp "C:\ Program Files
(x86)\Microsoft SQL Server\90\Shared\sqlmgmprovider.mof"
3. If the above doesn't work you can even try to register the file sqlmgmproviderxpsp2up.mof as
below.
a) C:\ Program Files (x86)\Microsoft SQL Server\90\Shared>mofcomp "C:\ Program Files
(x86)\Microsoft SQL Server\90\Shared\sqlmgmproviderxpsp2up.mof"

KB: 956013: Error message when you open SQL Server Configuration Manager in SQL Server
2008: "Cannot connect to WMI provider. You do not have permission or the
server is unreachable"

Saturday, February 21, 2009

How To Detach All User Database?

SQL 2005
===========

select 'EXEC sp_detach_db @dbname = N'''+RTRIM ( name)+''','''+'true''' from sys.sysdatabases where dbid>6 and name not like 'distribution'


SQL 2000
==========

select 'EXEC sp_detach_db @dbname = N'''+RTRIM ( name)+''','''+'true''' from sysdatabases where dbid>4 and name not like 'distribution'

To Which Database Does The Object Belong

You know the object name but you do not know to which database does it belongs :) ..

Try msforeacdb :
sp_msforeachdb 'use ?;print "?";select name from sys.sysobjects where name='''''