sqlschool.gr logo

articles

Articles of SQLschool.gr Team

Searching for tables with an identity column in a database

Antonios Chatzipavlis
Wednesday 03 September 2014

Όταν είσαι σε open space office φτάνουν στα αυτιά σου διάφορα. Έχεις μάθει να μην δίνεις σημασία και να κάνεις την δουλεία σου. Βέβαια αν είσαι geek υπάρχουν λέξεις κλειδιά που σε κάνουν να δώσει προσοχή στα λεγόμενα.

Δύο συνάδελφοι κάτι έφτιαχναν και ο ένας από αυτούς εκστόμισε λέξεις όπως table, script, identity, sql server. Όπως ήταν φυσικό αυτές οι λέξεις πυροδότησαν τους αντίστοιχους attention trigger του εγκεφάλου μου.

Ο συνάδελφος αυτός ήθελε να βρει τους πίνακες που έχουν identity fields μέσα σε μια βάση και έψαχνε έναν ένα πίνακα μέσα στον SSMS και όπως γίνεται πάντα διαμαρτύρονταν γιατί δεν υπάρχει κάτι που να το δείχνει συγκεντρωτικά στον SSMS με το πάτημα ενός κουμπιού.

Κάτι τέτοιος δεν υπάρχει αλλά δεν είναι πυρηνική φυσική για να το μάθεις αρκεί να είσαι εξοικειωμένος με τα metadata του SQL Server.

Αυτό λοιπόν είναι εύκολο να το πάρεις σαν πληροφορία με ένα από τα παρακάτω queries. Διαλέξτε και εκτελέστε όποιο θέλετε.


SELECT T.TABLE_SCHEMA, C.TABLE_NAME,C.COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS AS C
    INNER JOIN INFORMATION_SCHEMA.TABLES AS T 
    ON T.TABLE_SCHEMA=C.TABLE_SCHEMA 
       AND 
       T.TABLE_NAME = C.TABLE_NAME
WHERE COLUMNPROPERTY(object_id(C.TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
      AND T.TABLE_TYPE='BASE TABLE'
ORDER BY C.TABLE_NAME;
GO

SELECT SCHEMA_NAME(o.schema_id) AS SchemaName
       ,o.name as TableName,c.name as ColumnName
FROM sys.objects o 
    INNER JOIN sys.columns c on o.object_id = c.object_id
WHERE c.is_identity = 1
    AND O.type='U';
GO

SELECT SCHEMA_NAME(CAST (OBJECTPROPERTYEX(object_id,'SchemaID') AS INT)) as SchemaName, 
       OBJECT_NAME(object_id) AS TableName, 
       name as ColumnName
FROM sys.columns
WHERE is_identity = 1 AND OBJECTPROPERTYEX(object_id,'IsUserTable')=1;
GO

SELECT SCHEMA_NAME(schema_id) AS schema_name
    , t.name AS table_name
    , c.name AS column_name
    , seed_value
    , increment_value
    , last_value
FROM sys.tables AS t
JOIN sys.identity_columns c ON t.object_id = c.object_id
ORDER BY schema_name, table_name;
GO

Με την σειρά να και τα αποτελέσματα των παραπάνω metadata queries χρησιμοποιώντας την Northwind database

results

/*antonch*/

Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios is a Data Solutions Consultant and Trainer. He has been working in IT since 1988. In his career, he has worked as senior developer, IT Manager, Solutions Architect and IT Consultant. Since 1995 he has been devoted on new technologies and software development tools, mainly by Microsoft, either by training company staff and colleagues or assisting them in design, development and implementation as a consultant or chief developer. He has focused in Databases and Data Science since 1995. He specialized in Microsoft SQL Server since version 6.0 in areas like SQL Server Internals, Database Design and Development, Business Intelligence and in 2010 he has started working with Azure Data Platform, NoSQL databases, Big Data Technologies and Machine Learning. He is an active member of many IT communities in Greece, answering colleagues' questions and writing articles in his web site. He is the owner of SQLschool.gr which is a community portal with a lot of information about Microsoft SQL Server. He has been a Microsoft Certified Trainer (MCT) since 2000. Microsoft honored him as MVP on Data Platform due to his activities in SQL Server since 2010. He holds a large number of Microsoft Certifications and Microsoft SQL Server Certifications since version 6.5.

Tip

What's New in SQL Server 2022 - Episodes

More Tips...

Become a member

If you want to receive updates from us become a member to our community.

Connect

Explore

Learn


sqlschool.gr © 2010-2024 All rights reserved

This site uses cookies for operational and analytics purposes only. By continuing to browse this site, you agree to their use.