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
 General SQL Server Forums
 New to SQL Server Programming
 Invalid column name in conditional

Author  Topic 

blueboy
Starting Member

5 Posts

Posted - 2009-03-17 : 14:37:31
I want to check for table / column existence and update IF the column exists. When this code is run against a table with the column it works fine. When run against a table without the column I get the invalid column name error. What am I missing?


IF EXISTS(SELECT name FROM sysobjects WHERE name = 'RecurringTime')
BEGIN
PRINT ''
PRINT 'Found RecurringTime table.'
IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'RecurringTime'
AND
COLUMN_NAME = 'Department')
BEGIN
PRINT ' It has a Department column.'
PRINT ' Checking for rows with no department.'

DECLARE @t_ID INT
DECLARE time_cursor CURSOR FOR
SELECT TimeID
FROM RecurringTime
WHERE Department IS NULL

-- Use the cursor to "get" the ID from the "errant" rows
OPEN time_cursor

-- now loop through these rows and update the department
WHILE 1=1
BEGIN
FETCH NEXT FROM time_cursor
INTO @t_ID
IF @@FETCH_STATUS <> 0
BREAK

PRINT ' Updating row with ID: ' + CONVERT(VARCHAR(9), @t_ID)
UPDATE RecurringTime
SET Department = 'All Departments'
WHERE TimeID = @t_ID
END
CLOSE time_cursor
DEALLOCATE time_cursor
END
ELSE
BEGIN
PRINT ' No ''Department'' column in this table.'
END
END
ELSE
BEGIN
PRINT 'Did not find a RecurringTime table.'
END
PRINT 'Exiting.'

pootle_flump

1064 Posts

Posted - 2009-03-17 : 14:47:33
What do you mean "errors"? When? What is the message? Where is this code? If it is a proc then you could be getting Deferred Name Resolution errors, but we need more details.
Go to Top of Page

blueboy
Starting Member

5 Posts

Posted - 2009-03-17 : 14:57:15
quote:
Originally posted by pootle_flump

What do you mean "errors"? When? What is the message? Where is this code? If it is a proc then you could be getting Deferred Name Resolution errors, but we need more details.



This code is in a text file that is used in an osql command.
When executed and the RecurringTime table has no Department column it generates:
Msg 207, Level 16, State 3, Server blah, Line 15
Invalid column name 'Department'.
Msg 207, Level 16, State 1, Server blah, Line 31
Invalid column name 'Department'.

Go to Top of Page

pootle_flump

1064 Posts

Posted - 2009-03-18 : 04:11:45
Yeah, this sounds like deferred name resolution. Try the below - if it works, I'll explain why.

IF EXISTS(SELECT name FROM sysobjects WHERE name = 'RecurringTime')
BEGIN
PRINT ''
PRINT 'Found RecurringTime table.'
IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'RecurringTime'
AND
COLUMN_NAME = 'Department')
BEGIN
PRINT ' It has a Department column.'

EXEC ('
UPDATE dbo.RecurringTime
SET Department = ''All Departments''
WHERE Department IS NULL
')

END
ELSE
BEGIN
PRINT ' No ''Department'' column in this table.'
END
END
ELSE
BEGIN
PRINT 'Did not find a RecurringTime table.'
END
PRINT 'Exiting.'
We'll also have to have a serious conversation about the evil of cursors and why on earth you can't be sure about the structure of your tables.
Go to Top of Page

blueboy
Starting Member

5 Posts

Posted - 2009-03-18 : 09:58:56
Perfect!

I also like how simple you made it eliminating the cursor. I really should have thought of that...
Go to Top of Page

pootle_flump

1064 Posts

Posted - 2009-03-18 : 10:02:44
Glad it worked and glad you see why eliminating cursors is a good thing.

So how come you don't know the columns in your table at run time?
Go to Top of Page

shaggy
Posting Yak Master

248 Posts

Posted - 2009-03-18 : 10:13:24
You can also eliminate dynamic execution of update statement.
Go to Top of Page

blueboy
Starting Member

5 Posts

Posted - 2009-03-18 : 10:31:13
quote:
Originally posted by pootle_flump

Glad it worked and glad you see why eliminating cursors is a good thing.

I only thought it was a good thing because it made the code so much 'cleaner', easier to read. But from what I've read in a number of posts, cursors apparently have some other issues...

quote:

So how come you don't know the columns in your table at run time?


Our application has a small database. The application has evolved over time and the new features introduced required more columns. The script is actually a work around for an errant upgrade installer.
Go to Top of Page

blueboy
Starting Member

5 Posts

Posted - 2009-03-18 : 12:17:33
How, Shaggy?

And by dynamic I assume you mean the use of EXEC, or it it something else?
Go to Top of Page

pootle_flump

1064 Posts

Posted - 2009-03-19 : 04:22:44
quote:
Originally posted by shaggy

You can also eliminate dynamic execution of update statement.

Deferred Name Resolution - if the table exists and the column does not, it will not compile.

blueboy - I really think you should get your database(s) sorted out rather than attempt to code around it.
Go to Top of Page

pootle_flump

1064 Posts

Posted - 2009-03-19 : 04:43:45
quote:
Originally posted by blueboy

And by dynamic I assume you mean the use of EXEC

Correct.
Have a look here:
http://msdn.microsoft.com/en-us/library/ms190686.aspx
In particular the first Note - exactly your problem.
Go to Top of Page
   

- Advertisement -