0
Please consider the script below..
He checks if there’s such a thing in a table, if there is one, he selects it. Then check again if it exists (only for didactic fims ;) and existing Dropa the column.
All right till then. Now comes the problem..
Do we get someone to run the script again? In theory, the field no longer exists, so it will not execute it nor select it or retry the right drop?
WRONG
If you run only the first part, you will see that from the error in select saying that the column does not exist (the check did not do anything) Now if you run the second part, it works! it DOES NOT attempt to drop since the column does not exist.
Now see the question.. how do I get to run this script N times without returning error?
IF EXISTS (SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMNS.TABLE_NAME = 'TabelaTal'
AND COLUMNS.COLUMN_NAME = 'campoTal')
begin
select campoTal from TabelaTal;
end;
IF EXISTS (SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMNS.TABLE_NAME = 'TabelaTal'
AND COLUMNS.COLUMN_NAME = 'campoTal')
begin
ALTER TABLE TabelaTal DROP COLUMN campoTal;
end;
Change your
IF
forIF COL_LENGTH('TabelaTal' , 'campoTal' ) IS NOT NULL
– Sorack