I've used the COLUMNPROPERTY to Identify tables with Identity columns.
SELECT Distinct Table_Name,MAX(COLUMNPROPERTY (OBJECT_ID(Table_Name),Column_Name,'IsIdentity')) as Id FROM INFORMATION_SCHEMA.COLUMNS (NOLOCK)GROUP BY Table_Name
Microsoft recommends that you use the Information Schema views instead of the system tables when possible. To quote SQL Server 2000 Books Online: "To obtain meta data, use system stored procedures, system functions, or these system-supplied [Information Schema] views only. Querying the system tables directly may not provide accurate information if system tables are changed in future releases."
Also, I noticed that schema (object ownership) was not addressed and if a table exists with multiple owners (Bob.MyTable, Jane.MyTable, dbo.MyTable) then the IDENT_CURRENT results are inaccurate. In order to utilize the Information Schema views and account for the multiple table ownership, you need to query as follows:
SELECT T.TABLE_SCHEMA, T.TABLE_NAME, C.COLUMN_NAME, C.DATA_TYPE, IDENT_CURRENT(T.TABLE_SCHEMA + '.' + T.TABLE_NAME) [CURRENT_IDENTITY_VALUE]FROM INFORMATION_SCHEMA.TABLES AS T (NOLOCK) INNER JOIN (SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS (NOLOCK) GROUP BY TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE HAVING MAX(COLUMNPROPERTY (OBJECT_ID(Table_Name),Column_Name,'IsIdentity')) = 1) AS C ON C.TABLE_SCHEMA = T.TABLE_SCHEMA AND C.TABLE_NAME = T.TABLE_NAME
Your comments (and everyone else who expressed similar comments) about this not being an Identity-specific problem are absolutely correct. I focused on the Identity columns in particular because I found it interesting that there were multiple ways of getting at the same piece of metadata. And now, with some of the other responses, I have a couple more ways to find Identities.
It is also correct to say that a comprehensive look at the problem of inappropriate datatype choices is beyond the scope of the article's conclusion. I can see that perhaps I should have been more general in that respect, but, hey, this is my first crack at a tech article.
So I'm interested to further discuss anyone's ideas about how this could be generalized a bit. Sticking with integers only, what would you do if you were a consultant brought in to "check the health" of an enterprise system? How would we do a comprehensive check of integer values accross all tables? How would we focus in on those tables most likely to cause problems? Is it something that could be done in a single script, or would we need to do some kind of script->code generation->script solution?
I'll give this some thought and post ideas later this evening. I look forward to seeing what others come up with, too!
Excellent! Now I know how to find that kind field quickly with 3 or more options.
In my mind, I hate to use identity 1. you must have other alternative key to ensure the uniquness of the row, otherwise, you could have a million same contents records except the identity column. 2. however, it might be useful in a situation that there are 3 or more concatenated pk. then, an identity field is ok. In summary, my max idenetity column is 34 million, still have room to grow. but to change 2300 databases from int to bigint to stop the 7days operation might be a big issue. In addition, the FK is also a issue.
Good job, Troy Ketsdever
I used David Bird's code and modified a bit as follows and am happy with it.
SELECT table_name, column_name, ordinal_position orgPostion, data_type FROM INFORMATION_SCHEMA.COLUMNS (NOLOCK) where COLUMNPROPERTY (OBJECT_ID(Table_Name),Column_Name,'IsIdentity') = 1
The author mentioned "the table should now have no problems for the next 6 to 9 Billion years"
if the usage of the table was to remain the same would the following not be true?
max value for signed int = ((2 ^ 32) / 2) - 1 = 2147483647
rows per year = (rowcount / monthsInOperation) * 12 = (2147483647 / 8) * 12 = 3221225471
-> rowcount chosen as max value of signed int cause it was the datatype to overflow and monthsInOperation from artical
max value of signed bigint = ((2 ^ 64) / 2) - 1 = 9.22337E+18
number of years = max value of signed bigint / rows per year = 9.22337E+18 / 3221225471 = 2863311532
a mere 2.8 Billion years (yea I know someone else will have my job by then), somewhat less than the 6 to 9 that the author claimed
If we are talking about understanding the demands on our data types and choosing them wisely it seems such calculations should be done. I can imagine the designer thinking int would be more than enough, don't do the same with again by not taking into account such simple calculations.
I think you're right... the 6-9 billion may have stuck in my head because the team was joking about how maybe we should seed the value at -2^63 to get double the years. All I know is that I'm glad I'm not going to be the one responsible for converting the column to a hugeint when the time comes!
hmm, more than 1?
My point was meant to be that the problem initiated from bad design and planning (particularly relating to the maximum size of the chosen data type and the amount of rows produced), such assumptions probably caused the problem in the first place.