In SQL Compare can generated scripts do 'IF EXISTS....' check before adding columns?
JayR
Posts: 5 New member
in SQL Compare
We are having SQL Compare generate scripts to upgrade databases. We have a fairly complicated scenario where we need to add a new not null column to a table in pre-scripts and populate the new column with scripts. RedGate is generating scripts that attempt to add the column that is already there. It would be really great if there was a way to tell RedGate to check for the columns existence before the script attempts to adding the column.
Tagged:
Answers
There is an option in SQL Compare to Add object existence checks:
Have you tried this or does it not add them for columns?
Kind regards,
Alex
Have you visited our Help Center?
Hmm, to clarify - are you generating the SQL Compare deployment before or after you run the pre-script that generates and populates the new column?
SQL Compare should be generating it's deployment script based on the current state, which would seem to indicate that the deployment script is being generated before the pre-script that adds the new not-null column. In that case, there's not much SQL Compare can do.
If that doesn't seem to be the case, can you give a bit more overview of what your process is and perhaps an example of the pre-script and the table source and target where the issue is occurring?
If needed, it looks like you are supported so I can reach out through a support ticket to get that information from you.
Kind regards,
Alex
Have you visited our Help Center?
Here is a copy of the script that was generated. The column we added is called 'ARCo' . Basically that entire chunk the the script should have an "if exists..." check so that it can be skipped if the column is already there. Possibly RedGate isn't putting in the appropriate "if exists..." because of various combination of not null/indexes/primary keys on the table.
....
...
...
...
..
...
...
...
Looks like you're still using version 12.4.10.4968 - is that for any particular reason? It looks like you are supported so you should be able to install version 13 side by side with version 12. Could you please do this and see if the same thing occurs in the latest 13.7.16?
Kind regards,
Alex
Have you visited our Help Center?
Actually, wait, the bit in the script you have there is the temp table recovery table
We would create that table to be like the source, then insert the data from the original target table, and then delete the original and rename the recovery to the original name (to preserve data). That's why the INSERT doesn't have an entry for the ARCo column as the target didn't have it to start.
Going over this again, SQL Compare wouldn't create an object existence for the recovery table as it is generated at the time so wouldn't exist and then dropping and renaming wouldn't collide with an existing object. Where are you actually getting an error and what specifically does it say?
Also, I need to clarify this again as it sounds like you are comparing two tables - the source with the ARCo column and the target without - but then you are running a script outside of the SQL Compare process to add the ARCo column to the target and populate it with data which we can't account for.
Kind regards,
Alex
Have you visited our Help Center?
Righto, so the main problem here is that columns are not considered their own objects in SQL Compare and so the object existence check isn't going to work for them and thus there isn't going to be a way to get this to work for you with the current functionality.
I was getting stuck on thinking the object existence check was for the temp table itself rather than seeing if the column existed to determine whether the temp table needed to be created, drop original and rename temp all needed to happen.
You should create a suggestion on the SQL Compare Uservoice Forum https://redgate.uservoice.com/forums/141379-sql-compare for this.
Kind regards,
Alex
Have you visited our Help Center?