I have an existing database (SSDT project as source) where I have to update a table in. I'm only changing a column type from nvarchar(MAX)
to nvachar(256)
. The problem is, that I have an existing view bound to this table with SCHEMABINDING
. I'm not able to edit table column because SCHEMABINDING
prevent changes which influences the VIEW.
The following script is executed in the PreDeploymentScript. When I call the DROP VIEW [base].[VIEW_DEPENDING_ON_TABLE]
statement, the view is missing after deployment. My idea is, to disable SCHEMABINDING
during deployment and enable it after finished. How is this possible in TSQL script? Or is there a better way to do this?
IF EXISTS ( SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = N'base'
AND TABLE_NAME = 'TABLENAME'
AND COLUMN_NAME = 'Instance'
AND CHARACTER_MAXIMUM_LENGTH = -1)
AND NOT EXISTS (SELECT * FROM sys.indexes WHERE name='IX_TABLENAME_Instance' AND object_id = OBJECT_ID(N'[base].[TABLENAME]'))
BEGIN
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[base].[VIEW_DEPENDING_ON_TABLE]') AND type in (N'V'))
BEGIN
DROP VIEW [base].[VIEW_DEPENDING_ON_TABLE]
END
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[base].[TABLENAME]') AND type in (N'U'))
BEGIN
UPDATE [base].[TABLENAME] SET [Instance] = LEFT(Instance, 256)
ALTER TABLE [base].[TABLENAME] ALTER COLUMN [Instance] NVARCHAR(256)
END
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[base].[TABLENAME]') AND type in (N'U'))
BEGIN
CREATE NONCLUSTERED INDEX [IX_TABLENAME_Instance] ON [base].[TABLENAME]
(
[Instance] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
END
END
GO
ALTER VIEW dbo.name AS ...
, then make your change, thenALTER VIEW dbo.name WITH SCHEMABINDING AS ...
– FarmhandALTER VIEW dbo.name AS ...
? How to disableSCHEMABINDING
? – ExternalityWITH SCHEMABINDING
option. There is no command to turn it off, you just change the view and don't turn it on. – Farmhandsys.sql_modules
, so you could access it programmatically. But you could also consider copying the view definition into this script and removing the schemabinding option yourself. Or you could call that other T-SQL script after deployment. – Farmhandsys.sql_modules
. Could you post this as answer? Thank you! – Externality