Posts

Showing posts from March, 2013

Delete duplicate records in SQL Server

Declare @tbl As table  ( iid int, iname varchar(10) )   insert into @tbl values(1,'A') insert into @tbl values(2,'B') insert into @tbl values(3,'C') insert into @tbl values(4,'A') insert into @tbl values(5,'C') Select * from @tbl  Delete From @tbl Where iid in ( Select iid from  (Select * , ROW_NUMBER() Over ( partition by iname Order by iname ) As ICount  From @tbl) As Qry Where Qry.ICount > 1 ) Select * from @tbl

Repair Suspect Database using Query In SQL Server

This is the query to repair suspect database in SQL Server.   If your Database is marked as suspected, here are the steps to fix it.       In this you have replace “dbName” with suspected db name and run this query in master database. Step 1 --command to recover suspected database ALTER DATABASE DbName SET EMERGENCY DBCC checkdb('DbName') ALTER DATABASE DbName SET SINGLE_USER WITH ROLLBACK IMMEDIATE DBCC CheckDB ('DbName', REPAIR_ALLOW_DATA_LOSS) ALTER DATABASE DbName SET MULTI_USER          This step 1 cannot set Index created for that database so now we have to rebuild the index. This query should be run in that suspected database. Step 2 -- command to rebuild all indexes EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 80)"

Create Folder / Directory using SQL Server

Dear Friends we can use command of command prompt in sqlserver   use   xp_cmdshell   for fire batching command   Like   EXEC   xp_cmdshell   ' Command String ' EXEC xp_cmdshell 'dir *.exe'   to get all directory information EXEC xp_cmdshell 'mkdir kunal' to Create New Directory is deafult path system32 you can set Reference From : http://www.c-sharpcorner.com/Blogs/9569/create-directory-using-sqlserver.aspx 

Get Column values in a single coma separated string in SQL Server

Image
We can get all values of a column in a single coma separated string this query.  Declare @id  varchar(Max) Select @id = coalesce(@id + ',' ,'') + Cast(ItCode as varchar(10)) From ItMast Select @id  After you will get result like this Reference From : http://www.c-sharpcorner.com/Blogs/9637/get-a-column-value-in-a-single-coma-separated-string-in-sql.aspx

Get Nth Highest Value by SQL Query

CREATE   TABLE   [dbo] . [tblDupRecord] (       [id] [int]   NULL,       [name] [varchar] ( 50 )   COLLATE   SQL_Latin1_General_CP1_CI_AS   NULL,       [marks] [int]   NULL )   ON   [PRIMARY] GO insert   into   [tblDupRecord]   values ( 1 , 'Name1' , 25 ) insert   into   [tblDupRecord]   values ( 2 , 'Name2' , 50 ) insert   into   [tblDupRecord]   values ( 3 , 'Name3' , 35 ) insert   into   [tblDupRecord]   values ( 4 , 'Name4' , 40 ) insert   into   [tblDupRecord]   values ( 5 , 'Name5' , 45 ) insert   into   [tblDupRecord]   values ( 6 , 'Name6' , 45 ) GO   select   *   from   tblDupRecord  a where   2 =   ( select   count (   distinct ( b . marks ))   from   tblDupRecord b   where   a . marks   <= b . marks )   Go   select   top   1   with   ties   *   from   ( select   top   2   with   ties   *     from   tblDupRecord   order   by   marks   desc )   as   t   order   by marks   asc GO   with

Get comma Separated Data using XML Path

Select    Qry .*,   Case   When   Qry . Door   =   ''   Then   Qry . Door   Else     Substring ( Qry . Door ,   3 , Len ( Qry . Door )   -   2 )   End   As   Door From     ( SELECT   dbo . tblHardware . Cmp_Id ,   Hardware_Id   ,   Hardware_Code   AS   [Hardware Code] , ( Select   ', '   +   cast ( Door_Name   as   varchar )   FROM   tbldoor                            WHERE   tbldoor . Hardware_Id   = tblHardware . Hardware_Id   FOR   XML   path ( '' ))   Door FROM   dbo . tblHardware   WHERE   ( dbo . tblHardware . Is_Deleted   =   0 ) )   As   Qry    Reference By http://www.c-sharpcorner.com/Blogs/10297/fetch-comma-separated-data-using-xml-path.aspx

Find procedure and function in SQL Server

ROUTINE_TYPE =   'PROCEDURE' OR     'Function' Select   ROUTINE_TYPE , SPECIFIC_NAME , ROU TINE_DEFINITION FROM      INFORMATION_SCHEMA.ROUTINES WHERE     OBJECTPROPERTY ( OBJECT_ID ( SPECI FIC_NAME ), 'IsMSShipped' )   = 0          AND   ROUTINE_DEFINITION    like   '%prcleave%'          AND   ROUTINE_TYPE = 'PROCEDURE'

“Secrets of Life”

Winners believe!! Winners believe in themselves….in what they do. Belief drives passion… passion for a definite purpose. If we believe in ourselves, believe in our personal vision with unyielding determination, we succeed.  “Whatever the mind can conceive and believe… it can achieve.”                   – Napoleon Hill One of the important lessons I have learned from decades of experiencing the cycles of life is that how we view the world is all in our minds. If we think we “can’t”….then we “can’t”. If we only “try”, then we are not fully committed and leave the door open for failure. Our lives become what we truly believe, and what we believe creates our lives.  I believe….. first in my marriage, and have the best marriage of anyone I know. I believe…. in myself (sometimes less than perfect,) and have developed strong self confidence. Self confidence attracts others… it’s contagious. We all need cooperation…..even the love of others, to achieve our aspi