BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
go
ALTER TABLE dbo.GridLayOutFilterTbl ADD
	FK_FilterId int NULL
go
INSERT INTO GridFilterTitleTbl
					  (LayoutId, DisplayText)
SELECT     LayoutId, MAX(DisplayName) AS DisplayName
FROM         GridLayOutFilterTbl
GROUP BY LayoutId, DisplayName
go
update GridLayOutFilterTbl set FK_FilterId=(select top(1) syscode from GridFilterTitleTbl where layoutid=GridLayOutFilterTbl.layoutid and [DisplayText]=GridLayOutFilterTbl.displayname )
go
ALTER TABLE dbo.GridLayOutFilterTbl ADD CONSTRAINT
	PK_GridLayOutFilterTbl PRIMARY KEY CLUSTERED 
	(
	SysCode
	) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
go
ALTER TABLE dbo.GridLayOutFilterTbl ADD CONSTRAINT
	FK_GridLayOutFilterTbl_GridFilterTitleTbl FOREIGN KEY
	(
	FK_FilterId
	) REFERENCES dbo.GridFilterTitleTbl
	(
	SysCode
	) ON UPDATE  NO ACTION 
	 ON DELETE  CASCADE 
go
ALTER TABLE dbo.GridLayOutFilterTbl
	DROP CONSTRAINT FK_GridLayOutFilterTbl_GridLayouts
go
ALTER TABLE dbo.GridLayOutFilterTbl
	DROP COLUMN LayoutId, DisplayText, DisplayName
go
COMMIT
go

--use master