Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 NULL != OBJECT_ID( 'table_Mit') fails

Author  Topic 

Mithun
Starting Member

1 Post

Posted - 2010-03-05 : 22:34:57
Hi All,
I have a Job which executes the below query to check the availability of a table

IF ( NULL != OBJECT_ID( 'table_mit' ) )
BEGIN
PRINT ''
PRINT ' ... Dropping table: table_mit'
PRINT ''
DROP TABLE table_mit
END
GO


PRINT ''
PRINT ' Create table: table_mit'
PRINT ''
GO

CREATE TABLE table_mit (
Name Char(10) NOT NULL
,First Name Char(10) NOT NULL
,Last Name Char(30) NOT NULL
)
GO


the table table_mit already exist in my database but the condition
IF ( NULL != OBJECT_ID( 'table_mit' ) ) is failing
Can somebody help me out why is this happening

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2010-03-05 : 23:46:13
try like this

IF ( OBJECT_ID('table_mit') IS NOT NULL )
BEGIN
PRINT ''
PRINT ' ... Dropping table: table_mit'
PRINT ''
DROP TABLE table_mit
END
GO
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-06 : 02:09:19
quote:
Originally posted by Mithun

Hi All,
I have a Job which executes the below query to check the availability of a table

IF ( NULL != OBJECT_ID( 'table_mit' ) )
BEGIN
PRINT ''
PRINT ' ... Dropping table: table_mit'
PRINT ''
DROP TABLE table_mit
END
GO


PRINT ''
PRINT ' Create table: table_mit'
PRINT ''
GO

CREATE TABLE table_mit (
Name Char(10) NOT NULL
,First Name Char(10) NOT NULL
,Last Name Char(30) NOT NULL
)
GO


the table table_mit already exist in my database but the condition
IF ( NULL != OBJECT_ID( 'table_mit' ) ) is failing
Can somebody help me out why is this happening



You cant use operators like = ,!= etc to compare with NULL unless you've ANSI NULL settings off which is not the default. Under default conditions NULL is not regarded as a value but it represents condition unknown or undefined value. So you need to use IS NULL or IS NOT NULL for comparisons of null values

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -