Transactions in Sql Server

August 28, 2009

To Check any active transactions which is not commited:

SELECT * FROM sys.dm_tran_session_transactions

/* This work like activity Monitor */

To See all Executing Queries:

sp_who2 active

DBCC inputbuffer(blkby) [To check sp_who2 blkby id executing query or procedure]

declare @table table(spid int,status varchar(50),[login] varchar(100),hostname varchar(100),
blkby varchar(100),dbname varchar(15),command varchar(100),cputime int,diskio int,
lastbatch varchar(100),programname varchar(100),spid1 int,requestid int)
insert into @table
exec sp_who2 active
select spid,status,login,hostname,cputime,command,blkby from @table where ltrim(rtrim(hostname))<>’.’ and dbname<>’master’
and ltrim(rtrim(blkby))<>’.’ and status<>’sleeping’ and isnull(login,”)<>”
order by hostname

DBCC inputbuffer(blkby)


How to get last date of a Month in Sql Server

April 15, 2009

declare @date varchar(10)

set @date=convert(varchar,year(getdate()))+’-'+convert(varchar,(month(getdate())+1))+’-01′

select dateadd(day,-1,@date)

Logic is which month we want to find the last date take that month.

Suppose i want to find last day of   14-04-2009

Add 1 month to this and create a format of next month first day

the next month will be 05 ,2009

2009-05-01

With dateadd function add -1 day to that function . So it will come to the previous month last day.

dateadd(day,-1,’2009-05-01′)

it will display   ‘2009-04-31


Inserting using one insert statement

March 27, 2009

USE YourDB
GO
INSERT INTO MyTable (FirstCol, SecondCol)
SELECT ‘First’ ,1
UNION ALL
SELECT ‘Second’ ,2
UNION ALL
SELECT ‘Third’ ,3
UNION ALL
SELECT ‘Fourth’ ,4
UNION ALL
SELECT ‘Fifth’ ,5
GO


creating tables from other tables in sql 2005

March 27, 2009

Method 1 : INSERT INTO SELECT
This method is used when table is already created in the database earlier and data is to be inserted into this table from another table. If columns listed in insert clause and select clause are same, they are are not required to list them. I always list them for readability and scalability purpose.
USE AdventureWorks
GO
—-Create TestTable
CREATE TABLE TestTable (FirstName VARCHAR(100), LastName VARCHAR(100))
—-INSERT INTO TestTable using SELECT
INSERT INTO TestTable (FirstName, LastName)
SELECT FirstName, LastName
FROM Person.Contact
WHERE EmailPromotion = 2
—-Verify that Data in TestTable
SELECT FirstName, LastName
FROM TestTable
—-Clean Up Database
DROP TABLE TestTable
GO

Method 2 : SELECT INTO
This method is used when table is not created earlier and needs to be created when data from one table is to be inserted into newly created table from another table. New table is created with same data types as selected columns.
USE AdventureWorks
GO
—-Create new table and insert into table using SELECT INSERT
SELECT FirstName, LastName
INTO TestTable
FROM Person.Contact
WHERE EmailPromotion = 2
—-Verify that Data in TestTable
SELECT FirstName, LastName
FROM TestTable
—-Clean Up Database
DROP TABLE TestTable
GO


Some SQL Tips

May 22, 2008

How to replace spaces in text stored in sql server with in function or Store Proceduer?

declare @name as varchar(50)
declare @rep as varchar
set @rep=’ ‘
set @name=’kamal   raja       v’
select replace(@name,@rep,”)

It will replaces the characters what ever you define in @rep that character is replaced from @name.

It is help full when writing functions or stored procedures , bcoz sql does not provides directly Trim() function which replaces spaces between given text.

select getdate()                      —– > To get Current Date

select datepart(day,getdate())  —–> To get Day

select datepart(month,getdate()) —-> To get Current Month

select datepart(year,getdate()) —-> To get Current Year

Some examples of converting date format:

select convert(varchar,getdate(),111)  =====>  ‘2008/5/21′

select convert(varchar,getdate(),106)  =====> ‘21 May 2008′


How to use Sql Profiler?

April 1, 2008

Take some example that some data base updations will be done through one of your screen in the project. To know what are the Sql server tables will be effected when doing some thing in that screen…………………….

Make sure that dabase will be on local machine….

Open Sql Profiler of Sql Server……………….

Go File –> New –> Trace

Then Trace Properties screen will appears and asks to give trace name. Give what ever name you like..

Click Run Button….

Now go for your project screen and click any button which will update or modify database. All the transactions will be loaded in the Trace of sql profiler.

means what are the queries executed when you clicked that button , which stored  procedures executed………etc….

Gives a sample view that what are the tables effected when click a button of a screen in the project.