Thursday, November 30, 2006

SQL Server: How to rename an index

When you create a table with a primary key in SQL Server, SQL Server creates an index for you with an automatically generated name, like PK__acctreg__29819341. I find the naming convention they use to be annoying, so I wanted to be able to change the name. Here's how:




-- to find the names of your current indexes
use my_database
select tab.name table_name, idx.name index_name
from sysobjects tab
left join sysindexes idx on idx.id = tab.id
where tab.xtype = 'U'
order by 1

-- to rename an existing index
exec sp_rename 'company.PK__company__1ED599B2', 'company$companyID', 'INDEX'

Monday, November 6, 2006

SQL Server: What service pack am I on?

Anyone remember the SQL Slammer virus? It turns out that it attacked SQL Server databases (and SQL Server DBA's) that were not protected by the latest Service Pack. I sit around and scare myself by asking myself questions like, what service pack am I on?

Here is a simple query to tell you what service pack you are on and some other interesting information about your SQL Server instance:




select serverproperty ('servername') [Server Name],
serverproperty ('productversion') [Product Version],
serverproperty ('productlevel') [Product Level],
serverproperty ('edition') Edition,
serverproperty ('licensetype') [License Type],
serverproperty ('numlicenses') [# Licenses]


Of course you have to connect to the server you are interested in before running this query. The Service Pack level will be in the "Product Level" column.