DECLARE @sql NVARCHAR(MAX) = N'';
declare @FNY_NAME nvarchar(200)

	DECLARE POP CURSOR for
	       SELECT 'Orginal'
	       UNION ALL
		   select FNY_Name  from Common.FinancialYear  where ISNULL(FNY_IsDeleted ,0) = 0
	OPEN POP
	FETCH NEXT FROM POP INTO @FNY_NAME
	WHILE @@FETCH_STATUS =0
	BEGIN 

SELECT @sql =  '
------------------------------
---- Schema: ' + @FNY_NAME + '
------------------------------

DECLARE @defname NVARCHAR(200);

-- پیدا کردن نام Default Constraint
SELECT @defname = d.name
FROM sys.default_constraints d
JOIN sys.columns c 
    ON d.parent_object_id = c.object_id 
   AND d.parent_column_id = c.column_id
WHERE d.parent_object_id = OBJECT_ID(''' + QUOTENAME(@FNY_NAME) + '.FactorScale'')
  AND c.name = ''FCS_FactorSerialNumber'';

-- حذف Default Constraint اگر وجود داشته باشد
IF @defname IS NOT NULL
BEGIN
 --   PRINT ''Dropping constraint '' + @defname + '' in schema ' + @FNY_NAME + ''';
    EXEC(''ALTER TABLE ' + QUOTENAME(@FNY_NAME) + '.FactorScale DROP CONSTRAINT '' + @defname);
END

-- تغییر نوع ستون
--PRINT ''Altering column in schema ' +@FNY_NAME + ''';
EXEC(''
ALTER TABLE ' + QUOTENAME(@FNY_NAME) + '.FactorScale
ALTER COLUMN FCS_FactorSerialNumber [Common].[_BIGINT_] NULL
'');

-- بازگرداندن Default Constraint
--PRINT ''Re-adding default constraint in schema ' + @FNY_NAME + ''';
EXEC(''
ALTER TABLE ' + QUOTENAME(@FNY_NAME) + '.FactorScale
ADD CONSTRAINT DF_FactorScale_FCS_FactorSerialNumber
DEFAULT (0) FOR FCS_FactorSerialNumber
'');
'
FROM sys.schemas s
WHERE OBJECT_ID(QUOTENAME(@FNY_NAME) + '.FactorScale') IS NOT NULL;

-- چاپ کل اسکریپت برای بررسی (اختیاری)
--PRINT @sql;

-- اجرای نهایی پس از بررسی
 EXEC sp_executesql @sql;

	   FETCH NEXT FROM POP INTO @FNY_NAME
    END
    CLOSE POP
    DEALLOCATE POP	  