DECLARE @Len VARCHAR(2)
DECLARE @NameType VARCHAR(10)
DECLARE @sqlCommand VARCHAR(100)

DELETE FROM dbo.Over_Under_Row WHERE NOT EXISTS(SELECT * FROM dbo.Havaleh WHERE Autoid = dbo.Over_Under_Row.AutoId)

SELECT @NameType =
Case
  WHEN t.name = 'U_Id' THEN 'Bigint'
  WHEN t.name = 'Numeric' THEN t.name
END , @len = 
    c.precision
FROM    
    sys.columns c
INNER JOIN 
    sys.types t ON c.user_type_id = t.user_type_id
LEFT OUTER JOIN 
    sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
LEFT OUTER JOIN 
    sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
WHERE
    c.object_id = OBJECT_ID('Havaleh') AND c.name = 'AutoId'
IF @NameType = 'Numeric' 
EXEC('ALTER TABLE dbo.Over_Under_Row ALTER COLUMN Autoid ' + @NameType + '(' + @Len + ',0) Null')
ELSE
  EXEC('ALTER TABLE dbo.Over_Under_Row ALTER COLUMN Autoid ' + @NameType )


IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_Over_Under_Row_Havaleh]') AND parent_object_id = OBJECT_ID(N'[dbo].[Over_Under_Row]'))
ALTER TABLE [dbo].[Over_Under_Row]  WITH CHECK ADD  CONSTRAINT [FK_Over_Under_Row_Havaleh] FOREIGN KEY([AutoId])
REFERENCES [dbo].[Havaleh] ([AutoId])
ON DELETE CASCADE