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)
Comments Off |
Sql Tip |
Permalink
Posted by kamalraaja
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‘
Leave a Comment » |
Sql Tip |
Permalink
Posted by kamalraaja
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′
Leave a Comment » |
Sql Tip |
Permalink
Posted by kamalraaja
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.
Leave a Comment » |
Sql Tip |
Permalink
Posted by kamalraaja