12.31.07

Useful SQL Server 2005 queries

Posted in Important SQL Query, SQL Server tagged , , , , , , at 4:27 am by dayananthan

Here I am listing out some of the useful important queries used in sql server. Hope it will be useful. I will keep updating this post….

How to find out recently run queries in SQL Server 2005? 

Select dmStats.last_execution_time as ‘Last Executed Time’,dmText.text as ‘Executed Query’ from sys.dm_exec_query_stats as dmStats Cross apply sys.dm_exec_sql_text(dmStats.sql_handle) as dmText Order BydmStats.last_execution_time desc

Run the following query to export data from sql server to excel  

USE [AdventureWorks]

INSERT INTO OPENROWSET (‘Microsoft.Jet.OLEDB.4.0′, ‘Excel 8.0;Database=c:\Test.xls;’,‘Select * from [Emp$]‘)

Select EmployeeID, Title FROM HumanResources.Employee

Query to find Object Owner? 

SELECT
A.NAME AS [OBJECT],
B.NAME AS [SCHEMA],

USER_NAME(ISNULL(A.PRINCIPAL_ID,B.PRINCIPAL_ID)) as [OWNER]

FROM

SYS.ALL_OBJECTS A INNER JOIN SYS.SCHEMAS B
ON A.SCHEMA_ID = B.SCHEMA_ID
WHERE A.NAME =
‘object name here’
 

Query to select distinct records without duplicate 

select EmpId,EmpName from Emp group by EmpID,EmpName having count(*)>1 

PIVOT 

select * from products

ProductID

ProductName Year TotalDue
702 MoutainBlack 2005 124566
703 Pen 2005 45321
704 Gloppy 2003 454875
705 Watch 2003 456586
706 Disc 2004 4565456
707 Time 2004 4562164

Now you want to Cross-tab year based results

select ProductID,ProductName,SUM(case TheYear when 2003 then TotalDue else 0 end) as [2003],SUM(case TheYear when 2004 then TotalDue else 0 end) as [2003],SUM(case TheYear when 2005 then TotalDue else 0 end) as [2005]from productsgroup by ProductID,ProductName

(or same we can achive using PIVOT )

–pivot

select ProductID,ProductName,[2003],[2004],[2005] from products

pivot

(sum(TotalDue) for TheYear in([2003],[2004],[2005])) as PVT 

–Results

ProductID

ProductName 2003 2004 2005
702 MoutainBlack NULL NULL 124566
703 Pen NULL NULL 45321
704 Gloppy 454875 NULL NULL
705 Watch 456586 NULL NULL
706 Disc NULL 4565456 NULL
707 Time NULL 4562164 NULL

 To Find all dependency objects and its type

select ObjectName = object_name(id),b.Type_Desc Object_type, DependObjectName =  object_name(depid),c.Type_Desc DependObject_Type from sys.sysdepends a inner join sys.objects b on a.id = b.object_id inner join sys.objects c on a.depid = c.object_id

Note : Script does not take care about dynamic sql statement present in the sp

Keep on coming …… Dayaa

12.19.07

CLR Integration with SQL Server 2005

Posted in SQL Server tagged , , , , at 3:52 am by dayananthan

CLR Integration in SQL Server 2005
——————————————

Introduction to CLR:

The .NET Framework Common Language Runtime (CLR) is an environment that executes compiled code written in programming languages such as C# and VB.NET. The code is compiled to a file called an assembly that contains the compiled code together with an assembly manifest. The manifest contains metadata about the assembly, including types, methods, and inheritance relationships. Code running within the CLR is called managed code.
The CLR provides services such as automatic garbage collection, security support, and runtime type checking. Because the compiled code is executed by the CLR rather than directly by the operating system, managed code applications are platform- and language-independent.

The Common Language Runtime, popularly known as CLR is the heart and soul of the .NET Framework.

The CLR Provides a number of services that include:
–Loading and execution of programs
–Common Type Systems
–Compilation of IL into Native Code executable code
–Providing Metadata
–Memory Management
–Enforcement of Security
–Interoperability with other systems
–Managing Exceptions and Errors

Common Type System ( CTS )

The .Net Framework provides multiple support using the feature known as Common type system that is build into the CLR.The CTS Supports variety of types and operations found in most programming languages and therefore calling one language from another does not require type conversions.

Common Language Specification ( CLS )
CLS is the subset of CTSThe CLS defines a set of rules that enables interoperability on the .Net platform

Microsoft Intermediate Language ( MSIL )
Contains instructions for loading, storing, initializing and calling methods
Metadata
Metadata is machine-readable information about a resource, or “data about data”.In .NET metadata includes type definitions, version information, external assembly references and other standard information
Managed Code
The Code that satisfies the CLR at runtime in order to execute is referred to as managed code.
Execution of CLR:
——————

Steps to enable the CLR Integration
To Enable the CLR Integration
sp_configure
‘clr enabled’,1

To Create Assembly
CREATE ASSEMBLY MyFirstCLR
FROM
‘C:\MyFirstCLR.dll‘
WITH PERMISSION_SET = SAFE

MyFirstCLR is the assembly name

Assembly Specification
SAFE:

This is the default level and it is the most restrictive. This means that your code does not need any external resources in addition to the operation. Safe code can access data from the local SQL Server databases or perform computations and business logic.

EXTERNAL_ACCESS:

This level signifies that certain external resources, such as files, networks, Web services, environmental variables, and the Registry, are accessible. UNSAFE:

This level, which you should try very hard to avoid, specifies that your code is allowed to do anything. In other words, you are requesting to be free of any granular-level control, and thus have the same permissions as an extended stored procedure. using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void GetEmployeeDetails()
{
SqlCommand command = new SqlCommand();
//Use the context connection
command.Connection = new SqlConnection(“Context connection=true”);
command.Connection.Open();
//Define the T-SQL to execute
string sql =”select * from Employee”;
command.CommandText = sql.ToString();
//Get the data
SqlContext.Pipe.ExecuteAndSend(command);
//Close the Connection
command.Connection.Close();
}
};