Smart Rename fails when column participates in check
fabianc2k
Posts: 8
A check constraint on a table references a function, which references the same table.
In particular, the function references the column being renamed.
SQL Refactor scripted the change to the function before the change to the table, resulting in a fail.
Modifying the produced change script so that the function alteration occurs after the column rename makes the script function without problem.
In particular, the function references the column being renamed.
SQL Refactor scripted the change to the function before the change to the table, resulting in a fail.
Modifying the produced change script so that the function alteration occurs after the column rename makes the script function without problem.
Comments
Many Thanks
Eddie Davis
Red Gate Software Ltd
Technical Support
E-Mail: support@red-gate.com
/*
Script created by SQL Refactor version 1.1.0.97 from Red Gate Software Ltd at 30/07/2007 16:43:50
Run this script on Sample to perform the Smart Rename refactoring.
Please back up your database before running this script.
*/
-- Summary for the smart rename:
--
-- Action Plan:
-- Drop check constraint [order_check] from table [dbo].[Sales]
-- Alter table [dbo].[Sales]
-- Alter function [dbo].[customer_orders]
-- Add constraint [order_check] to [dbo].[Sales]
--
-- No warnings
SET NUMERIC_ROUNDABORT OFF
GO
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors')) DROP TABLE #tmpErrors
GO
CREATE TABLE #tmpErrors (Error int)
GO
SET XACT_ABORT ON
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
BEGIN TRANSACTION
GO
PRINT N'Dropping constraints from [dbo].[Sales]'
GO
ALTER TABLE [dbo].[Sales] DROP CONSTRAINT [order_check]
GO
@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[Sales]'
GO
sp_rename N'[dbo].[Sales].[Ship_Date]', N'Shipping_Date', 'COLUMN'
GO
@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[customer_orders]'
GO
ALTER FUNCTION dbo.customer_orders(@pr_number INT)
RETURNS TABLE
AS RETURN(SELECT Shipping_Date AS Ship_Date
FROM dbo.customer, dbo.Sales
WHERE customer.cust_no = Sales.cust_no
AND Order_no = @pr_number)
GO
@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Adding constraints to [dbo].[Sales]'
GO
ALTER TABLE [dbo].[Sales] ADD CONSTRAINT [order_check] CHECK (([Order_Date]<=Shipping_Date))
GO
@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION
GO
@TRANCOUNT>0 BEGIN
PRINT 'The database update succeeded'
COMMIT TRANSACTION
END
ELSE PRINT 'The database update failed'
GO
DROP TABLE #tmpErrors
GO
Senior Product Support Engineer
Redgate Software Ltd
Email: support@red-gate.com
1. Run create table / function script below
2. Attempt to use smart rename on a column ColumnTwo
[/code]
Using your code I reproduced the reported fault symptoms, where the function is incorrectly altered before the table.
As per your orignal post, changing the dependency order so that the table is altered before function resolves the issue.
I will feed this information onto our Development and wait for the outcome of their investigations.
Many Thanks
Eddie
Eddie davis
Red Gate Software Ltd
Technical Support
Senior Product Support Engineer
Redgate Software Ltd
Email: support@red-gate.com