解決SQL Server裡sp_helptext輸出格式錯行問題

來源:互聯網
上載者:User
use Mastergoif object_id('SP_SQL') is not nulldrop proc SP_SQLgo/******************************************************************************************************************************************************整理人:中國風(Roy)日期:2008.01.01******************************************************************************************************************************************************/create proc [dbo].[SP_SQL](@ObjectName sysname)asset nocount on ;declare @Print nvarchar(max)----@Print用nvarcharif exists(select 1 from syscomments  where ID=object_id(@ObjectName) and encrypted=1)begin Print N'對象已加密!' returnendif coalesce(object_id(@ObjectName,N'P'),object_id(@ObjectName,N'FN'),object_id(@ObjectName,N'IF'),object_id(@ObjectName,N'TF'),object_id(@ObjectName,N'TR'),object_id(@ObjectName,N'V')) is  nullbeginPrint N'對象只針對函數、存儲過程、觸發器、視圖!'return endprint 'Use '+db_Name()print 'Go'print 'if object_ID('+quotename(case when charindex(']',@ObjectName)=0 then '['+replace(rtrim(@ObjectName),'.','].[')+']' else @ObjectName end ,'''')+') is not null'print char(9)+'Drop '+casewhen object_id(@ObjectName,N'P') is not null then 'Procedure ' when Coalesce(object_id(@ObjectName,N'FN'),object_id(@ObjectName,N'IF'),object_id(@ObjectName,N'TF')) is not null then 'Function ' when object_id(@ObjectName,N'TR') is not null then 'Trigger ' else 'View 'end +case when charindex(']',@ObjectName)=0 then '['+replace(rtrim(@ObjectName),'.','].[')+']' else @ObjectName endPrint 'Go'declare @T table(Col nvarchar(max))insert @T select object_definition(object_id(@ObjectName)) +char(13)+char(10) while (select max(Col) from @T)>''beginselect top 1 @Print=left(Col,charindex(char(13)+char(10),Col)-1) from @Tprint @Printupdate @T set Col=stuff(Col,1,charindex(char(13)+char(10),Col)+1,'')endprint 'Go'go調用方法:use test--指定所屬數據庫exec sp_sql 'sp_sql'--指定對象
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.