Search This Blog

Wednesday, July 25, 2007

Keep a check on your IDENTITY columns in SQL Server

 The IDENTITY columns, or 'auto number' columns as some people call them, are auto incrementing columns provided by SQL Server. There can only be one IDENTITY column per table. You just have to provide a base value, and an increment value, and SQL Server will take care of incrementing this column automatically. Some people like these, and some don't, but the truth is, IDENTITY columns are gaining popularity, and many production systems, including critical ones are using IDENTITY columns these days. So, it is important to keep an eye on these columns, to make sure they are not reaching the limit of their base data type. For example, if you created an IDENTITY column of smallint datatype, its values can go upto 32767. If you try to insert anymore rows, you will get the following error:

Server: Msg 8115, Level 16, State 1, Line 1
Arithmetic overflow error converting IDENTITY to data type smallint.
Arithmetic overflow occurred.

If this table happens to be a part of a critical production system, then you are in trouble. You will have to do something about it to resolve it. If the data from this table can be deleted, then delete the data using TRUNCATE TABLE command. TRUNCATE TABLE resets the IDENTITY column to its base value. The DELETE command doesn't do this. But then, if this table is referenced by a foreign key, then TRUNCATE TABLE is not allowed on this table. Your other option is to run DBCC CHECKIDENT on your table with RESEED option.

An IDENTITY column of tinyint datatype can go upto 255, smallint can go upto 32767, int can go upto 2147483647 and bigint can go upto 9223372036854775807.

You can proactively monitor these IDENTITY columns, to avoid getting into such problems. If you can see in advance, that an IDENTITY column is reaching its limit, then you could do something about it, before it reaches the limit. The other day, one of my friends was trying to automate a process that checks all the IDENTITY columns in a database and reports on how far away those columns are from the limit. He was using a cursor to go through all the tables in the database, and running a "SELECT MAX(IdentityCol) FROM TableName" on all the tables that have an IDENTITY column. It would take ages to run on a database with many big tables. It can be simplified into one simple query using IDENT_CURRENT function. That's what I did, and thought it will be useful for other DBAs as well. So, here I am writing about it.

There are three different versions of this procedure. First one is for SQL Server 2005. The second and third versions work in SQL Server 2000. You will have to create this procedure in the database of your interest. And run it as shown below:

EXEC dbo.CheckIdentities
GO

This procedure below, displays information about all IDENTITY columns in the database, and shows you the percentage of IDENTITY values already used. If you are seeing any IDENTITY columns that have used up 80% or more values, then you need to start thinking about it. You could customise this procedure to automatically email you or log an error if there are any IDENTITY columns that are nearing the limit. You could also schedule this procedure as an SQL Agent job, so that it checks these columns regularly. Any new IDENTITY columns added to the database will automatically get picked up by this query.

A quick note about 64 bit SQL Server. Even though SQL Server 64 bit editions can access a lot more memory inherently, than the 32 bit systems could - the IDENTITY columns are still limited to the limits imposed by the base datatypes. I'm writing this because, someone recently asked me if int data type can store a higher number in 64 bit server, compared to a 32 bit server.

/* The SQL Server 2005 version of the stored procedure. It uses new catalog views */


CREATE PROC dbo.CheckIdentities
AS
BEGIN
SET NOCOUNT ON

SELECT QUOTENAME(SCHEMA_NAME(t.schema_id)) + '.' + QUOTENAME(t.name) AS TableName,
c.name AS ColumnName,
CASE c.system_type_id
WHEN 127 THEN 'bigint'
WHEN 56 THEN 'int'
WHEN 52 THEN 'smallint'
WHEN 48 THEN 'tinyint'
END AS 'DataType',
IDENT_CURRENT(SCHEMA_NAME(t.schema_id) + '.' + t.name) AS CurrentIdentityValue,
CASE c.system_type_id
WHEN 127 THEN (IDENT_CURRENT(SCHEMA_NAME(t.schema_id) + '.' + t.name) * 100.) / 9223372036854775807
WHEN 56 THEN (IDENT_CURRENT(SCHEMA_NAME(t.schema_id) + '.' + t.name) * 100.) / 2147483647
WHEN 52 THEN (IDENT_CURRENT(SCHEMA_NAME(t.schema_id) + '.' + t.name) * 100.) / 32767
WHEN 48 THEN (IDENT_CURRENT(SCHEMA_NAME(t.schema_id) + '.' + t.name) * 100.) / 255
END AS 'PercentageUsed'
FROM sys.columns AS c
INNER JOIN
sys.tables AS t
ON t.[object_id] = c.[object_id]
WHERE c.is_identity = 1
ORDER BY PercentageUsed DESC
END



If you try to create the above stored procedure in SQL Server 2000, you will get the following error:

Server: Msg 195, Level 15, State 10, Procedure a, Line 4 'SCHEMA_NAME' is not a recognized function name. So, here are some SQL Server 2000 compatible versions. /* The SQL Server 2000 version of the stored procedure. Uses system tables. This should work in SQL Server 7.0 too */




