Why is SQL Source Control stripping "ALTER" out of stored procedures?
rpaul
Posts: 7 New member
Last week I tried to commit a stored procedure through SQL Source Control, and was surprised to see that it was stripping the "ALTER" out of "CREATE OR ALTER" in the file. This is obviously a problem because the file will fail in deployment if it is an update.
Tagged:
Answers
When it comes to SQL Source Control and the Scripts folders, the scripts folder is just a representation of a schema, and is not intended to be runnable scripts.
SQL Source Control and SQL Compare (the engine behind the tool) create targeted scripts dynamically based on the state of the database you're deploying to.
Kind regards
Dan Calver | Redgate Software
Have you visited our Help Center?
All objects in the scripts folder albeit Views, Stored Procedures, and Tables will all be scripted as CREATE statements, if you're using SQL Compare and comparing the SQL Source Control project to another database, it will dynamically use ALTER statement or CREATE based on the state (whether the object is pre-existing)
Kind regards
Dan Calver | Redgate Software
Have you visited our Help Center?
With regards to using the tool correctly, it's both yes and no, I'll do my best to explain here.
When you commit to GitHub with SQL Source Control it's just storing the Scripts Folder, as mentioned earlier that script folder is just a representation of a schema.
If you create a stored procedure in SSMS with Create or Alter and then script it out into a new window, it will just have the Create statement, so when SQL Source Control is picking up the object, it's just grabbing its current state.
If an object already exists in the repository it will just overwrite what's existing, if an object has a change in the repository and you're doing a pull, it won't have an ALTER statement, it will just overwrite and recreate it in the state from the repository as shown below:
If you wish to update the database, you should make use of SQL Compare, using the SQL Source Control project as the source and deploying the changes to your target database, this will dynamically change the statements based on the state of the target.
Kind regards
Dan Calver | Redgate Software
Have you visited our Help Center?
Could you tell me more about your use case and I can assist further?
Kind regards
Dan Calver | Redgate Software
Have you visited our Help Center?
Thanks for that confirmation, what tools are you using in order to deploy the project?
We have SQL Change Automation which will work directly with your SQL Source Control project and script everything into migrations to be applied against your target within Jenkins.
https://documentation.red-gate.com/sca/deploying-database-changes/add-ons/jenkins
Kind regards
Dan Calver | Redgate Software
Have you visited our Help Center?
EDIT: To be more specific, we manually deploy once a week.
From the information you've given me so far, it would be relatively simple to implement SQL Change Automation into your current workflow.
Essentially you would have your Jenkins pipeline configured to use SCA and reference your SQL Source Control project as the source.
Kind regards
Dan Calver | Redgate Software
Have you visited our Help Center?