Identity columns

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • newtophp2000@yahoo.com

    Identity columns

    I have been using the following query to identify the IDENTITY columns
    in a given table. (The query is inside an application.)

    select column_name
    from information_sch ema.columns
    where table_schema = 'user_a' and
    table_name = 'tab_a' and
    columnproperty( object_id(table _name), column_name, 'IsIdentity') = 1

    This works. When "user_a" performs the query, everything is OK.

    Now, another user wanted to use the same application. So, "user_b"
    clicks on a button, and the exact same query as above is run. (No
    substitutions are made; user_b is trying to see the identity column in
    [user_a].[tab_a]). However, the query returns null, instead of the
    identity column name. User_b can read the table and select from it
    just fine.

    Why am I getting two different results against the same query? Do I
    need to rewrite the query to go against different information schema
    views?

  • Dan Guzman

    #2
    Re: Identity columns

    You might try specifying the table schema in the OBJECT_ID function to avoid
    ambiguity. Also, consider quoting the identifiers:

    SELECT COLUMN_NAME
    FROM INFORMATION_SCH EMA.COLUMNS
    WHERE TABLE_SCHEMA = 'user_a' AND
    TABLE_NAME = 'tab_a' AND
    COLUMNPROPERTY(
    OBJECT_ID(
    QUOTENAME(TABLE _SCHEMA) + '.' + QUOTENAME(TABLE _NAME)),
    COLUMN_NAME, 'IsIdentity') = 1

    --
    Hope this helps.

    Dan Guzman
    SQL Server MVP

    <newtophp2000@y ahoo.com> wrote in message
    news:1103334367 .788877.229370@ f14g2000cwb.goo glegroups.com.. .[color=blue]
    >I have been using the following query to identify the IDENTITY columns
    > in a given table. (The query is inside an application.)
    >
    > select column_name
    > from information_sch ema.columns
    > where table_schema = 'user_a' and
    > table_name = 'tab_a' and
    > columnproperty( object_id(table _name), column_name, 'IsIdentity') = 1
    >
    > This works. When "user_a" performs the query, everything is OK.
    >
    > Now, another user wanted to use the same application. So, "user_b"
    > clicks on a button, and the exact same query as above is run. (No
    > substitutions are made; user_b is trying to see the identity column in
    > [user_a].[tab_a]). However, the query returns null, instead of the
    > identity column name. User_b can read the table and select from it
    > just fine.
    >
    > Why am I getting two different results against the same query? Do I
    > need to rewrite the query to go against different information schema
    > views?
    >[/color]


    Comment

    • newtophp2000@yahoo.com

      #3
      Re: Identity columns

      Thanks, Dan! This works great.


      Dan Guzman wrote:[color=blue]
      > You might try specifying the table schema in the OBJECT_ID function[/color]
      to avoid[color=blue]
      > ambiguity. Also, consider quoting the identifiers:
      >
      > SELECT COLUMN_NAME
      > FROM INFORMATION_SCH EMA.COLUMNS
      > WHERE TABLE_SCHEMA = 'user_a' AND
      > TABLE_NAME = 'tab_a' AND
      > COLUMNPROPERTY(
      > OBJECT_ID(
      > QUOTENAME(TABLE _SCHEMA) + '.' + QUOTENAME(TABLE _NAME)),
      > COLUMN_NAME, 'IsIdentity') = 1
      >
      > --
      > Hope this helps.
      >
      > Dan Guzman
      > SQL Server MVP
      >[/color]

      Comment

      • newtophp2000@yahoo.com

        #4
        Re: Identity columns

        Dan Guzman wrote:[color=blue]
        > You might try specifying the table schema in the OBJECT_ID function[/color]
        to avoid[color=blue]
        > ambiguity. Also, consider quoting the identifiers:
        >
        > SELECT COLUMN_NAME
        > FROM INFORMATION_SCH EMA.COLUMNS
        > WHERE TABLE_SCHEMA = 'user_a' AND
        > TABLE_NAME = 'tab_a' AND
        > COLUMNPROPERTY(
        > OBJECT_ID(
        > QUOTENAME(TABLE _SCHEMA) + '.' + QUOTENAME(TABLE _NAME)),
        > COLUMN_NAME, 'IsIdentity') = 1[/color]

        Hi Dan,

        As I noted before, this works; however, it seems that it doesn't do the
        right thing if the databases are different.

        So, my question is, given a database, a table, and a column (along with
        dbo/table owner), is there a way to check whether or not that column is
        the identity for that table? Is it possible to generalize the above
        query to work across databases/users/etc.?

        Thanks!


        [color=blue]
        > --
        > Hope this helps.
        >
        > Dan Guzman
        > SQL Server MVP
        >[/color]

        Comment

        • Erland Sommarskog

          #5
          Re: Identity columns

          (newtophp2000@y ahoo.com) writes:[color=blue]
          > As I noted before, this works; however, it seems that it doesn't do the
          > right thing if the databases are different.
          >
          > So, my question is, given a database, a table, and a column (along with
          > dbo/table owner), is there a way to check whether or not that column is
          > the identity for that table? Is it possible to generalize the above
          > query to work across databases/users/etc.?[/color]

          SELECT *
          FROM db..sysobjects o
          JOIN db..syscolumns c ON o.id = c.id
          JOIN db..sysusers u ON o.uid = u.uid
          WHERE o.name = @tbl
          AND c.name = @col
          AND u.name = @user
          AND c.status & 0x80 <> 0

          will return a row if the column is an identity column.

          When I wrote this query, I assumed that I was on undocumented ground,
          but this value is actually documented for syscolumns.stat us, and thus
          permissible to use. The code should work in SQL 2005 as well. (Although
          SQL 2005 also offer new catalog views which are better for the task.)
          --
          Erland Sommarskog, SQL Server MVP, esquel@sommarsk og.se

          Books Online for SQL Server SP3 at
          SQL Server 2025 redefines what's possible for enterprise data. With developer-first features and integration with analytics and AI models, SQL Server 2025 accelerates AI innovation using the data you already have.

          Comment

          • Dan Guzman

            #6
            Re: Identity columns

            > So, my question is, given a database, a table, and a column (along with[color=blue]
            > dbo/table owner), is there a way to check whether or not that column is
            > the identity for that table? Is it possible to generalize the above
            > query to work across databases/users/etc.?[/color]

            You can specify the desired database context with a USE statement
            immediately before the SELECT to set the database context.

            To return data from different databases in the same query, you'll need to
            use the technique Erland suggested and use a UNION ALL to concatenate
            results from different databases.

            --
            Hope this helps.

            Dan Guzman
            SQL Server MVP

            <newtophp2000@y ahoo.com> wrote in message
            news:1108137110 .964647.268740@ o13g2000cwo.goo glegroups.com.. .[color=blue]
            > Dan Guzman wrote:[color=green]
            >> You might try specifying the table schema in the OBJECT_ID function[/color]
            > to avoid[color=green]
            >> ambiguity. Also, consider quoting the identifiers:
            >>
            >> SELECT COLUMN_NAME
            >> FROM INFORMATION_SCH EMA.COLUMNS
            >> WHERE TABLE_SCHEMA = 'user_a' AND
            >> TABLE_NAME = 'tab_a' AND
            >> COLUMNPROPERTY(
            >> OBJECT_ID(
            >> QUOTENAME(TABLE _SCHEMA) + '.' + QUOTENAME(TABLE _NAME)),
            >> COLUMN_NAME, 'IsIdentity') = 1[/color]
            >
            > Hi Dan,
            >
            > As I noted before, this works; however, it seems that it doesn't do the
            > right thing if the databases are different.
            >
            > So, my question is, given a database, a table, and a column (along with
            > dbo/table owner), is there a way to check whether or not that column is
            > the identity for that table? Is it possible to generalize the above
            > query to work across databases/users/etc.?
            >
            > Thanks!
            >
            >
            >[color=green]
            >> --
            >> Hope this helps.
            >>
            >> Dan Guzman
            >> SQL Server MVP
            >>[/color]
            >[/color]


            Comment

            Working...