久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何獲得具有正確名稱的視圖的定義?

How can I get the definition of a view with the correct name?(如何獲得具有正確名稱的視圖的定義?)
本文介紹了如何獲得具有正確名稱的視圖的定義?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個包含多個視圖的數據庫,這些視圖過去已被用戶手動重命名.

如果我嘗試獲取視圖的定義以便我可以在其他地方編寫它的創建腳本,

有沒有辦法獲得視圖的正確定義,因為它將生成具有新名稱的視圖?或者,有沒有辦法提取視圖的幕后名稱,以便我可以在使用它之前將它replace 定義中的正確名稱?


真正令人討厭的是,如果我使用 GUI 將Script View as"創建到 >新的查詢編輯器窗口"它會生成正確的 CREATE 腳本,因此 SSMS 顯然可以通過某種方式訪問??此信息:

解決方案

有沒有辦法獲得視圖的正確定義,因為它將生成具有新名稱的視圖?

是的.使用 SMO(在代碼或 PowerShell 中)或 SSMS(使用 SMO)來編寫視圖腳本.SMO 是適用于所有 SQL Server 對象的全保真腳本引擎.

使用 Powershell,您可以輕松實現自動化,例如:

$sql = Get-SqlInstance -ServerInstance "localhost";$db = $sql |get-sqldatabase -Name "AdventureWorks2017";foreach($db.Views 中的 $v){[Microsoft.SqlServer.Management.Smo.View] $vv = $v寫主機 $vv.Script()}

從 Profiler 來看,這里沒有服務器端功能在起作用.SMO 獲取視圖定義,并有一個 TSQL 解析器,因此它可以在編寫對象腳本時修復 DDL.這是 SMO 運行以獲取視圖正文的查詢:

SELECT投擲(服務器屬性(N''服務器名'')AS sysname) AS [Server_Name],db_name() AS [Database_Name],SCHEMA_NAME(v.schema_id) AS [架構],v.name AS [名稱],CAST(ISNULL(OBJECTPROPERTYEX(v.object_id,N''ExecIsQuotedIdentOn''),0) AS bit) AS [QuotedIdentifierStatus],投擲(案件當 v.is_ms_shipped = 1 然后 1什么時候 (選擇主要_id從sys.extended_properties在哪里Major_id = v.object_id 和minor_id = 0 和類 = 1 和name = N''microsoft_database_tools_support'')不為空則為 1否則 0結尾AS 位) AS [IsSystemObject],CAST(ISNULL(OBJECTPROPERTYEX(v.object_id,N''ExecIsAnsiNullsOn''),0) AS bit) AS [AnsiNullsStatus],v.object_id AS [ID],NULL AS [文本],ISNULL(smv.definition, ssmv.definition) AS [定義]從sys.all_views AS v左外連接 sys.sql_modules AS smv ON smv.object_id = v.object_id左外連接 sys.system_sql_modules AS ssmv ON ssmv.object_id = v.object_id在哪里(v.type = @_msparam_0)and(v.name=@_msparam_1 and SCHEMA_NAME(v.schema_id)=@_msparam_2)訂購者[Database_Name] ASC,[Schema] ASC,[Name] ASC',N'@_msparam_0 nvarchar(4000),@_msparam_1 nvarchar(4000),@_msparam_2 nvarchar(4000)

I have a database with several views that have been manually renamed by users in the past.

If I try to get the definition of the view so that I can script its creation elsewhere, the name comes out wrong. I can get a list of databases with "wrong" names behind-the-scenes using:

SELECT OBJECT_NAME(object_id), definition
FROM sys.sql_modules
WHERE convert(nvarchar(200),definition) not like ('%'+OBJECT_NAME(object_id)+'%')

Is there any way to get the correct definition of the view, in that it will generate the view with the new name? Alternatively, is there a way to extract the behind-the-scenes name of the view so that I can replace it with the correct name in the definition before using it?


