Thursday, August 23, 2012

SQL Server Details

Below query displays the sql server name and product details along with CPU and RAM details
SELECT
SERVERPROPERTY('ServerName') AS [SQLServer],
SERVERPROPERTY('ProductVersion') AS [VersionBuild],SERVERPROPERTY('ProductLevel') AS [Product],SERVERPROPERTY ('Edition') AS [Edition],SERVERPROPERTY('IsIntegratedSecurityOnly') AS [IsWindowsAuthOnly],SERVERPROPERTY('IsClustered') AS [IsClustered],[cpu_count] AS [CPUs],round(cast([physical_memory_in_bytes]/1048576 as real)/1024,2) AS [RAM (GB)]FROM [sys].[dm_os_sys_info]

Wednesday, August 22, 2012

Retrieving Resultset Using XML

We can generate XML documents by using SQL Server FOR XML. There are 4 methods we can include with FOR XML are AUTO, EXPLICIT, PATH, RAW.

FOR XML AUTO - Displays nested XML tree with each column representing as a single element.

Sample Data




Examples

SELECT * FROM TestIdentity WHERE id = 1 FOR XML AUTO <TestIdentity Id="1" NAME="Kalyan" Phone="9090909092" City="Hyderabad" />

SELECT * FROM TestIdentity AS Students WHERE id = 1 FOR XML AUTO  <Students Id="1" NAME="Kalyan" Phone="9090909092" City="Hyderabad" />
SELECT
* FROM TestIdentity AS Students WHERE id in (1,2) FOR XML AUTO,ELEMENTS
<Students><
Id>1</Id><
NAME>Kalyan</NAME><
Phone>9090909092</Phone><
City>Hyderabad</City></
Students><
Students><
Id>2</Id><
NAME>Kishore</NAME><
Phone>8080808082</Phone><
City>Secunderabad</City></
Students>
SELECT * FROM TestIdentity WHERE id IN(1,2) FOR XML AUTO, ROOT('students')
<students><
Students Id="1" NAME="Kalyan" Phone="9090909092" City="Hyderabad" /><
Students Id="2" NAME="Kishore" Phone="8080808082" City="Secunderabad" /></
students>
SELECT CASE WHEN LEN(NAMES)>0 THEN LEFT(NAMES, LEN(NAMES)-1) ELSE '' END AS NAMES_CSV
FROM (SELECT T.NAME + ',' FROM TestIdentity T FOR XML PATH('')) TMP_NAMES (NAMES)
Kalyan,Kishore,Krishna,Raju,Ramesh,Shiva,Mallik,Naveen,Madhu,Srinu,Harish,Ramkumar

Using the below query we can create as csv

SELECT City, STUFF((SELECT ',' + T.NAME FROM TestIdentity T WHERE T.City = Ti.City FOR XML PATH('')),1,1,'')
FROM TestIdentity Ti GROUP BY City




Tuesday, August 21, 2012

Identity Columns

Using this property we can create an identity column in a table, An Identity column is a auto incrementing column. A table can have only one Identity column. SQL Server will automatically take care of identity columns. By default the column value will increment by 1, if require we can modify the increment value.


Identity (seed, increment) -- Here seed is the starting value and increment is the growth value.
If neither is specified then it treat as identity (1,1).

Example

CREATE TABLE TestIdentity (Id INT IDENTITY(1,1), NAME VARCHAR(20))
  
If you start inserting rows into the above table then it starts from 1 and increment by 1 from next row onwards.

INSERT INTO TestIdentity VALUES ('Kalyan')INSERT INTO TestIdentity VALUES ('Kishore')
By using DBCC CHECKIDENT command we can verify the current identity value of a table.

DBCC CHECKIDENT(TestIdentity) 
 
The disadvantage of identity property is, if you remove any row from a table then that gap will remains same.
If you want to insert value into identity column manually use the below command Identity_Insert on tablename, It will allow us to insert value into identity field  

SET IDENTITY_INSERT testIdentity ONINSERT
INTO TestIdentity (ID, NAME) VALUES (7, 'Mallik')SET IDENTITY_INSERT testIdentity OFF 
How to find last inserted identity value, SQL Server provides three methods to retrieve the value.
SELECT @@identity -- Returns last identity value irrespective of any table in the current session.
SELECT SCOPE_IDENTITY() -- Returns last identity value irrespective of any table in the current scope.SELECT IDENT_CURRENT('TestIdentity') -- Returns the last identity value of the specific table