|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 12:29 PM
Points: 182,
Visits: 952
|
|
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
David Bird
My PC Quick Reference Guide
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Sunday, May 15, 2011 7:52 PM
Points: 40,
Visits: 48
|
|
Hear! Hear! 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 Later, Peter
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 12:37 PM
Points: 1,203,
Visits: 683
|
|
Hi Malcolm; 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! TroyK
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 12:22 PM
Points: 192,
Visits: 111
|
|
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 -D
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 3:20 PM
Points: 1,137,
Visits: 667
|
|
Vic,
Using max(colval) + 1 is probably not a good idea. If two sessions hit it simultaneously, you're going to get a collision unless you serialize access. So you need to choose between scalability and either duplicate rows or constraint violations. That's one of the main benefits that IDENTITY provides.
-- Adam Machanic SQL Server MVP SQLblog.com: THE SQL Server Blog Spot on the Web
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 12:22 PM
Points: 192,
Visits: 111
|
|
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
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, May 31, 2007 6:30 PM
Points: 16,
Visits: 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.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 3:20 PM
Points: 1,137,
Visits: 667
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 12:37 PM
Points: 1,203,
Visits: 683
|
|
dMacey; 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! TroyK
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Thursday, May 31, 2007 6:30 PM
Points: 16,
Visits: 1
|
|
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.
|
|
|
|