Error in deployment script for table rebuild with computed columns
a.higgins
Posts: 90 Bronze 2
in SQL Compare
When attempting to deploy a change that
- necessitated a table rebuild, and
- included a computed field
My deployment script ran into an error: invalid column name "MyTable.MyColumn".
Investigation eventually revealed that the error was in the portion of the script that did the rebuild. What's happening is that the script creates a new table "RG_Recovery_1_MyTable" that includes the computed field, but as part of the computed field definition retains the table name of the original computation.
So, for example, when trying to rebuild a table named "FactChecklistCompletion" the script included the following (simplified):
CREATE TABLE dbo.RG_Recovery_1_FactChecklistCompletion<br> (<br> ChecklistCompletion_SK INT NOT NULL IDENTITY(1, 1)<br> ,ChecklistItem_SK INT NOT NULL<br> ,Person_SK INT NOT NULL<br> ,ChecklistItemCompletionDate DATETIME2(3) NULL<br> ,ChecklistItemCompletionDateKey AS (CONVERT(INT, CONVERT(CHAR(8), <b>FactChecklistCompletion.</b>ChecklistItemCompletionDate, (112)), (0)))<br> )<br>GO
Note that the definition of the ChecklistItemCompletionDateKey qualifies the attribute in the formula definition with the original table name, "FactChecklistCompletion".
Fixing this on my end involved manually editing the script to remove the table name from the computed column definition, at which point it ran successfully.
Tagged:
Answers
(
ChecklistCompletion_SK INT NOT NULL IDENTITY(1, 1)
,ChecklistItem_SK INT NOT NULL
,Person_SK INT NOT NULL
,ChecklistItemCompletionDate DATETIME2(3) NULL
,ChecklistItemCompletionDateKey AS (CONVERT(INT, CONVERT(CHAR(8), FactChecklistCompletion.ChecklistItemCompletionDate, (112)), (0)))
)
GO
I've reproduced it here as well and raised the issue under internal reference SC-10863 with the developers. I will post here when I have an update on the issue, but for now as you have seen, removing the 2 part naming of the columns used in the computed column is what you will need to do to get around this.
Kind regards,
Alex
Have you visited our Help Center?
Have you visited our Help Center?
Nor can I get it to insert the columns qualified by the table name when using auto-complete so I'm not sure if it's SQL Prompt - or if it was, if it still would be doing it.
It doesn't seem to remove it if it's there, but won't add it for me, so that may be the way forward here - to remove it so that going forward it doesn't cause a problem.
Have you visited our Help Center?
SQL Compare can't currently and shouldn't really be modifying the code without being told to do so. I suppose it could be an option to specify, but I don't think it would meet the threshold to make it as a new option (not enough people needing it essentially since they don't want to crowd the number of options there).
Take care!
Have you visited our Help Center?
Or we could just not include the computed column in the temp table, and then when it was renamed, we could also add the computed column back in using the appropriate table name prefix - though that wouldn't work if "force column order" was on and it was in the middle of the table (otherwise we're back to rebuilding the table again).
Having said all that, it looks like this doesn't even work in SSMS. It just throws this warning when the columns in the computed column are qualified by the table:
If I remove [Products] qualification it then creates a similar script as Compare with the temp table and copying over data and renaming.
So I think that may be the best thing to do here. I'll feed that back to the developers and see what they think.
Have you visited our Help Center?
I'll see where best we could put a mention of this being an issue and try to get that updated.
Have you visited our Help Center?
Have you visited our Help Center?