May 17, 2007 at 3:05 am
Is the following T-SQL line ok?
if exists (select 1 from INFORMATION_SCHEMA.tables where table_name = 'Result') and exists (select 1 from INFORMATION_SCHEMA.tables where table_name = 'Time_definition') begin
May 17, 2007 at 3:20 am
I would prefer to use system functions:
IF OBJECTPROPERTY (Object_ID('Result'), 'IsUserTable')= 1 and OBJECTPROPERTY (Object_ID('Time_definition'), 'IsUserTable') = 1
BEGIN
Your query will include views as well.
You need to add "AND TABLE_TYPE = 'BASE TABLE'" to make sure 'Result' is a table.
_____________
Code for TallyGenerator
May 17, 2007 at 5:34 am
I'm right there with Serqiy... I use system functions whenever I can...
I'll probably get my head handed to me for using undocumented features but I believe this works both in 2k and 2k5... a bit of "shorthand" for the Object_ID function...
IF OBJECT_ID('dbo.Result','U') IS NOT NULL
AND OBJECT_ID('dbo.Time_definition,'U') IS NOT NULL
BEGIN
--Jeff Moden
Change is inevitable... Change for the better is not.
May 17, 2007 at 5:36 am
Is it quicker to use OBJECT_ID? If so, any idea how much quicker?
May 17, 2007 at 5:49 am
Dpends on the queries you build. Many of the system views join many tables, many times even in some cases. So if you can avoid that and do a clustered index search in the system tables instead, you'll definitly get a performance boost.
May 17, 2007 at 7:19 am
Considering that only two rows of a table are being accessed for the original question being asked, I'd have to say the delta-t would be measured in nano-seconds. The big difference is in the readability and the number of characters typed. Also, despite what Microsoft says, the I/S views can change... the functionality of the OBJECT_ID function will not change anymore than, say, the ABS function.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 17, 2007 at 7:32 pm
OBJECT_ID('dbo.Result','U') does not work in SQL2000.
Did not try it in 2k yet.
_____________
Code for TallyGenerator
May 17, 2007 at 9:16 pm
It will work without the ", 'U'", since the name of the objects must be unique to a single owner, then there's no point in having to specify the object type has well. Looks like they had to change that in 2k5 tho!
May 17, 2007 at 9:31 pm
Actually there is a point to specify object type.
If you check for table existence OBJECT_ID will bring you valid ID and you could end up trying insert something into non-updateable view or even into UDF.
That's why I suggested OBJECTPROPERTY. If object does not exist it will return NULL. If it exists but is not user table it will return 0. And 1 will guarantee you are dealing with table, not anything else.
_____________
Code for TallyGenerator
May 17, 2007 at 11:54 pm
Hmmm... I'm using SQL Server 2000 sp4 Developer's Edition and it works fine...
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply