Thursday, October 23, 2008

Shutdown Keyword in SQL Server 2005

The Shutdown a running Microsoft SQL Server service.




Example:
Just type in Query Window and execute to test:
SHUTDOWN WITH NOWAIT


Get List of Tables in a Database - Query INFORMATION_SCHEMA.Tables - ADO.NET

string connectionString = "...";
DataTable tables
= new DataTable("Tables");
using (SqlConnection connection =
new SqlConnection(connectionString))
{
SqlCommand command
= connection.CreateCommand();
command.CommandText
= "select table_name as Name from
INFORMATION_SCHEMA.Tables where TABLE_TYPE =
'BASE TABLE'
";
connection.Open();
tables.Load(command.ExecuteReader(
CommandBehavior.CloseConnection));
}

Monday, October 20, 2008

Basics of XQuery in SQL Server 2005

declare @x xml
set @x=''
SELECT @x.query('
for $a in (1, 2, 3)
return $a')
-- result = 1 2 3

declare @x xml
set @x=''
SELECT @x.query('
for $a in
for $b in (1, 2, 3)
return $b
return $a')
-- result = 1 2 3

declare @x xml
set @x='111'
SELECT @x.query('
for $a in (xs:string( "test"), xs:double( "12" ), data(/ROOT/a ))
return $a')
-- result test 12 111

declare @x xml
set @x='

step 1


step 2

'
SELECT @x.query('
for $step in /ManuInstructions/Location[1]/Step
return string($step)
')

-- result step1