久久久久久久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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 少妇一级片 | 国产欧美久久久 | 青青操影院 | 99久久精品国产一区二区成人 | 九九热视频在线 | 国产午夜在线 | 高潮毛片又色又爽免费 | 日韩在线视频观看 | 久久久久久网站 | 欧美性猛交一区二区三区精品 | 欧美亚洲一区二区三区 | 综合色在线 | 午夜激情网 | 亚洲精品一区二区三区精华液 | 日韩精品视频在线播放 | 少妇高潮av久久久久久 | 亚洲一级黄色片 | 无套内谢的新婚少妇国语播放 | 中文字幕免费看 | 成人在线视频观看 | 亚洲激情网| 激情久久综合 | 亚洲色在线视频 | 欧洲亚洲一区 | 婷婷导航| 伊人2222 | 精品国产网站 | 久久国产综合 | 女人黄网站 | 色草在线| 中文字幕在线不卡 | 在线伊人 | 精品视频久久 | 精品亚洲国产成人av制服丝袜 | 可以看的毛片 | 亚洲国产免费 | 欧美日韩在线一区二区 | 999精品在线 | 在线观看免费av网站 | 亚洲激情第一页 | 日韩小视频在线观看 |