| 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 ENDELSE BEGIN PRINT 'Did not find a RecurringTime table.' ENDPRINT '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. |
 |
|
|
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 15Invalid column name 'Department'.Msg 207, Level 16, State 1, Server blah, Line 31Invalid column name 'Department'. |
 |
|
|
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 ENDELSE BEGIN PRINT 'Did not find a RecurringTime table.' ENDPRINT '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. |
 |
|
|
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... |
 |
|
|
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? |
 |
|
|
shaggy
Posting Yak Master
248 Posts |
Posted - 2009-03-18 : 10:13:24
|
| You can also eliminate dynamic execution of update statement. |
 |
|
|
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. |
 |
|
|
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? |
 |
|
|
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. |
 |
|
|
pootle_flump
1064 Posts |
|
|
|