Saturday 11 June 2011

Get or set shortdate format from regional setting in .net

Public regKey As Microsoft.Win32.RegistryKey
regKey = My.Computer.Registry.CurrentUser.OpenSubKey("Control Panel\International", True)
 If regKey.GetValue("sShortDate") = "dd/MM/yyyy" Then 
Else
   SetRegionalSetting("dd/MM/yyyy") 
End If

Friday 10 June 2011

Enable disable all Trigger In One Query

sp_MSforeachtable "ALTER TABLE ? DISABLE TRIGGER ALL"
GO
sp_MSforeachtable "ALTER TABLE ? ENABLE TRIGGER ALL"

Thursday 19 May 2011

Change Collation Of Database

USE [master]

ALTER DATABASE [DbName]  SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO

GO
ALTER DATABASE [DbName] COLLATE SQL_Latin1_General_CP1_CI_AS
GO
ALTER DATABASE [DbName] SET RECOVERY FULL WITH NO_WAIT
GO

Go
ALTER DATABASE [DbName]  SET MULTI_USER 
GO

Sql X Types

C = CHECK constraint
D = Default or DEFAULT constraint
F = FOREIGN KEY constraint
FN = Scalar function
IF = Inlined table-function
K = PRIMARY KEY or UNIQUE constraint
L = Log
P = Stored procedure
R = Rule
RF = Replication filter stored procedure
S = System table
TF = Table function
TR = Trigger
U = User table
V = View
X = Extended stored procedure

Comparing Tables For Difference Version

Create table #nik
(
    table_Name Varchar(300),
    Column_Name Varchar(300)
)
Create Table #Temp 
(
    Table_Name Varchar(300)
) 

Insert Into #nik Select TABLE_NAME ,Column_Name From dbName.Information_Schema.columns 
Where Column_Name Not IN
(Select Column_Name From dbName2.Information_Schema.columns) 
Select * From #nik 

Select * From #nik  Where table_Name Not in (Select table_Name from #Temp)
Insert Into #Temp Values('Table')
Select * From #Temp 

Tuesday 3 May 2011

Rename Computer Name When Sql Server Already Installed

To rename a computer that hosts a stand-alone instance of SQL Server
•    For a renamed computer that hosts a default instance of SQL Server, run the following procedures:

sp_dropserver <old_name>
GO
sp_addserver <new_name>, local
GO
Restart the instance of SQL Server.
•    For a renamed computer that hosts a named instance of SQL Server, run the following procedures:

sp_dropserver <'old_name\instancename'>
GO
sp_addserver <'new_name\instancename'>, local
GO
Restart the instance of SQL Server.
 After the Renaming Operation
________________________________________
After a computer has been renamed, any connections that used the old computer name must connect by using the new name.
To verify that the renaming operation has completed successfully
•    Select information from either @@SERVERNAME or sys.servers. The @@SERVERNAME function will return the new name, and the sys.servers table will show the new name. The following example shows the use of @@SERVERNAME.

SELECT @@SERVERNAME AS 'Server Name'

Thursday 17 March 2011

Kill Sleeping Connection in Sql

Create PROCEDURE [dbo].[SleepedConnKill] 
AS
 SET NOCOUNT ON;  
    Declare @StrKill Varchar(30)         
    Declare @SPID_Cur Numeric(18,0)
   
   Declare SysProcess_Cursor Cursor LOCAL  For
     Select SPID From sys.sysprocesses  Where SPID>50 And  Status='sleeping' And  DateDiff(MINUTE ,Last_Batch,GetDate())>5 
   Open SysProcess_Cursor
   Fetch Next From SysProcess_Cursor Into @SPID_Cur
   While @@Fetch_Status = 0                    
   Begin   
    
     Set @StrKill = 'KILL '+ convert(varchar(10),@SPID_Cur)            
     EXEC(@StrKill)    
    
   Fetch Next From SysProcess_Cursor Into @SPID_Cur
   End    
   Close SysProcess_Cursor
   Deallocate SysProcess_Cursor
    Return