Find the installed SQL Server’s CD KEY from the Windows Registry using T-SQL

July 12, 2013 § 6 Comments


Here is the way to find SQL Server CD key, which was used to install it on machine. If user do not have permission on the SP, please login using SA username. Expended stored procedure xp_regread can read any registry values.

Compatible with Windows XP AND SQL 2005 Version:

USE master GO

EXEC xp_regread

   ‘HKEY_LOCAL_MACHINE‘,

   ‘SOFTWARE\Microsoft\Microsoft SQL Server\80\Registration',

   'CD_KEY'

GO

Compatible with SQL 2008 or Later:

USE master GO

EXEC xp_regread

   NHKEY_LOCAL_MACHINE‘,

   ‘SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\Setup',

   N'ProductCode'

GO

Detail:

  1. The first parameter specifies the root key in the registry.
  2. The second parameter is the path to the key.
  3. The third parameter specifies the key value that you’re looking for.

Note that you should be wary of using xp_regread in an application because it’s an undocumented function that Microsoft can remove without warning. However, using the extended stored procedure for ad-hoc access is perfectly safe.

Also download the t-sql word file!

Happy Coding 🙂

Tagged: ,

§ 6 Responses to Find the installed SQL Server’s CD KEY from the Windows Registry using T-SQL

  • Nice Article …
    Keep posting dear Gaurav sir!

    Mohammad Shahnawaz
    Oracle’s Passion

  • Dear Gaurav sir ,

    i have one query, kindly correct it.

    select convert(nvarchar(10),warnt.created_dt,120) ‘Date’,loc.loc_name ‘Location’,–loctype.LocationTypeDesc,
    (select SUM(stk.sel_price) from tbl_mst_stock as stk
    inner join tbl_gen_warranty as wrn on stk.Stock_ID=wrn.Stock_Id
    inner join tbl_mst_model as mdl on mdl.Model_ID=wrn.model_id

    where wrn.mobile_type like ‘SL’)’Selleing Price’,
    (Select COUNT(stk.stk_code) from tbl_mst_stock as stk
    inner join tbl_gen_warranty as warn on stk.Stock_ID=warn.Stock_Id
    inner join tbl_mst_model mdl on mdl.Model_ID=warn.model_id

    where warn.mobile_type like ‘SL’) ‘Stock count’
    From tbl_mst_location as loc
    inner join tbl_mst_location_types loctype on loc.LocationTypeID=loctype.LocationTypeID
    inner join tbl_gen_warranty as warnt on loc.Location_ID=warnt.Location_id
    Where loctype.LocationTypeID=1 and loctype.group_id=1
    and (CONVERT(nvarchar(10),warnt.created_dt,120)
    Between CONVERT(nvarchar(10),’2013-01-01′,120) and convert(nvarchar(10),’2013-07-09′))
    Group by loc.loc_name,loctype.LocationTypeDesc,warnt.created_dt,loctype.LocationTypeID


    output of this is here under

    Date Location Selleing Price Stock count
    2013-01-07 AIRPORT 22935250.000 91744
    2013-02-27 AIRPORT 22935250.000 91744
    2013-02-27 AIRPORT 22935250.000 91744
    2013-02-27 AIRPORT 22935250.000 91744
    2013-03-04 AIRPORT 22935250.000 91744
    2013-06-07 KUWAIT CITY 22935250.000 91744
    2013-06-09 KUWAIT CITY 22935250.000 91744
    2013-01-06 AIRPORT 22935250.000 91744
    2013-06-09 KUWAIT CITY 22935250.000 91744


    Here it is doing some for to the all records presented in the tbl_mst_warranty table but i need according to the location.

    Thanking you

    Mohammad Shahnawaz
    Oracle’s Passion

    • Gaurav Lal says:

      hi Shahnawaz,

      sorry for the late response.

      Can you please share me the abopve query alongwith the proper table structure and some dummy data… so that I can try your query…

      Also write your expected output…. You can email me at gaurava16fc@gmail.com

      Thanks,
      Gaurav

  • Nick Xu says:

    Thanks for posting this, very useful script!

  • rko281 says:

    I got the following after running this:

    Msg 102, Level 15, State 1, Line 2
    Incorrect syntax near ‘GO’.

  • Gaurav Lal says:

    Hello rko281,

    You are facing this error just because of Formatting issue on my article. I have added the t-sql script in a word document, which is downloadable now from this post itself.

    Please try this and revert back if you still find any issue!

    Regards,
    Gaurav

Leave a comment

What’s this?

You are currently reading Find the installed SQL Server’s CD KEY from the Windows Registry using T-SQL at Gaurav Lal.

meta