Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Return name and identity for all tables

Return name and identity for all tables

Scheduled Pinned Locked Moved Database
databasehelpquestion
3 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yippiecoder
    wrote on last edited by
    #1

    Little help with returning both the name and the current identity for all tables? If a table doesn't have any rows, instead of the current identity, I'd like to return 0. The database will be in single user mode during the query if that helps. The following query is close but it still returns 1 for tables that don't have any rows. Somehow the CASE expression isn't right. Thanks in advance.SELECT TABLE_NAME, CASE WHEN COUNT(TABLE_NAME) = 0 THEN 0 ELSE IDENT_CURRENT(TABLE_NAME) END FROM INFORMATION_SCHEMA.TABLES WHERE OBJECTPROPERTY(OBJECT_ID(TABLE_NAME), 'TableHasIdentity') = 1 AND TABLE_TYPE = 'BASE TABLE' GROUP BY TABLE_NAME

    B 1 Reply Last reply
    0
    • Y yippiecoder

      Little help with returning both the name and the current identity for all tables? If a table doesn't have any rows, instead of the current identity, I'd like to return 0. The database will be in single user mode during the query if that helps. The following query is close but it still returns 1 for tables that don't have any rows. Somehow the CASE expression isn't right. Thanks in advance.SELECT TABLE_NAME, CASE WHEN COUNT(TABLE_NAME) = 0 THEN 0 ELSE IDENT_CURRENT(TABLE_NAME) END FROM INFORMATION_SCHEMA.TABLES WHERE OBJECTPROPERTY(OBJECT_ID(TABLE_NAME), 'TableHasIdentity') = 1 AND TABLE_TYPE = 'BASE TABLE' GROUP BY TABLE_NAME

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      SELECT CASE WHEN COUNT(TABLE_NAME) = 0 THEN 0 WHEN COUNT(TABLE_NAME) > 0 THEN IDENT_CURRENT(TABLE_NAME) END FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' GROUP BY TABLE_NAME


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

      D 1 Reply Last reply
      0
      • B Blue_Boy

        SELECT CASE WHEN COUNT(TABLE_NAME) = 0 THEN 0 WHEN COUNT(TABLE_NAME) > 0 THEN IDENT_CURRENT(TABLE_NAME) END FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' GROUP BY TABLE_NAME


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.aktualiteti.com

        D Offline
        D Offline
        David Skelly
        wrote on last edited by
        #3

        Maybe I'm missing something here. How can that ever return 0? If I have understood it correctly, this is what the query will do: Select the rows from TABLES with type "BASE TABLE" Group them by TABLE_NAME Counts the number of rows for each TABLE_NAME If the count is 0, return 0 If the count is not 0, return the identity of the table But, surely the only way COUNT(TABLE_NAME) can be 0 is if there are no rows in TABLES with this TABLE_NAME and type "BASE TABLE". In which case, this query would never pick up that table name, so it would never appear in the result set. So for any TABLE_NAME that this query picks up, the count will always be greater than 0.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups