Tuesday, 8 December 2009

Reseed an SQL Table Identity Column

If you get an unexpected error like this...

Violation of PRIMARY KEY constraint 'PK_Name'. Cannot insert duplicate key in object 'Table_Name'.

You look at your table and the primary key and realise that the primary key that was complianing was a indentity column then you think well isn't that a bit odd. The point of an identity column is that you insert new rows in a database and it generates a primarykey value for you, usually a unqiue integer value.

Sometimes though for some mystic unknown reason (one that I can't be bothered to find out the real cause for) the identity value that is created is invalid. So whats the solution?


You can use this SQL command to check what the current status of an identity column on a table is. If you run the below command, it will tell you the current identify value, i.e. the next value that would be generated as a unquire identity value, and it will tell you the current column value, ie. the current highest unqiue identity value used in your database.

Now if it returns something like this then you may have a problem.

Checking identity information: current identity value '10', current column value '17'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

If the current column value is higher than the current identity value then the problem may be that you are trying to insert a new row and the identity value being inserted automatically is '10' when the rows have already been created for identity values up to '17'.

To fix this just run this command to reset the identity value.

Then run the previous command again and you should see this result.

Checking identity information: current identity value '17', current column value '17'.
DBCC execution completed. If DBCC printed error messages, contact your system administrator.


Tuesday, 19 May 2009

Make ‘Folders View’ the Windows Explorer Default in Windows XP

Here is a link to a very handy blog post on how to make folders view in the explorer bar, in windows explorer default to folders view, instead of that silly blue common tasks bar.



Thursday, 7 May 2009

How to create your own Code Signing Certificates

This has been a real pain for me recently. At work we had previously got code signing certificates from a certificate authority called Thawte. But when it came time to renew, they have been totally unhelpful, slow and inept. We have spent a month dealing with them trying to get a renewed certificated with no joy, so they can now go shove a banana because we are going to sort out our own certificates.

There is a great step by step guide in this forum discussion on channel9 that will help you set up your own code signing certificate.


It's a fiddly process but worth it if all you need is a certificate to sign your code for use within your own network.

Wednesday, 29 April 2009

Using SyntaxHighlighter to display code snippets

Today I found a very handy javascript to display code snippets in web pages, and it can be adapted to work in Blogs as well.

The SyntaxHighlighter is very clever, and can display a lot of different code types, for example C#.

//Here is some C# Code
public void Method()
string test = "display some code in my blog";
test = test.tostring() + "and format it nicely";

There is a nice blog post by Arun K Viswanathan on how to easily adapt the code to work in Blogger. Check it out.

Thursday, 22 January 2009

Drop Constraint in MS SQL that has an unknown name

Now when I say unknown name I catually mean I know constraint I want to drop, however the name of the constraint was automatically generated by SQL Server and that name contains a random number e.g.


Well I could just write a script like this to get rid of it:

alter table [user]
drop constraint [DF__user__15502E78]

Fine that works, but what if I have created multiple version of that DB on different servers, each time SQL Server creates that constraint for me automatically it will contain a different random number in the name. So now when I run the script I worte above on a different server it fails.

So I wrote this little script below, that will search out the constraint and drop it for me. Now you will have to tweak this to get it to work for you as you may have more constraints as I ad in my DB, so you will need to alter the WHERE clause a bit to just get back the constraint name you are looking for.

-- Because this constraint is dyncamically created by SQL Server
-- the name of the constraint contains a random number
-- so the name will be different from DB to DB.
-- So the only way to drop the constraint is to use the script below.
DECLARE @constraint varchar(100)
SET @constraint = (SELECT OBJECT_NAME(OBJECT_ID) --AS NameofConstraint,
--SCHEMA_NAME(schema_id) AS SchemaName,
--OBJECT_NAME(parent_object_id) AS TableName,
--type_desc AS ConstraintType
FROM sys.objects
AND OBJECT_NAME(parent_object_id) = 'user'

print @constraint

declare @sqlscript varchar(200)

set @sqlscript = 'alter table [user] drop constraint ' + @constraint

print @sqlscript


If you look at the select clause in the script above that is what i'm using to return the name of the constraint, you can uncomment the values in that select statement and just run and tweak that to get it just right for you and then insert it back into the rest of the script and your all sorted.