码迷,mamicode.com
首页 > 其他好文 > 详细

Coding for removing servername from the views.

时间:2014-10-22 09:55:53      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:style   color   io   os   ar   for   strong   sp   div   

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.

标签:style   color   io   os   ar   for   strong   sp   div   

原文地址:http://www.cnblogs.com/GeorgeYao/p/4042277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!