Tuesday, February 28, 2006

SQL statement along with its active SPIDS

Find out the actual SQL statement along with its active SPIDS that are currently running and not just the last statement sent to SQL server (as DBCC INPUTBUFFER displays). Below scripts will display the current activity of ALL spids that are currently active along with its SQL statements.

--SQL 2000 Syntax

SET NOCOUNT ON
GO
DECLARE
@SPID INT,
@last_Batch datetime,
@hostname varchar(32),
@loginame varchar(32),
@bHandle BINARY(20),
@stmt_start INT,
@stmt_end INT,
@waittime int

--Get spids in loop, only where there is some statement in the buffer.
SET @SPID=(SELECT MIN(SPID) FROM Master.dbo.SYSPROCESSES WHERE (stmt_start<>0 or stmt_end<>0) and SPID<>@@SPID)

WHILE @SPID IS NOT NULL
BEGIN
SELECT
@last_Batch=last_batch,
@hostname=hostname,
@loginame=loginame,
@bHandle=sql_handle,
@stmt_start = stmt_start/2,
@stmt_end = stmt_end/2,
@waittime = waittime
FROM
MASTER.DBO.SYSPROCESSES
WHERE SPID = @SPID AND ecid = 0

IF @stmt_end = 0
SELECT @SPID,@last_batch,
@hostname,
@loginame,
SUBSTRING(text,@stmt_start,8000)
FROM ::fn_get_sql(@bHandle)
ELSE
SELECT @SPID,@last_batch,
@hostname,
@loginame,
SUBSTRING(text,@stmt_start,@stmt_end - @stmt_start)
FROM ::fn_get_sql(@bHandle)

--GET NEXT SPID
SET @SPID=(SELECT MIN(SPID)
FROM Master.dbo.SYSPROCESSES
WHERE (stmt_start<>0 or stmt_end<>0)
AND SPID<>@@SPID
AND SPID>@SPID)
END
GO

--SQL 2005 Syntax
SELECT
s2.session_id,
s2.start_time,
s1.host_name,
s1.login_name,
s2.command,
s2.open_transaction_count,
(SELECT TOP 1 SUBSTRING(s3.text, statement_start_offset / 2,
((CASE WHEN statement_end_offset = -1 THEN
(LEN(CONVERT(nvarchar(max),s3.text)) * 2)
ELSE statement_end_offset
END) - statement_start_offset) / 2)) AS sql_statement
FROM Master.sys.dm_exec_sessions s1
INNER JOIN Master.sys.dm_exec_requests s2 on s1.session_id=s2.session_id
CROSS APPLY Master.sys.dm_exec_sql_text(s2.sql_handle) AS s3
WHERE s2.sql_handle is NOT NULL
AND s2.session_id<>@@SPID

SQL Server backup across more than one file

You can stripe a SQL Server’s database backup across more than one file. Striping a SQL Server backup across more than one backup file can provide performance advantages as well as make it possible to backup a VLDB where there is no single drive available with enough free space. Striped backups are supported in SQL Server 2000 & SQL Server 2005.

-- take a striped backup of the DB. Can be many more files than just two.
backup database northwind
to disk='\\Server1\t$\northwind1.bak',
disk='\\Server2\h$\northwind2.bak'

-- take a look at the logical files in the DB so we can move them on restore
restore filelistonly
from disk='\\Server1\t$\northwind1.bak',
disk='\\Server2\h$\northwind2.bak'

-- restore a DB from a striped backup
restore database northwind
from disk='\\Server1\t$\northwind1.bak',
disk='\\Server2\h$\northwind2.bak'
with move 'northwind_Data' to 't:\northwind2.mdf',
move 'northwind_Log' to 'h:\northwind2.ldf',
replace

Wednesday, February 22, 2006

GotDotNet Code Gallery

Are you looking for a place where you can share, find, download, evaluate and discuss evolving .NET applications, uncompiled code, ideas, and technical documents?
You have a greate place to go now, CodeGallery at
http://www.gotdotnet.com/codegallery/

Tuesday, February 21, 2006

SQL Server 2005 - Default Trace

SQL Server 2005 out of the box comes with a default trace that's always running which tracks configurational changes and process level information. You may obtain this information from SQL Server Management Studio by higlighting the registered server and selecting "Reports" from the summary page:

You will find:
Configuration change history
Schema Changes History
Memory Consumption
All Blocking Transactions
Top Sessions
Top Connections
Top Transactions by Age
Top Queries by Average CPU time
Top Queries by Average IO & lot of other information

You may also query the default trace file using the below query:
SELECT * FROM fn_trace_gettable ('C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\LOG\log.trc', default)

GO

SQL 2005 COPY_ONLY Backup

In SQL Server 2000 or it's earlier versions, if you perform a backup of a database out of sequence/chain (Full & Diffrential or Full & Log, etc) it used to break the sequence of the backups. Meaning you had to reset the backup sequence everytime you had to take a seperate out of sequence backups of the database. Taking a backup normally changes the database, in turn affecting other backups and how they are restored. Sometimes, however, a backup must be taken for a special purpose that should not affect the overall backup and restore sequence or procedures for the database.

With SQL Server 2005, you can now perform an out of sequence backups using "COPY_ONLY" option with the backup statement and this option is available for all types of backups.

Note:
A Full backup taken with the COPY_ONLY option cannot be used as a base backup and does not affect any existing differential backups.
A Diffrential backup taken with the COPY_ONLY option is identical to a regular diffrential backup.
A Log backup taken with COPY_ONLY option causes the backup to retain the current log archive point and also the Transaction log is not truncated by a log backup.
Important: SQL Server Management Studio does not support COPY_ONLY backup/restore functionality, but you can use BACKUP & RESTORE commands using T-SQL for COPY_ONLY backup of a database.
Example:
Backup database AdventureWorks to Disk='D:\AdventureWorks.bak' with COPY_ONLY

Wednesday, February 08, 2006

Can a Web Application with no SSL use ADFS Web Agent NT Token to authenticate users?
It would not work because it require SSL and to mark cookies as secure only. Cookies are not encrypted and without SSL they could be easily attacked. The return URL must be an https-based URL.
Is there a sample of single sign-on using ADFS?
Here is a step by step guide to setup ADFS:
http://www.microsoft.com/downloads/details.aspx?familyid=062F7382-A82F-4428-9BBD-A103B9F27654&displaylang=en

How to use ADSchemaAnalyzer tool?
For the ADSchemaAnalyzer tool, the target schema is the AD instance and the base schema is the ADAM instance.
To use the AdamSyncer tool you need to do the following:
Import the LDIF files MS-AdamSchemaW2K3.LDF and MS-AdamSyncMetadata.LDF into the Adam instance.
Using the AdSchemaAnalyzer tool check if the schema of the AD instance and ADAM instance are the same.
Open the AdSchemaAnalyzer tool from c:\windows\adam
Choose File / Target, set server: localhost:389
Choose File / Base, set server: localhost:50000
Select Schema / Mark all non-present elements as included
Select File / Create Ldif file.
Save to r2-diff.ldf