Tag: Script

  • Script to determine identity table and identity column

    Script to determine the identity tables and columns in the databases.

    USE <DBNAME>
    --script to determine the table with the identity column
    select name, OBJECTPROPERTY(id, 'TableHasIdentity') AS TableHasIdentityCol
    from sysobjects
    where xtype = 'U'
    
    --script to determine the table and columne with the identity on
    SELECT OBJECT_NAME(id) as TblName, name as ColName
    FROM syscolumns
    WHERE status = 0x80
    
    --script to determine the table and columne with the identity on using ColumnProperty
    SELECT OBJECT_NAME(id) as TblName, name as ColName
    FROM syscolumns
    WHERE COLUMNPROPERTY(id, name, 'IsIdentity') = 1
    
  • Setting the PowerShell Execution Policy

    Problem
    Recently I moved PowerShell script files to a production environment and when executing it from the command prompt, I got this error: “File cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details”. In this tip we cover what needs to be done to resolve this issue.

    Solution
    http://www.mssqltips.com/sqlservertip/2702/setting-the-powershell-execution-policy/

  • Using PowerShell to Register All SQL Instances Into Central Management Server

    Problem

    Managing multiple SQL Servers has its challenges. One of the big challenges is having to collect data from each of the servers you manage and figure out which servers need attention. SQL Server has introduced a new feature called Central Management repository which can be used to manage multiple instances. One of the challenges to using CMS is that you have to register all the SQL Servers manually into CMS.  The below article will guide you on how to register hundreds of servers in a second for SQL Server CMS.

    Solution

    http://www.mssqltips.com/sqlservertip/2658/using-powershell-to-register-all-sql-instances-into-central-management-server/

  • Steps to create the deadlock scenario

    A deadlock occurs when two or more processes permanently block each other by each process having a lock on a resource which the other process are trying to lock.

    Please execute the below queries as per the mentioned comments to produce a deadlock.

    --turning on the traceflag to record deadlock info into error log
    dbcc traceon(1204,-1)
    dbcc tracestatus(1204)
    
    --creating test database
    create database sqlDBPool
    --Connecting to SQLDBPool database
    use sqldbpool
    --table creation
    create table tb1 (col1 int)
    create table tb2 (col1 int)
    --inserting dummy records
    insert into tb1 values(1),(2),(3)
    insert into tb2 values(1),(2),(3)
    
    --Open first connection to update table explicit transaction
    begin transaction
      update tb1 set col1 = 5
      
    --Open second connection to update table explicit transaction
    use sqlDBPool
    begin transaction
      update tb2 set col1 = 6
      update tb1 set col1 = 6
    
    --Open first connection to update table explicit transaction
      update tb2 set col1 = 5
    

    You can see the one of the transaction will fail with the below error message.

    Msg 1205, Level 13, State 45, Line 3
    Transaction (Process ID 55) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

    As we have turned on the deadlock trace flag, you can see the below information in the SQL Server error log.

    Starting up database 'sqlDBPool'.
    Deadlock encountered .... Printing deadlock information
    Wait-for graph
    NULL
    Node:1  
    RID: 9:1:153:0                 CleanCnt:2 Mode:X Flags: 0x3
     Grant List 1:
       Owner:0x05684480 Mode: X        Flg:0x40 Ref:0 Life:02000000 SPID:52 ECID:0 XactLockInfo: 0x065F82A8
       SPID: 52 ECID: 0 Statement Type: UPDATE Line #: 1
       Input Buf: Language Event: update tb2 set col1 = 5
    Requested by: 
      ResType:LockOwner Stype:'OR'Xdes:0x05A8CC10 Mode: U SPID:55 BatchID:0 ECID:0 TaskProxy:(0x05A70354) Value:0x6767b20 Cost:(0/432)
    NULL
    Node:2  
    RID: 9:1:155:0                 CleanCnt:2 Mode:X Flags: 0x3
     Grant List 2:
       Owner:0x067679A0 Mode: X        Flg:0x40 Ref:0 Life:02000000 SPID:55 ECID:0 XactLockInfo: 0x05A8CC38
       SPID: 55 ECID: 0 Statement Type: UPDATE Line #: 3
       Input Buf: Language Event: begin transaction    update tb2 set col1 = 6    update tb1 set col1 = 6
    Requested by: 
      ResType:LockOwner Stype:'OR'Xdes:0x065F8280 Mode: U SPID:52 BatchID:0 ECID:0 TaskProxy:(0x0941A354) Value:0x6a943a0 Cost:(0/432)
    NULL
    Victim Resource Owner:
     ResType:LockOwner Stype:'OR'Xdes:0x05A8CC10 Mode: U SPID:55 BatchID:0 ECID:0 TaskProxy:(0x05A70354) Value:0x6767b20 Cost:(0/432)  
    

  • How to take database out of emergency mode?

    In SQL Server 2000

    sp_configure ‘allow’ ,1
    GO
    Reconfigure with override
    GO
    Update sysdatabases set status = status&(~32768) where name = ‘SQLDBPool’
    GO
    sp_configure ‘allow’, 0
    GO
    Reconfigure with override
    go

    IN SQL Server 2005/2008

    ALTER DATABASE sqldbpool
    SET online