Question

I have a table dnn_Files with user-defined function column Folder:

CREATE TABLE [dnn_Files](
    ...
    [Folder]  AS ([GetFileFolderFunc]([FolderID])),
    ...
...

The database was created by dotnetnuke installation and was used. So, it's not empty.

My true goal is to add "dnn_" object qualifier to the database (original database has empty qualifier).

To archive that I renamed all dnn tables, deleted stored procedures, user-defined functions(except GetFileFolderFunc - it can't be deleted because it is being referenced by dnn_Files ), views and recreate it with right object qualifier.

After that I get the table as described above. The table has right qualifier while GetFileFolderFunc hasn't. As tables was renamed GetFileFolderFunc now refers to a nonexistent table. The proper twin with qualifier(dnn_GetFileFolderFunc) is in the database.

So, I want the dnn_Files to use the new dnn_GetFileFolderFunc function.

When I try to change the Folder column (by changing Computed Column Specification through Sql Management studio designer) it fails with error "Saving changes is not permitted. The changes you have made require dnn_Files to be dropper and re-created ...". As dnn_Files has many relations it can't be dropeed. Seems logical.

Then I try to change GetFileFolderFunc's body to use tables with new object qualifier, but no luck either. The error is "Cannot ALTER 'dbo.GetFileFolderFunc' because it is being referenced by object 'dnn_Files'".

Are there any workarounds on first or second restrictions?

Sql server version: 11.0.2100.60

Était-ce utile?

La solution

ALTER TABLE dbo.dnn_Files  DROP COLUMN Folder;
GO
ALTER TABLE dbo.dnn_Files ADD Folder AS ([dbo].[dnn_GetFileFolderFunc]([FolderID]));

You are welcome @mt_serg. If you want to give me credit, just accept this answer. I was just a little too busy with some other things to write a good, full answer.

Autres conseils

L_7337 was right, it's just a matter of using SSMS UI.

Sql altering:

ALTER TABLE dbo.dnn_Files  DROP COLUMN Folder;
GO
ALTER TABLE dbo.dnn_Files ADD Folder AS ([dbo].[dnn_GetFileFolderFunc]([FolderID]));

works fine. Thanks a lot!

Before I saw the comment I solved the problem another way. So, I decided to answer my own question because it can be useful for somebody.

I did the trick through bacpac. After unzipping (bacpac is just a zip) I modified the model.xml. Related part here:

<Element Type="SqlComputedColumn" Name="[dbo].[dnn_Files].[Folder]">
  <Property Name="ExpressionScript">
    <Value><![CDATA[([dbo].[dnn_GetFileFolderFunc]([FolderID]))]]></Value>
  </Property>
  <Relationship Name="ExpressionDependencies">
    <Entry>
      <References Name="[dbo].[dnn_GetFileFolderFunc]" />
    </Entry>
    <Entry>
      <References Name="[dbo].[dnn_Files].[FolderID]" />
    </Entry>
  </Relationship>
</Element>

Then zip back and use DacChkSum.exe to calculate checksum.

>dacchksum /InputFilename:database.bacpac
Model checksum
Stored:   :[5745242756E4D7CAD12C913058A1078BC1A5FA5A069C7CC0CF417719F2A64196]
Calculated:[A2B39E19E2219E05B3B4F8DC95A1B764B7A91ABBA9C70D18EAA4247B76120BB0]

After changing checksum in Origin.xml I just restored the database.

It seems like to fire a cannon at sparrows. But there is one benefit, the column keeps its order while after altering it becomes the latest. I don't expect any side effect in altering case, but after bacpac trick database looks more consistent.

  1. Script 'dbo.GetFileFolderFunc’ as create to a new tab. Change the function name and run script.
  2. Alter the dnn_files table to reference the new function.
  3. Alter the original function.
  4. Alter the dnn_files table to reference the original function that has now been altered.
  5. Drop the new function that is no longer referenced by anything.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top