I'm trying to make a query that is somewhat universal to be run against differing versions of the same DB. In older versions, certain columns do not exist in the specific table I'm trying to address. The idea is for this to be run by someone who won't know what version they have. So, I'm trying to make the query check if a certain column exists (and if that one doesn't, none of the other questionable columns do either), then run one of two variations of the same script based on which columns exist and don't.
Masking some proprietary info, this is the problem area:
IF COL_LENGTH('dbo.users', '<column A>') IS NOT NULL -- Check for existence of <column A> column
-- Some earlier versions do not have this column
UPDATE Users
SET Password = 'XXXX', <column A> = 0, <column B> = NULL
WHERE Login = 'User' -- Reset password for later versions
ELSE
UPDATE Users
SET Password = 'XXXX'
WHERE Login = 'User' -- Reset password for earlier versions
PRINT 'The Password for Login "User" has been reset to XXXX.'
END
The problem I'm finding is that the entire script (There is more above this area) is erroring out and doing nothing because column A does not exist in the older version DB. It doesn't seem to be ignoring the first update statement shown if that column is not present.
Testing on the newer version DB is fully successful, though that one has column A.
Any ideas on this are appreciated. Thank you.