| Author |
Topic |
|
vgr.raju
Yak Posting Veteran
95 Posts |
Posted - 2008-02-19 : 03:16:54
|
| Hi, I want to do some DML Operation based on the column existence in the table,But I get the Error and not able to figure out why.IF EXISTS(SELECT * FROM SYS.COLUMNS WHERE [name]='COL3' AND OBJECT_ID=OBJECT_ID('ColumnExistenceCheck'))BEGINPRINT'COL3 EXISTS'INSERT INTO ColumnExistenceCheck(COL3)SELECT COL3 FROM ColumnExistenceCheckENDELSEBEGININSERT INTO ColumnExistenceCheck(COL2)SELECT 2ENDMsg 207, Level 16, State 1, Line 8Invalid column name 'COL3'.Msg 207, Level 16, State 1, Line 7Invalid column name 'COL3'.I have the existence check COL3. IF COL3 Exists,then only I go to begin..end Block..In this scenario,the COL3 does not exist..Still SQL Server validates the column names in the Table which throws the Error.Any help would be highly appreciated.Thanks,RajuThanks!Rajuhttp://rajusqlblog.blogspot.com/ |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-19 : 03:35:10
|
| If col_length('table_name','column_name') is null--column doesnt existelse--column existsMadhivananFailing to plan is Planning to fail |
 |
|
|
mahesh_bote
Constraint Violating Yak Guru
298 Posts |
Posted - 2008-02-19 : 03:51:59
|
quote: Originally posted by vgr.raju Hi, I want to do some DML Operation based on the column existence in the table,But I get the Error and not able to figure out why.IF EXISTS(SELECT * FROM SYS.COLUMNS WHERE [name]='COL3' AND OBJECT_ID=OBJECT_ID('ColumnExistenceCheck'))BEGINPRINT'COL3 EXISTS'INSERT INTO ColumnExistenceCheck(COL3)SELECT COL3 FROM ColumnExistenceCheckENDELSEBEGININSERT INTO ColumnExistenceCheck(COL2)SELECT 2ENDMsg 207, Level 16, State 1, Line 8Invalid column name 'COL3'.Msg 207, Level 16, State 1, Line 7Invalid column name 'COL3'.I have the existence check COL3. IF COL3 Exists,then only I go to begin..end Block..In this scenario,the COL3 does not exist..Still SQL Server validates the column names in the Table which throws the Error.Any help would be highly appreciated.Thanks,RajuThanks!Rajuhttp://rajusqlblog.blogspot.com/
I really didn't get, what exactly u wants to do here? Why r u selecting the value and again inserting the same in the same table? whats the use of it?Let us know, what u wants to do?Mahesh |
 |
|
|
vgr.raju
Yak Posting Veteran
95 Posts |
Posted - 2008-02-19 : 07:26:33
|
| Hi Madhivanan, Thanks for the reply.When I try using col_length function, it throws the same error message.If col_length('ColumnExistenceCheck','COL3') is not nullBEGINPRINT'COL3 EXISTS'INSERT INTO ColumnExistenceCheck(COL3)SELECT COL3 FROM ColumnExistenceCheckENDELSEBEGININSERT INTO ColumnExistenceCheck(COL2)SELECT 2ENDMsg 207, Level 16, State 1, Line 5Invalid column name 'COL3'.Msg 207, Level 16, State 1, Line 4Invalid column name 'COL3'.We are doing the database updgrade step. These columns are going to be dropped in few test environment and I want my script to handle the existence and non-existence of these columns.Thanks,RajuThanks!Rajuhttp://rajusqlblog.blogspot.com/ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-19 : 07:36:14
|
| I think it is parser error. Make it as procedure and runBut still dont know what you are trying to doMadhivananFailing to plan is Planning to fail |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2008-02-19 : 07:36:38
|
Make sure you have not created same table under different schemas. Prefix schema name to the table name while checking like:select col_length('dbo.ColumnExistenceCheck','COL3')Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
vgr.raju
Yak Posting Veteran
95 Posts |
Posted - 2008-02-19 : 07:49:45
|
| I have referenced the schema and the same issue.The issue for the problem is "I am referencing the invalid column name"in the select statement. But this shouldn't even go inside that block because the if condition returns false. Eventhough IF condition return false, SQL Server somehow tries to validate the columns and throws the Error.If I don't have the invalid column reference in select statement,then things work fine..Any help would be much appreciated..If col_length('ColumnExistenceCheck','COL3') is not nullBEGINPRINT'COL3 EXISTS'INSERT INTO ColumnExistenceCheck(COL3)SELECT COL3 FROM ColumnExistenceCheckENDELSEBEGININSERT INTO ColumnExistenceCheck(COL2)SELECT 2ENDThanks!Rajuhttp://rajusqlblog.blogspot.com/ |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2008-02-19 : 08:22:54
|
This happens simply because table name resolution is done at compile time. So no matter what condition you write to prevent this, column name will always be resolved at compile-time and will give you an error.The only way to prevent this is to wrap INSERT statement inside EXEC() call.If col_length('ColumnExistenceCheck','COL3') is not nullBEGINEXEC('INSERT INTO ColumnExistenceCheck(COL3)SELECT COL3 FROM ColumnExistenceCheck')ENDHarsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
vgr.raju
Yak Posting Veteran
95 Posts |
Posted - 2008-02-19 : 08:23:53
|
| Interestingly,it works when I use the dynamic SQL.declare @dsql varchar(4000)IF EXISTS(SELECT * FROM SYS.COLUMNSWHERE [name]='COL3' AND OBJECT_ID=OBJECT_ID('ColumnExistenceCheck'))BEGINPRINT'COL3 EXISTS'set @dsql='INSERT INTO ColumnExistenceCheck(COL3)SELECT COL3 FROM ColumnExistenceCheck'ENDELSEBEGINset @dsql='INSERT INTO ColumnExistenceCheck(COL2)SELECT 2'ENDEXEC(@dsql)Thanks,RajuThanks!Rajuhttp://rajusqlblog.blogspot.com/ |
 |
|
|
|