CREATE PROC dbo.CheckIdentities
AS
BEGIN
SET NOCOUNT ON

SELECT QUOTENAME(USER_NAME(t.uid))+ '.' + QUOTENAME(t.name) AS TableName,
c.name AS ColumnName,
CASE c.xtype
WHEN 127 THEN 'bigint'
WHEN 56 THEN 'int'
WHEN 52 THEN 'smallint'
WHEN 48 THEN 'tinyint'
END AS 'DataType',
IDENT_CURRENT(USER_NAME(t.uid) + '.' + t.name) AS CurrentIdentityValue,
CASE c.xtype
WHEN 127 THEN (IDENT_CURRENT(USER_NAME(t.uid) + '.' + t.name) * 100.) / 9223372036854775807
WHEN 56 THEN (IDENT_CURRENT(USER_NAME(t.uid) + '.' + t.name) * 100.) / 2147483647
WHEN 52 THEN (IDENT_CURRENT(USER_NAME(t.uid) + '.' + t.name) * 100.) / 32767
WHEN 48 THEN (IDENT_CURRENT(USER_NAME(t.uid) + '.' + t.name) * 100.) / 255
END AS 'PercentageUsed'
FROM syscolumns AS c
INNER JOIN
sysobjects AS t
ON t.id = c.id
WHERE COLUMNPROPERTY(t.id, c.name, 'isIdentity') = 1
AND OBJECTPROPERTY(t.id, 'isTable') = 1
ORDER BY PercentageUsed DESC
END


/* The SQL Server 2000 version of the stored procedure. Uses INFORMATION_SCHEMA views. */


CREATE PROC dbo.CheckIdentities
AS
BEGIN
SET NOCOUNT ON

SELECT QUOTENAME(t.TABLE_SCHEMA) + '.' + QUOTENAME(t.TABLE_NAME) AS TableName,
c.COLUMN_NAME AS ColumnName,
c.DATA_TYPE AS 'DataType',
IDENT_CURRENT(t.TABLE_SCHEMA + '.' + t.TABLE_NAME) AS CurrentIdentityValue,
CASE c.DATA_TYPE
WHEN 'bigint' THEN (IDENT_CURRENT(t.TABLE_SCHEMA + '.' + t.TABLE_NAME) * 100.) / 9223372036854775807
WHEN 'int' THEN (IDENT_CURRENT(t.TABLE_SCHEMA + '.' + t.TABLE_NAME) * 100.) / 2147483647
WHEN 'smallint' THEN (IDENT_CURRENT(t.TABLE_SCHEMA + '.' + t.TABLE_NAME) * 100.) / 32767
WHEN 'tinyint' THEN (IDENT_CURRENT(t.TABLE_SCHEMA + '.' + t.TABLE_NAME) * 100.) / 255
END AS 'PercentageUsed'
FROM INFORMATION_SCHEMA.COLUMNS AS c
INNER JOIN
INFORMATION_SCHEMA.TABLES AS t
ON c.TABLE_SCHEMA = t.TABLE_SCHEMA AND c.TABLE_NAME = t.TABLE_NAME
WHERE COLUMNPROPERTY(OBJECT_ID(t.TABLE_SCHEMA + '.' + t.TABLE_NAME), c.COLUMN_NAME, 'isIdentity') = 1
AND c.DATA_TYPE IN ('bigint', 'int', 'smallint', 'tinyint')
AND t.TABLE_TYPE = 'BASE TABLE'
ORDER BY PercentageUsed DESC
END





You could read the original article from the following link: http://vyaskn.tripod.com/sql_server_check_identity_columns.htm

Friday, June 15, 2007

Solution for "UNICODE ASP files are not supported" error in .Net

Error – ASP.Net
Active Server Pages error 'ASP 0239'
Cannot process file
/GR/submit_page.asp, line 1
UNICODE ASP files are not supported


Solution
With your project open, click on File, Advanced Save Options. Select US-ASCII.
Then click to apply to ALL DOCUMENTS. 
It will give you a warning that some characters may not play, but you can ignore that.
From that point forward, all saves should be in ASCII and your pages will play.

Provided by Ravichander Kannan, GTS, CSC India


Please visit my other blogs too: http://edwardanil.blogspot.com for information and http://netsell.blogspot.com for internet marketing. Thanks !!

Monday, February 12, 2007

SQL Stored Procedure to Log Updates, Independent of Database

Have you ever needed a quick, retroactive solution to track changes on your database?
Well, if youre a good system designer you wouldn't, but when contracting for clients you may find often that the characterization of the project is ever changing, and there is only so much you are able to foresee.

When working with multiple clients, with only a certain amount of hours dedicated to each project, you may want your patches to be applicable for multiple projects. The following is such a patch. It functions to track updates done through the website, storing information such as the user information, the page on which the action took place, the old value and the new value of the changed information and a date time stamp.

Please check out this article to get the code on how to develop the SQL Stored Procedure for creating a log on all updates.

Please visit my other blogs too: http://edwardanil.blogspot.com for information and http://netsell.blogspot.com for internet marketing. Thanks !!

Saturday, December 16, 2006

Free Web Hosting

Can you really get FREE web hosting?

Yes, there are hundreds of free hosting web sites, as far as not having to pay any money to have your website hosted. Generally they either cost you in time, web hosting restrictions, or modifying your free web pages by adding pop ups, banners, or other adverts. When looking for free web hosting (especially on search engines), you should beware that there are also a large number of commercial web hosts that claim to offer free hosting services, but those often have a catch, such as paying an excessive amount for a domain name or other service, and therefore aren't really free. The free free hosting guide below will give you some tips for finding the right free web hosting company for you.

How do the free web hosts make money?

The free website hosts often make money in other ways, such as putting banners, pop ups, or pop unders ads on your free web pages. Some free web hosting companies do not put ads on your site, but require you as the webmaster to click on banners in their control panel or sign up process, or just display banners in the file manager in hopes you will click them. Some lure visitors with free hosting in hopes you will upgrade and pay for advanced features. A few send you occasional emails with ads, or may even sell your email address. A new method that is becoming popular is requiring a certain number of "quality" forum posting, usually as a means of getting free content for them and thereby being able to display more ads to their website visitors.

Are free web hosts reliable?

Generally no, although there are a few exceptions. If the free host is making money from banner ads or other revenue sources directly from the free hosting service, then they likely will stay in business, provided someone doesn't abuse their web hosting server with spam, hacking, etc., as often happens to new free web hosting companies with liberal signup policies. If the freehost accepts just anyone, especially with an automated instant activation and it offers features such as PHP or CGI, then some users invariably try to find ways to abuse it, which can cause the free server to have a lot of downtime or the free web server to be slow. It is best if you choose a very selective free hoster which only accepts quality sites (assuming you have one).

Uses for free webspace

Free web hosting is not recommended for businesses unless you can get domain hosting from an ad-free host that is very selective. Other reasons for using free hosting websites would be to learn the basics of website hosting, have a personal website with pictures of your family or whatever, a doorway page to another web site of yours, or to try scripts you have developed on different web hosting environments.

How to find the right free web hosting site

The best place to search for free web hosting is on a free webspace directory website (i.e. a web site which specializes in listing only free web hosting providers). There are some which add new free hosts pretty much every week (and if it is updated often, has usually had to delete about as many). There are also many which almost never update their web site, and a huge percent of their links and info are outdated. Unfortunately that includes most of the directories that were the best several years ago. The problem is free hosts change so often, and most fold up in less than a year (often even after only a day or two), that it is hard to keep such a free hosting directory up-to-date.

For a more selective list of the best free hosts, there are also these free webspace hosting directories:
Free WebHosts (http://www.free-webhosts.com)
Best Free Webspace (http://www.100-Best-Free-Webspace.com/)
Free Hosting (http://www.Absolutely-Free-Hosting.com/)
Free Webspace (http://www.free-webspace.org/)
Other (usually less useful) resources include subcategories of freebies sites, search engines and directories, and forums. Your ISP might also supply you with free webhosting.

Hints for finding the best free web hosting service

Generally it is best not to choose a free hosting package with more features than you need, and also check to see if the company somehow receives revenue from the free hosting itself to keep it in business. As already mentioned, it is best to try to get accepted to a more selective free host if possible. Look at other sites hosted there to see what kind of ads are on your site, and the server speed (keep in mind newer hosts will be faster at first). Read the Terms of Service (TOS) and host features to make sure it has enough bandwidth for your site, large webspace and file size limit, and any scripting options you might need. Read free webspace reviews and ratings by other users on free hosting directories. If you don't have your own domain name, you might want to use a free URL forwarding service so you can change your site's host if needed.

Recommended free web hosts

It would be awfully hard to recommend any free web space host and someone not like it, as different people need different web hosting features and have different priorities, and the webhosting quality may change over time. Also some people want free domain hosting (you own the domain), and others might not be able to buy a domain name. Here are some of the most recommended free web hosts, and their main features.

You could read the original article in this link: http://www.free-webhosts.com/free-webhosting-article.php

More Lists of Free Web Hosts

Please visit my other blogs too: http://edwardanil.blogspot.com for information and http://netsell.blogspot.com for internet marketing. Thanks !!

Thursday, December 07, 2006

Manipulating Data in TEXT Type Columns

Its always tricky to do string manipulation in TEXT datatype fields.

For many SQL Server 2000 DBAs working with text columns in T-SQL is no different than any other datatype. But there are some tricks when you work with very large values that you need to know. Leo Peysakhovich brings us some advanced queries that you might need if you work with large XML documents as he does.

Please visit this link to know more about how to manipulate data in Text type columns.


Please visit my other blogs too: http://edwardanil.blogspot.com for information and http://netsell.blogspot.com for internet marketing. Thanks !!