What's really annoying is that if I use the GUI to "Script View as > CREATE to > New Query Editor Window" it results in the correct CREATE script, so SSMS obviously has some way of getting access to this information:

解決方案

Is there any way to get the correct definition of the view, in that it will generate the view with the new name?

Yes. Use SMO (in code or PowerShell) or SSMS (which uses SMO) to script the view. SMO is the full-fidelity scripting engine for all SQL Server objects.

With Powershell you can automate this easilly, eg:

$sql = Get-SqlInstance -ServerInstance "localhost"
$db = $sql | get-sqldatabase -Name "AdventureWorks2017"
foreach ($v in $db.Views)
{
   [Microsoft.SqlServer.Management.Smo.View] $vv = $v
   write-host $vv.Script()
}

And from Profiler, there's no server-side functionality at work here. SMO fetches the view definition, and has a TSQL parser so it can fix the DDL while scripting the object. Here's the query SMO runs to get the view body:

SELECT
CAST(
        serverproperty(N''Servername'')
       AS sysname) AS [Server_Name],
db_name() AS [Database_Name],
SCHEMA_NAME(v.schema_id) AS [Schema],
v.name AS [Name],
CAST(ISNULL(OBJECTPROPERTYEX(v.object_id,N''ExecIsQuotedIdentOn''),0) AS bit) AS [QuotedIdentifierStatus],
CAST(
 case 
    when v.is_ms_shipped = 1 then 1
    when (
        select 
            major_id 
        from 
            sys.extended_properties 
        where 
            major_id = v.object_id and 
            minor_id = 0 and 
            class = 1 and 
            name = N''microsoft_database_tools_support'') 
        is not null then 1
    else 0
end          
             AS bit) AS [IsSystemObject],
CAST(ISNULL(OBJECTPROPERTYEX(v.object_id,N''ExecIsAnsiNullsOn''),0) AS bit) AS [AnsiNullsStatus],
v.object_id AS [ID],
NULL AS [Text],
ISNULL(smv.definition, ssmv.definition) AS [Definition]
FROM
sys.all_views AS v
LEFT OUTER JOIN sys.sql_modules AS smv ON smv.object_id = v.object_id
LEFT OUTER JOIN sys.system_sql_modules AS ssmv ON ssmv.object_id = v.object_id
WHERE
(v.type = @_msparam_0)and(v.name=@_msparam_1 and SCHEMA_NAME(v.schema_id)=@_msparam_2)
ORDER BY
[Database_Name] ASC,[Schema] ASC,[Name] ASC',N'@_msparam_0 nvarchar(4000),@_msparam_1 nvarchar(4000),@_msparam_2 nvarchar(4000)

這篇關于如何獲得具有正確名稱的視圖的定義?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 成人影院网站ww555久久精品 | 日韩在线小视频 | 午夜激情一区 | 亚洲欧美视频 | 精品视频免费 | 最新日韩欧美 | 欧洲高清转码区一二区 | 在线不卡 | 作爱视频免费观看 | 久久成人一区 | 秋霞在线一区 | 久久成人精品 | 男女污污网站 | 色综合天天综合网国产成人网 | 91精品国产综合久久久久久丝袜 | 国产精品高潮呻吟久久av黑人 | 欧美一级二级视频 | 国产精品久久av | 在线观看日韩精品视频 | 爱爱视频网 | 亚洲精品电影网在线观看 | 亚洲一区二区三区四区五区中文 | 中文字幕免费视频 | 日本不卡免费新一二三区 | 韩日一区二区 | 成人一区二区在线 | 欧美一卡二卡在线 | 一本综合久久 | 九九热在线视频 | 国产精品激情小视频 | 国产成人综合一区二区三区 | 中文字幕亚洲区一区二 | 日本手机在线 | 精品国产乱码一区二区三区 | 99久久久久国产精品免费 | 美女天天操 | 91亚洲精品在线 | 日本免费小视频 | 精品综合久久久 | 国产一区二区三区欧美 | 成人在线视频免费看 |