Bug found? Cannot alter function referenced by tablehI,  I generated a schema compare script and when I executed the script I got this error, which is:<br/> <br/> The system is trying to alter a function, but that function is used by a table, it seems the script is not correctly generated, as far as I know it should alter first the function and then alter the column of the table that uses that function<br/> <br/> Or maybe this is not a bug from the product and its happening by something else?<br/> <br/> <br/> Msg 3729, Level 16, State 3, Procedure CalcularCaudalPromedioLADCaudalesLodosActivados, Line 18<br/> Cannot ALTER 'ROP.CalcularCaudalPromedioLADCaudalesLodosActivados' because it is being referenced by object 'CaudalesLodosActivados'.<hr class="sig">MCPD ENTERPRISE APPLICATIONS DEVELOPER http://wantmvp.blogspot.com/© 2009 Microsoft Corporation. All rights reserved.Thu, 08 Oct 2009 18:58:03 Z9158f260-64ec-4d2b-9646-8a863ee90818http://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/9158f260-64ec-4d2b-9646-8a863ee90818#9158f260-64ec-4d2b-9646-8a863ee90818http://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/9158f260-64ec-4d2b-9646-8a863ee90818#9158f260-64ec-4d2b-9646-8a863ee90818Luis Esteban Valencia Muñozhttp://social.msdn.microsoft.com/Profile/en-US/?user=Luis%20Esteban%20Valencia%20Mu%u00f1ozBug found? Cannot alter function referenced by tablehI,  I generated a schema compare script and when I executed the script I got this error, which is:<br/> <br/> The system is trying to alter a function, but that function is used by a table, it seems the script is not correctly generated, as far as I know it should alter first the function and then alter the column of the table that uses that function<br/> <br/> Or maybe this is not a bug from the product and its happening by something else?<br/> <br/> <br/> Msg 3729, Level 16, State 3, Procedure CalcularCaudalPromedioLADCaudalesLodosActivados, Line 18<br/> Cannot ALTER 'ROP.CalcularCaudalPromedioLADCaudalesLodosActivados' because it is being referenced by object 'CaudalesLodosActivados'.<hr class="sig">MCPD ENTERPRISE APPLICATIONS DEVELOPER http://wantmvp.blogspot.com/Tue, 30 Jun 2009 16:18:36 Z2009-07-22T00:20:46Zhttp://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/9158f260-64ec-4d2b-9646-8a863ee90818#05c3dabd-5c88-41c8-9f74-b125f51d012ahttp://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/9158f260-64ec-4d2b-9646-8a863ee90818#05c3dabd-5c88-41c8-9f74-b125f51d012aGert Drapershttp://social.msdn.microsoft.com/Profile/en-US/?user=Gert%20DrapersBug found? Cannot alter function referenced by tableIf you are using the GDR release make sure to uncheck the Schema Compare option &quot;Enforce only minimal dependencies&quot;.<br/><hr class="sig">GertD @ www.DBProj.com Tue, 30 Jun 2009 19:07:53 Z2009-06-30T19:07:53Zhttp://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/9158f260-64ec-4d2b-9646-8a863ee90818#cef2fdec-6286-40cf-8b9b-b828e7fb9618http://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/9158f260-64ec-4d2b-9646-8a863ee90818#cef2fdec-6286-40cf-8b9b-b828e7fb9618Luis Esteban Valencia Muñozhttp://social.msdn.microsoft.com/Profile/en-US/?user=Luis%20Esteban%20Valencia%20Mu%u00f1ozBug found? Cannot alter function referenced by tableIts unchecked.<br/> <br/> Let me explain you what I have found in detail<br/> <br/> We have these errors:<br/> <br/> Msg 207, Level 16, State 1, Procedure CalcularMasaDesecharCaudalesLodosActivados, Line 8<br/> Invalid column name 'totalMasaEnReactores'<br/> <br/> The script of the function is:<br/> <br/> ALTER FUNCTION [ROP].[CalcularMasaDesecharCaudalesLodosActivados]<br/> (@idCaudalLodoActivado DECIMAL (30, 4))<br/> RETURNS INT<br/> AS<br/> BEGIN<br/>     declare @masaDesechar decimal(30,4)<br/> <br/>     select @masaDesechar = <strong>totalMasaEnReactores </strong> / edadLodos<br/>     from [ROP].CaudalesLodosActivados<br/>     where idCaudalLodoActivado = @idCaudalLodoActivado    <br/>     return @masaDesechar<br/> END<br/> <br/> The field in bold is not being found, so I made a search and I found that its dropping that column in the script:<br/> <br/> ALTER TABLE [ROP].[CaudalesLodosActivados] DROP COLUMN [totalMasaEnReactores];<br/> <br/> I have more or less 10 errors like this one, and all seems to be by the same thing, the columns are being deleted and then the function is being altered.<br/> <br/> <br/> <br/> I also found this error, it seems that its something for renaming tables.<br/> <br/> Cannot find either column &quot;ROP&quot; or the user-defined function or aggregate &quot;ROP.CalcularEdadLodosRealCaudalesLodosActivados&quot;, or the name is ambiguous.<br/> <br/> <br/> BEGIN TRANSACTION;<br/> <br/> CREATE TABLE [ROP].[tmp_ms_xx_CaudalesLodosActivados] (<br/>     [idCaudalLodoActivado]              INT            IDENTITY (1, 1) NOT NULL,<br/>     [idCaudalLodoActivadoEncabezado]    INT            NOT NULL,<br/>     [hora]                              TINYINT        NOT NULL,<br/>     [totalMasaEnReactores]              AS             ([ROP].[CalcularTotalMasaEnReactoresCaudalesLodosActivados]([idCaudalLodoActivado])),<br/>     [solidosPromedioSedimentacionFinal] AS             ([ROP].[CalcularSolidosPromedioSedimentacionFinalCaudalesLodosActivados]([idCaudalLodoActivado])),<br/>     [qQ]                                AS             ([ROP].[CalcularqDCaudalesLodosActivados]([idCaudalLodoActivado])),<br/>     [edadLodos]                         SMALLINT       NOT NULL,<br/>     [caudalPromedioLAD]                 AS             ([ROP].[CalcularCaudalPromedioLADCaudalesLodosActivados]([idCaudalLodoActivado])),<br/>     [concentracionLAR]                  AS             ([ROP].[CalcularConcentracionLARCaudalesLodosActivados]([idCaudalLodoActivado])),<br/>     [edadLodosReal]                     AS             ([ROP].[CalcularEdadLodosRealCaudalesLodosActivados]([idCaudalLodoActivado])),<br/>     [masaDesechar]                      AS             ([ROP].[CalcularMasaDesecharCaudalesLodosActivados]([idCaudalLodoActivado])),<br/>     [caudalBombaTFP]                    AS             ([ROP].[CalcularCaudalBombaTFPCaudalesLodosActivados]([idCaudalLodoActivado])),<br/>     [horasBombear]                      DECIMAL (6, 2) NOT NULL,<br/>     [usuario]                           VARCHAR (50)   NOT NULL<br/> );<br/> <br/> ALTER TABLE [ROP].[tmp_ms_xx_CaudalesLodosActivados]<br/>     ADD CONSTRAINT [tmp_ms_xx_clusteredindex_PK_CaudalesLodosActivados] PRIMARY KEY CLUSTERED ([idCaudalLodoActivado] ASC) WITH (ALLOW_PAGE_LOCKS = ON, ALLOW_ROW_LOCKS = ON, PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF);<br/> <br/> IF EXISTS (SELECT TOP 1 1<br/>            FROM   [ROP].[CaudalesLodosActivados])<br/>     BEGIN<br/>         SET IDENTITY_INSERT [ROP].[tmp_ms_xx_CaudalesLodosActivados] ON;<br/>         INSERT INTO [ROP].[tmp_ms_xx_CaudalesLodosActivados] ([idCaudalLodoActivado], [idCaudalLodoActivadoEncabezado], [hora], [edadLodos], [horasBombear], [usuario])<br/>         SELECT   [idCaudalLodoActivado],<br/>                  [idCaudalLodoActivadoEncabezado],<br/>                  [hora],<br/>                  [edadLodos],<br/>                  [horasBombear],<br/>                  [usuario]<br/>         FROM     [ROP].[CaudalesLodosActivados]<br/>         ORDER BY [idCaudalLodoActivado] ASC;<br/>         SET IDENTITY_INSERT [ROP].[tmp_ms_xx_CaudalesLodosActivados] OFF;<br/>     END<br/> <br/> DROP TABLE [ROP].[CaudalesLodosActivados];<br/> <br/> EXECUTE sp_rename N'[ROP].[tmp_ms_xx_CaudalesLodosActivados]', N'CaudalesLodosActivados';<br/> <br/> EXECUTE sp_rename N'[ROP].[tmp_ms_xx_clusteredindex_PK_CaudalesLodosActivados]', N'PK_CaudalesLodosActivados', N'OBJECT';<br/> <br/> COMMIT TRANSACTION;<br/> <br/><hr class="sig">MCPD ENTERPRISE APPLICATIONS DEVELOPER http://wantmvp.blogspot.com/Tue, 30 Jun 2009 19:23:17 Z2009-06-30T19:23:17Zhttp://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/9158f260-64ec-4d2b-9646-8a863ee90818#8a8dd6a4-8090-4776-a995-7809206e5de0http://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/9158f260-64ec-4d2b-9646-8a863ee90818#8a8dd6a4-8090-4776-a995-7809206e5de0Luis Esteban Valencia Muñozhttp://social.msdn.microsoft.com/Profile/en-US/?user=Luis%20Esteban%20Valencia%20Mu%u00f1ozBug found? Cannot alter function referenced by tableI found that after the script REMOVES the column, it doesnt add it again anywhere else.  <br/> <br/> <br/><hr class="sig">MCPD ENTERPRISE APPLICATIONS DEVELOPER http://wantmvp.blogspot.com/Tue, 30 Jun 2009 19:54:38 Z2009-06-30T19:54:38Zhttp://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/9158f260-64ec-4d2b-9646-8a863ee90818#27534706-a0cf-4e10-91f6-1279c1fd586bhttp://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/9158f260-64ec-4d2b-9646-8a863ee90818#27534706-a0cf-4e10-91f6-1279c1fd586bGenevieve Orchardhttp://social.msdn.microsoft.com/Profile/en-US/?user=Genevieve%20OrchardBug found? Cannot alter function referenced by table<p>Luis,<br/><br/>This sounds like it might be a bug. A few questions for you:<br/><br/>* which version of Visual Studio Database Edition do you have?</p> <p>* are you excluding any object types from the schema comparison (either manually, or through schema compare options)?</p> <p>* are you comparing a database project to a database?<br/><br/>* are there a lot of differences between the source and target, and if not, can you list them here?<br/><br/>Hopefully we can help you through this.</p><hr class="sig">-Genevieve Orchard (VSTS Database Edition Test Team)Wed, 01 Jul 2009 17:10:22 Z2009-07-01T17:10:22Zhttp://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/9158f260-64ec-4d2b-9646-8a863ee90818#7508c978-5d6a-463c-9a8c-be5790da560fhttp://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/9158f260-64ec-4d2b-9646-8a863ee90818#7508c978-5d6a-463c-9a8c-be5790da560fLuis Esteban Valencia Muñozhttp://social.msdn.microsoft.com/Profile/en-US/?user=Luis%20Esteban%20Valencia%20Mu%u00f1ozBug found? Cannot alter function referenced by tableHello<br/><br/>1. I am using the latest version. GDR RC I think<br/>2. Nop, I am using all default options.<br/>3. Yes, what I do is: create a database project, then import a database, then on project properties I set the target database, then I build and deploy and it generates a long script.<br/>4.  There are many differences but the main problem I have detected is because of this:<br/><br/>We have tables with computed columns, those computed columns use a function.  That function changed.  So the script generated a DROP COLUMN.  Then it generated an strange script in which it creates a temporal table, and then it drops the old table and rename the tempo one to the name it should have.<br/><br/>I took yesterday 4 hours, splitting the generated script into pieces until I found the error: and I had to fix it this way.<br/><br/>DROP the table, Alter the function. create the temp table, and then rename it.<br/><br/>Oh, another thing I found, its that some of the functions used in its computations columns from the same table that the script had already removed in the first steps of the script.<br/><br/>This is one part of the script that I modified to make it work.  However you must know that those tables were empty, so I hadnt to do any additional steps.<br/>The generated scripts of the product shoud do in this case:<br/><br/>1. Create a temporary table<br/>2. Copy the data from the table to the temporary table.<br/>3. Drop the table<br/>4. Create or alter the functions that dont depend on other columns of the same table.<br/>5.  Alter the functions that depend on the same table<br/>6. Rename the temporary the table to the name it shoud have.<br/><br/>Just my toughts, maybe I amw ron in step 5, because it might be confusing.<br/><br/><br/><br/> <pre lang=x-sql>GO SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; SET XACT_ABORT ON; drop table [rop].ConsumoSal GO ALTER FUNCTION [ROP].[CalcularconsumoNaClTotalConsumoSal] (@idConsumoSal DECIMAL (30, 2)) RETURNS BIGINT AS BEGIN DECLARE @resultado decimal(30,2) SELECT @resultado = isnull(consumoNaClBT1,0) + isnull(consumoNaClBT2,0) FROM ConsumoSal WHERE idConsumoSal =@idConsumoSal return @resultado END GO GO BEGIN TRANSACTION; CREATE TABLE [ROP].[tmp_ms_xx_ConsumoSal] ( [idConsumoSal] INT IDENTITY (1, 1) NOT NULL, [idPlanta] INT NULL, [NroBomba] INT NULL, [PorcentajeBP] DECIMAL (18) NULL, [PorcentajeStroke] AS ([ROP].CalcularPorcentajeStrokeConsumoSal([idConsumoSal])), [FlujoBP] AS ([ROP].[CalcularFlujoBPConsumoSal]([idConsumoSal])), [InicioFechaInicial] DATETIME NULL, [IniciohoraInicial] VARCHAR (50) NULL, [FinFechaFinal] DATETIME NULL, [FinHoraFinal] VARCHAR (5) NULL, [TiempoHoras] AS (datediff(minute,[inicioFechaInicial],isnull([finFechaFinal],getdate()))/(60.0)), [ConcentracionBT1] DECIMAL (18) NULL, [ConsumoNaClBT1] AS ([ROP].[CalcularConsumoNaClBT1ConsumoSal]([idConsumoSal])), [ConcentracionBT2] DECIMAL (18) NULL, [ConsumoNaClBT2] AS ([ROP].[CalcularConsumoNaClBT2ConsumoSal]([idConsumoSal])), [ConsumoNaClTotal] AS ([ROP].[CalcularconsumoNaClTotalConsumoSal]([idConsumoSal])), [ConcentracionInicialSRP] DECIMAL (18) NULL, [ConcentracionFinalSRP] DECIMAL (18) NULL, [usuario] VARCHAR (50) NULL ); ALTER TABLE [ROP].[tmp_ms_xx_ConsumoSal] ADD CONSTRAINT [tmp_ms_xx_clusteredindex_PK_ConsumoSal] PRIMARY KEY CLUSTERED ([idConsumoSal] ASC) WITH (ALLOW_PAGE_LOCKS = ON, ALLOW_ROW_LOCKS = ON, PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF); --IF EXISTS (SELECT TOP 1 1 -- FROM [ROP].[ConsumoSal]) -- BEGIN -- SET IDENTITY_INSERT [ROP].[tmp_ms_xx_ConsumoSal] ON; -- INSERT INTO [ROP].[tmp_ms_xx_ConsumoSal] ([idConsumoSal], [idPlanta], [nroBomba], [PorcentajeBP], [inicioFechaInicial], [inicioHoraInicial], [finFechaFinal], [finHoraFinal], [concentracionBT1], [concentracionBT2], [ConcentracionInicialSRP], [ConcentracionFinalSRP], [usuario]) -- SELECT [idConsumoSal], -- [idPlanta], -- [nroBomba], -- [PorcentajeBP], -- [inicioFechaInicial], -- [inicioHoraInicial], -- [finFechaFinal], -- [finHoraFinal], -- [concentracionBT1], -- [concentracionBT2], -- [ConcentracionInicialSRP], -- [ConcentracionFinalSRP], -- [usuario] -- FROM [ROP].[ConsumoSal] -- ORDER BY [idConsumoSal] ASC; -- SET IDENTITY_INSERT [ROP].[tmp_ms_xx_ConsumoSal] OFF; -- END --DROP TABLE [ROP].[ConsumoSal]; EXECUTE sp_rename N'[ROP].[tmp_ms_xx_ConsumoSal]', N'ConsumoSal'; EXECUTE sp_rename N'[ROP].[tmp_ms_xx_clusteredindex_PK_ConsumoSal]', N'PK_ConsumoSal', N'OBJECT'; COMMIT TRANSACTION; -- Fin Parte6 --</pre> <br/><br/><br/>Please contact me via email and I can send you the full script, and I can even send you the 2 databases compared, I am sure with my help we can make this error to be solved for the next release.<br/><br/>lvalencia at intergrupo dot com<br/>or<br/>le.valencia at hotmail dot com<br/><br/><br/><br/><hr class="sig">MCPD ENTERPRISE APPLICATIONS DEVELOPER http://wantmvp.blogspot.com/Wed, 01 Jul 2009 22:24:43 Z2009-07-01T22:24:43Zhttp://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/9158f260-64ec-4d2b-9646-8a863ee90818#fd146b55-0ff5-463f-944d-ddd23d3ecaefhttp://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/9158f260-64ec-4d2b-9646-8a863ee90818#fd146b55-0ff5-463f-944d-ddd23d3ecaefGenevieve Orchardhttp://social.msdn.microsoft.com/Profile/en-US/?user=Genevieve%20OrchardBug found? Cannot alter function referenced by tableI will follow up with Luis offline.<hr class="sig">-Genevieve Orchard (VSTS Database Edition Test Team)Thu, 02 Jul 2009 23:20:29 Z2009-07-02T23:20:29Zhttp://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/9158f260-64ec-4d2b-9646-8a863ee90818#ae0acb6f-89b8-417d-bb52-3ffd8fbb33cahttp://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/9158f260-64ec-4d2b-9646-8a863ee90818#ae0acb6f-89b8-417d-bb52-3ffd8fbb33caepnlarryhttp://social.msdn.microsoft.com/Profile/en-US/?user=epnlarryBug found? Cannot alter function referenced by tableYou probably already resolved this, but I found a simple solution and others might find it useful if not obvious.&nbsp; <br /><br />Create an SQL function that calls your original function very simply, for example, let's say you had a function that converted numbers to linguistic text named "NumToText", as I had when I stumbled upon this situation, and this function was used as a&nbsp;formula for a&nbsp;computed column specification for field tbl1.myNumToTextFld.&nbsp; Now you can't modify NumToText without messing with any table/field elements that are referring to it.&nbsp; Instead of having to mess with that,&nbsp;have myNumToTextFld refer to the new "NumToText_Ref" function, which simply calls NumToText.&nbsp; Now you can modify NumToText all you wish and&nbsp;with complaints since it's not directly referenced by the computed column function spec.<br /><br />The formula for the field myNumToTextFld would look like:<br />([dbo].[NumToText_Ref](isnull([myNumFld],(0))))<br /><br />And you create a new function<br /><br /><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;"><font size="2" color="#0000ff"><font size="2" color="#0000ff"> <p>ALTER</p> </font></font></span><font size="2" color="#0000ff"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small;"> </span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">FUNCTION</span></span><span style="font-size: x-small;"> [dbo]</span><span style="font-size: x-small; color: #808080;"><span style="font-size: x-small; color: #808080;">.</span></span><span style="font-size: x-small;">[NumToText_Ref] </span><span style="font-size: x-small; color: #808080;"><span style="font-size: x-small; color: #808080;">(</span></span><span style="font-size: x-small;">@num </span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">decimal</span></span><span style="font-size: x-small; color: #808080;"><span style="font-size: x-small; color: #808080;">(</span></span><span style="font-size: x-small;">18</span><span style="font-size: x-small; color: #808080;"><span style="font-size: x-small; color: #808080;">,</span></span><span style="font-size: x-small;">7</span><span style="font-size: x-small; color: #808080;"><span style="font-size: x-small; color: #808080;">))</span></span></p> <span style="font-size: x-small; color: #008000;"><span style="font-size: x-small; color: #008000;"> <p>/* exists solely so it can be referenced by a calculated field in table&nbsp;myTbl and the underlying functions can be recompiled</p> <p>without having to drop table columns */</p> </span></span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;"><font size="2" color="#0000ff"><font size="2" color="#0000ff"> <p>RETURNS</p> </font></font></span><font size="2" color="#0000ff"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small;"> </span><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">nchar</span></span><span style="font-size: x-small; color: #808080;"><span style="font-size: x-small; color: #808080;">(</span></span><span style="font-size: x-small;">256</span><span style="font-size: x-small; color: #808080;"><span style="font-size: x-small; color: #808080;">)</span></span></p> <span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;"> <p>BEGIN</p> </span></span><span style="font-size: x-small;"><font size="2"> <p>&nbsp;</p> </font></span> <p><span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;">RETURN</span></span><span style="font-size: x-small;"> dbo</span><span style="font-size: x-small; color: #808080;"><span style="font-size: x-small; color: #808080;">.</span></span><span style="font-size: x-small;">NumToText</span><span style="font-size: x-small; color: #808080;"><span style="font-size: x-small; color: #808080;">(</span></span><span style="font-size: x-small;">@num</span><span style="font-size: x-small; color: #808080;"><span style="font-size: x-small; color: #808080;">) /* Now you can modify NumToText all you wish */</span></span></p> <span style="font-size: x-small; color: #0000ff;"><span style="font-size: x-small; color: #0000ff;"> <p>END</p> </span></span>Thu, 08 Oct 2009 18:58:03 Z2009-10-08T18:58:03Z