473,663 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?

Jul 23 '05 #1
5 3495
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

<ne**********@y ahoo.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.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_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?

Jul 23 '05 #2
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_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


Jul 23 '05 #3
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_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
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


Jul 23 '05 #4
(ne**********@y ahoo.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.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, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #5
> 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

<ne**********@y ahoo.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.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_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


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

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

112
10308
by: Andy | last post by:
Hi All! We are doing new development for SQL Server 2000 and also moving from SQL 7.0 to SQL Server 2000. What are cons and pros for using IDENTITY property as PK in SQL SERVER 2000? Please, share your experience in using IDENTITY as PK .
17
3579
by: Trevor Best | last post by:
I don't know if this has been reported before but it appears to be a bug with Access. If I create two tables both with an identity column then create an insert trigger on table1 that inserts a related record into table2, now create a form on table1 with a subform on table2. Insert records into the main form to your heart's content and everything's fine, each main record automatically gets a child record and so far the identity columns...
2
15633
by: Omavlana Omav | last post by:
Hi, I have created a datatable, dataadapter, added the table to a dataset and filling the table. Here I want to add an identity column to the above datatable and Later I will use this identity colum for printing the records using print document. How to create an identity column that increments its value when each row
41
3148
by: pb648174 | last post by:
In a multi-user environment, I would like to get a list of Ids generated, similar to: declare @LastId int select @LastId = Max(Id) From TableMania INSERT INTO TableMania (ColumnA, ColumnB) SELECT ColumnA, ColumnB From OtherTable Where ColumnC > 15 --get entries just added
5
32014
by: Veeru71 | last post by:
Given a table with an identity column (GENERATED BY DEFAULT AS IDENTITY), is there any way to get the last generated value by DB2 for the identity column? I can't use identity_val_local() as the INSERTS are happening in a different session. Eg, We have the following table....
0
1236
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, dataAdapter1.Fill(ds, "tbl1") say this adds 30 rows to tbl1. Then I want to add an Identity column to the table ds.Tables("tbl1").Columns.Add("tblID", System.Type.GetType("System.Int32") ds.Tables("tbl1").Columns("tblID").AutoIncrement = True ds.Tables("tbl1").Columns("tblID").AutoIncrementSeed = 1
0
3028
by: Frank Swarbrick | last post by:
So we're trying to decide if it's better to use IDENTITY columns or sequences to create a surrogate key as the primary key for our tables. I kind of like the identity column, because it's more 'tightly integrated' in to the table. With a sequence you have to make sure that each application that inserts records uses the same sequence. (Probably not likely that it wouldn't, but...) One thing where it seems like a SEQUENCE would be...
11
2687
by: stegze | last post by:
Hi All, I have a problem with a DB2 server of my customer. It is a Debian Linux running DB2 Express-C. I have an IDENTITY field as PK in a table and I use this value as FK in another table. Two weeks ago the FK values got corrupted or mixed up somehow. I also got some warnings about system temporary table spaces so I created a 16K page size system temporary table space to be sure. All seemed to be okay until today. Something happened an...
3
2989
by: Frank Swarbrick | last post by:
How can I fix this so that it doesn't do essentially the same scan twice, but it gives the same results (a single row where IDENTITY_MODIFIER is either 'identityoverride' or '' and ROWCHANGETIMESTAMP_MODIFIER is either 'rowchangetimestampoverride' or ''? select case when exists ( select * from syscat.columns
0
8345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8771
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8548
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8634
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4182
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.