in a given table. (The query is inside an application.)
select column_name
from information_schema.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?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_SCHEMA.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@.yahoo.com> wrote in message
news:1103334367.788877.229370@.f14g2000cwb.googlegr oups.com...
>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_schema.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?|||Thanks, Dan! This works great.
Dan Guzman wrote:
> 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_SCHEMA.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|||Dan Guzman wrote:
> 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_SCHEMA.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
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!
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP|||(newtophp2000@.yahoo.com) writes:
> 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.?
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.status, 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@.sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||> 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.?
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@.yahoo.com> wrote in message
news:1108137110.964647.268740@.o13g2000cwo.googlegr oups.com...
> Dan Guzman wrote:
>> 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_SCHEMA.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
> 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!
>
>> --
>> Hope this helps.
>>
>> Dan Guzman
>> SQL Server MVP
>
No comments:
Post a Comment