首页 > 代码库 > Coding for removing servername from the views.

Coding for removing servername from the views.

Coding for removing servername from the views.

 

--The following script is to find out the existing views of the database machine‘s display

 

USE Database;

SELECT

b.name,

c.referenced_server_name,

c.referenced_database_name,

c.referenced_schema_name,

a.definition,

b.create_date,

b.modify_date

FROM sys.sql_modules AS a

JOIN

sys.objects AS b

ON a.object_id = b.object_id

JOIN

sys.sql_expression_dependencies AS c

ON b.object_id = c.referencing_id

WHERE b.type = ‘V‘

AND c.referenced_server_name IS NOT NULL

 

--The following script is to modify the database view exists after the replacement of the processing machine

 

 

SET NOCOUNT ON;

DECLARE @definition VARCHAR(MAX)

DECLARE @ServerN VARCHAR(50)

DECLARE View_Cursor CURSOR SCROLL

FOR (

SELECT

c.referenced_server_name,

a.definition

FROM sys.sql_modules AS a

JOIN

sys.objects AS b

ON a.object_id = b.object_id

JOIN

sys.sql_expression_dependencies AS c

ON b.object_id = c.referencing_id

WHERE b.type = ‘V‘

AND c.referenced_server_name IS NOT NULL

)

OPEN View_Cursor;

FETCH NEXT FROM View_Cursor INTO @ServerN,@definition;

WHILE @@fetch_status = 0

BEGIN

SELECT @definition = REPLACE (@definition,‘CREATE VIEW‘,‘ALTER VIEW‘)

SELECT @definition = REPLACE (@definition,‘[‘+@ServerN+‘]‘+‘.‘,‘‘)

--print(@definition);

exec(@definition);

FETCH NEXT FROM View_Cursor INTO @ServerN,@definition;

END

CLOSE View_Cursor;

DEALLOCATE View_Cursor;

GO

 

 

 

Coding for removing servername from the views.