Next step of programming

Just another WordPress.com weblog

Posts Tagged ‘QUERY

Getting row count without using count

without comments

To get the count of the rows we normally use

select count(ContactID) from AdventureWorks.Person.Contact 

But there is another way which is fast and less on cost as compared to previous method and the way is

select rows from sysindexes where id = OBJECT_ID('AdventureWorks.Person.Contact')
 and indid < 2

Below see the query cost details

Query Cost Details

Query Cost Details

Written by A.Sethi

December 10, 2008 at 12:46 pm

Posted in MS SQL SERVER

Tagged with , , ,

Check who all are connected to SQL Server

with 2 comments

To check who all are connected to sql server and what they are doing, is a very important requirement. It can be required when you want to perform some exclusive work on you database(s) or for some other work also. In order to check who all are there and connected to which database we can use following stored procedure.

–Check who all connected and doing what
exec sp_who
go

It will give you the following result 

 

Result of store procedure sp_who

Result of store procedure sp_who

Written by A.Sethi

September 8, 2008 at 7:04 am

Getting sql query output in XML

without comments

Most of the time we will be executing sql queries and getting results as number of rows mainly records. In order to get those records in the XML format we can modify our existing query by adding just few keywords. In the result of those queries we will get the result in raw or formatted XML. 

Normal query 

Select * from customers

Result of select query

Result of select query

Query for getting result in XML format

Select * from customer FOR XML RAW or

Result of query with "for xml raw" keywords

Result of query with

Select * from custoemr FOR XML AUTO

Result of query "for xml auto" keywords

Result of query

Now we will get the result in well formed xml by getting the elements well tagged and root node as custoemr with following query

Select * from customer FOR XML AUTO, ELEMENTS, ROOT(‘customer’)

Result of Query "for xml auto, elements, root('customer')" keywords

Result of Query

Written by A.Sethi

September 3, 2008 at 11:20 am

Posted in MS SQL SERVER

Tagged with , , ,