search
HomeDatabaseMysql TutorialSQLServer存储过程Print
SQLServer存储过程PrintJun 07, 2016 pm 02:57 PM
printsqlserverstorageprocess

SQLServer 存储过程,打印其他存储过程或函数对象的创建语句 代码来源于系统自带的存储过程 无 Create PROCEDURE [dbo].[sp_PrintProc] @objname NVARCHAR(776) , @columnname SYSNAME = NULLAS --打印对象 SET nocount ON DECLARE @dbname SYSNAME , @objid

SQLServer 存储过程,打印其他存储过程或函数对象的创建语句
代码来源于系统自带的存储过程

SQLServer存储过程Print
Create PROCEDURE [dbo].[sp_PrintProc]
    @objname NVARCHAR(776) ,
    @columnname SYSNAME = NULL
AS 
--打印对象
    SET nocount ON  
  
    DECLARE @dbname SYSNAME ,
        @objid INT ,
        @BlankSpaceAdded INT ,
        @BasePos INT ,
        @CurrentPos INT ,
        @TextLength INT ,
        @LineId INT ,
        @AddOnLen INT ,
        @LFCR INT --lengths of line feed carriage return  
        ,
        @DefinedLength INT  
  
/* NOTE: Length of @SyscomText is 4000 to replace the length of  
** text column in syscomments.  
** lengths on @Line, #CommentText Text column and  
** value for @DefinedLength are all 2550. These need to all have  
** the same values. 2550 was selected in order for the max length  
** display using down level clients  
*/ ,
        @SyscomText NVARCHAR(4000) ,
        @Line NVARCHAR(2550)  
  
    SELECT  @DefinedLength = 2550  
    SELECT  @BlankSpaceAdded = 0 /*Keeps track of blank spaces at end of lines. Note Len function ignores  
                             trailing blank spaces*/  
    CREATE TABLE #CommentText
        (
          LineId INT ,
          Text NVARCHAR(2550) COLLATE database_default
        )  
  
/*  
**  Make sure the @objname is local to the current database.  
*/  
    SELECT  @dbname = PARSENAME(@objname, 3)  
    IF @dbname IS NULL 
        SELECT  @dbname = DB_NAME()  
    ELSE 
        IF @dbname <> DB_NAME() 
            BEGIN  
                RAISERROR(15250,-1,-1)  
                RETURN (1)  
            END  
  
/*  
**  See if @objname exists.  
*/  
    SELECT  @objid = OBJECT_ID(@objname)  
    IF ( @objid IS NULL ) 
        BEGIN  
            RAISERROR(15009,-1,-1,@objname,@dbname)  
            RETURN (1)  
        END  
  
-- If second parameter was given.  
    IF ( @columnname IS NOT NULL ) 
        BEGIN  
        -- Check if it is a table  
            IF ( SELECT COUNT(*)
                 FROM   sys.objects
                 WHERE  object_id = @objid
                        AND type IN ( 'S ', 'U ', 'TF' )
               ) = 0 
                BEGIN  
                    RAISERROR(15218,-1,-1,@objname)  
                    RETURN(1)  
                END  
        -- check if it is a correct column name  
            IF ( ( SELECT   'count' = COUNT(*)
                   FROM     sys.columns
                   WHERE    name = @columnname
                            AND object_id = @objid
                 ) = 0 ) 
                BEGIN  
                    RAISERROR(15645,-1,-1,@columnname)  
                    RETURN(1)  
                END  
            IF ( COLUMNPROPERTY(@objid, @columnname, 'IsComputed') = 0 ) 
                BEGIN  
                    RAISERROR(15646,-1,-1,@columnname)  
                    RETURN(1)  
                END  
  
            DECLARE ms_crs_syscom  CURSOR LOCAL  
            FOR SELECT text FROM syscomments WHERE id = @objid AND encrypted = 0 AND number =  
            (SELECT column_id FROM sys.columns WHERE name = @columnname AND object_id = @objid)  
            ORDER BY number,colid  
            FOR READ ONLY  
  
        END  
    ELSE 
        IF @objid < 0 -- Handle system-objects  
            BEGIN  
  -- Check count of rows with text data  
                IF ( SELECT COUNT(*)
                     FROM   master.sys.syscomments
                     WHERE  id = @objid
                            AND text IS NOT NULL
                   ) = 0 
                    BEGIN  
                        RAISERROR(15197,-1,-1,@objname)  
                        RETURN (1)  
                    END  
     
                DECLARE ms_crs_syscom CURSOR LOCAL FOR SELECT text FROM master.sys.syscomments WHERE id = @objid  
                ORDER BY number, colid FOR READ ONLY  
            END  
        ELSE 
            BEGIN  
        /*  
        **  Find out how many lines of text are coming back,  
        **  and return if there are none.  
        */  
                IF ( SELECT COUNT(*)
                     FROM   syscomments c ,
                            sysobjects o
                     WHERE  o.xtype NOT IN ( 'S', 'U' )
                            AND o.id = c.id
                            AND o.id = @objid
                   ) = 0 
                    BEGIN  
                        RAISERROR(15197,-1,-1,@objname)  
                        RETURN (1)  
                    END  
  
                IF ( SELECT COUNT(*)
                     FROM   syscomments
                     WHERE  id = @objid
                            AND encrypted = 0
                   ) = 0 
                    BEGIN  
                        RAISERROR(15471,-1,-1,@objname)  
                        RETURN (0)  
                    END  
  
                DECLARE ms_crs_syscom  CURSOR LOCAL  
                FOR SELECT text FROM syscomments WHERE id = @objid AND encrypted = 0  
                ORDER BY number, colid  
                FOR READ ONLY  
  
            END  
  
/*  
**  else get the text.  
*/  
    SELECT  @LFCR = 2  
    SELECT  @LineId = 1  
  
  
    OPEN ms_crs_syscom  
  
    FETCH NEXT FROM ms_crs_syscom INTO @SyscomText  
  
    WHILE @@fetch_status >= 0 
        BEGIN  
  
            SELECT  @BasePos = 1  
            SELECT  @CurrentPos = 1  
            SELECT  @TextLength = LEN(@SyscomText)  
  
            WHILE @CurrentPos != 0 
                BEGIN  
        --Looking for end of line followed by carriage return  
                    SELECT  @CurrentPos = CHARINDEX(CHAR(13) + CHAR(10),
                                                    @SyscomText, @BasePos)  
  
        --If carriage return found  
                    IF @CurrentPos != 0 
                        BEGIN  
            /*If new value for @Lines length will be > then the  
            **set length then insert current contents of @line  
            **and proceed.  
            */  
                            WHILE ( ISNULL(LEN(@Line), 0) + @BlankSpaceAdded
                                    + @CurrentPos - @BasePos + @LFCR ) > @DefinedLength 
                                BEGIN  
                                    SELECT  @AddOnLen = @DefinedLength
                                            - ( ISNULL(LEN(@Line), 0)
                                                + @BlankSpaceAdded )  
                                    INSERT  #CommentText
                                    VALUES  ( @LineId,
                                              ISNULL(@Line, N'')
                                              + ISNULL(SUBSTRING(@SyscomText,
                                                              @BasePos,
                                                              @AddOnLen), N'') )  
                                    SELECT  @Line = NULL ,
                                            @LineId = @LineId + 1 ,
                                            @BasePos = @BasePos + @AddOnLen ,
                                            @BlankSpaceAdded = 0  
                                END  
                            SELECT  @Line = ISNULL(@Line, N'')
                                    + ISNULL(SUBSTRING(@SyscomText, @BasePos,
                                                       @CurrentPos - @BasePos
                                                       + @LFCR), N'')  
                            SELECT  @BasePos = @CurrentPos + 2  
                            INSERT  #CommentText
                            VALUES  ( @LineId, @Line )  
                            SELECT  @LineId = @LineId + 1  
                            SELECT  @Line = NULL  
                        END  
                    ELSE  
        --else carriage return not found  
                        BEGIN  
                            IF @BasePos <= @TextLength 
                                BEGIN  
                /*If new value for @Lines length will be > then the  
                **defined length  
                */  
                                    WHILE ( ISNULL(LEN(@Line), 0)
                                            + @BlankSpaceAdded + @TextLength
                                            - @BasePos + 1 ) > @DefinedLength 
                                        BEGIN  
                                            SELECT  @AddOnLen = @DefinedLength
                                                    - ( ISNULL(LEN(@Line), 0)
                                                        + @BlankSpaceAdded )  
                                            INSERT  #CommentText
                                            VALUES  ( @LineId,
                                                      ISNULL(@Line, N'')
                                                      + ISNULL(SUBSTRING(@SyscomText,
                                                              @BasePos,
                                                              @AddOnLen), N'') )  
                                            SELECT  @Line = NULL ,
                                                    @LineId = @LineId + 1 ,
                                                    @BasePos = @BasePos
                                                    + @AddOnLen ,
                                                    @BlankSpaceAdded = 0  
                                        END  
                                    SELECT  @Line = ISNULL(@Line, N'')
                                            + ISNULL(SUBSTRING(@SyscomText,
                                                              @BasePos,
                                                              @TextLength
                                                              - @BasePos + 1),
                                                     N'')  
                                    IF LEN(@Line) < @DefinedLength
                                        AND CHARINDEX(' ', @SyscomText,
                                                      @TextLength + 1) > 0 
                                        BEGIN  
                                            SELECT  @Line = @Line + ' ' ,
                                                    @BlankSpaceAdded = 1  
                                        END  
                                END  
                        END  
                END  
  
            FETCH NEXT FROM ms_crs_syscom INTO @SyscomText  
        END  
  
    IF @Line IS NOT NULL 
        INSERT  #CommentText
        VALUES  ( @LineId, @Line )  
    DECLARE @printLine NVARCHAR(2550)
    DECLARE PostCur CURSOR FOR   
    SELECT Text FROM #CommentText ORDER BY LineId  
    OPEN PostCur  
    FETCH NEXT FROM PostCur INTO @printLine  
    WHILE @@fetch_status = 0 
        BEGIN  	
            PRINT @printLine
            FETCH NEXT FROM PostCur INTO @printLine    
        END  
    CLOSE PostCur  
    DEALLOCATE PostCur   
    CLOSE  ms_crs_syscom  
    DEALLOCATE  ms_crs_syscom  
  
    DROP TABLE  #CommentText  
  
    RETURN (0) -- sp_PrintProc  
--存储过程查询
declare @StrSql varchar(max)
set @StrSql=(
Select 'exec('+char(39)+'sp_PrintProc '+name+char(39)+');' as [data()] From sys.objects where Type='P' and name like 'softManage_%' for xml path('')
)
exec(@StrSql)
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
sqlserver数据库中已存在名为的对象怎么解决sqlserver数据库中已存在名为的对象怎么解决Apr 05, 2024 pm 09:42 PM

对于 SQL Server 数据库中已存在同名对象,需要采取以下步骤:确认对象类型(表、视图、存储过程)。如果对象为空,可使用 IF NOT EXISTS 跳过创建。如果对象有数据,使用不同名称或修改结构。使用 DROP 删除现有对象(谨慎操作,建议备份)。检查架构更改,确保没有引用删除或重命名的对象。

sqlserver服务无法启动怎么办sqlserver服务无法启动怎么办Apr 05, 2024 pm 10:00 PM

当 SQL Server 服务无法启动时,可采取以下步骤解决:检查错误日志以确定根本原因。确保服务帐户具有启动服务的权限。检查依赖项服务是否正在运行。禁用防病毒软件。修复 SQL Server 安装。如果修复不起作用,重新安装 SQL Server。

怎么查看sqlserver端口号怎么查看sqlserver端口号Apr 05, 2024 pm 09:57 PM

要查看 SQL Server 端口号:打开 SSMS,连接到服务器。在对象资源管理器中找到服务器名称,右键单击它,然后选择“属性”。在“连接”选项卡中,查看“TCP 端口”字段。

sqlserver数据库在哪里sqlserver数据库在哪里Apr 05, 2024 pm 08:21 PM

SQL Server 数据库文件通常存储在以下默认位置:Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data可通过修改数据库文件路径设置来自定义数据库文件位置。

Java连接SqlServer错误如何解决Java连接SqlServer错误如何解决May 01, 2023 am 09:22 AM

问题发现这次使用的是SqlServer数据库,之前并没有使用过,但是问题不大,我按照需求文档的步骤连接好SqlServer之后,启动SpringBoot项目,发现了一个报错,如下:刚开始我以为是SqlServer连接问题呢,于是便去查看数据库,发现数据库一切正常,我首先第一时间问了我的同事,他们是否有这样的问题,发现他们并没有,于是我便开始了我最拿手的环节,面向百度编程。开始解决具体报错信息是这样,于是我便开始了百度报错:ERRORc.a.d.p.DruidDataSource$CreateCo

sqlserver英文安装怎么更改中文sqlserver英文安装怎么更改中文Apr 05, 2024 pm 10:21 PM

SQL Server 英文安装可通过以下步骤更改为中文:下载相应语言包;停止 SQL Server 服务;安装语言包;更改实例语言;更改用户界面语言;重启应用程序。

Win11无法安装SQL Server的原因及解决方案Win11无法安装SQL Server的原因及解决方案Dec 27, 2023 pm 07:48 PM

有网友反馈,在win11上无法安装sqlserver这款软件,不知道是怎么回事,根据目前的测试来看,win11存在硬盘问题,部分接口硬盘无法安装这款软件。win11为啥不能安装sqlserver:答:win11不能安装sqlserver是硬盘的问题。1、据了解,win11存在对于硬盘的检测bug。2、这导致sqlserver无法在“三星m.2接口”硬盘上安装。3、因此,如果我们要安装的话,需要准备一块其他硬盘。4、然后将该硬盘安装到电脑里,如果没有额外插槽的话就要换掉之前的硬盘。5、安装完成后,

sqlserver数据库日志怎么查询sqlserver数据库日志怎么查询Apr 05, 2024 pm 09:06 PM

可以通过以下步骤查询 SQL Server 数据库日志:1. 打开 SQL Server Management Studio,连接到数据库服务器;2. 展开“管理”节点,导航到“SQL Server 日志”;3. 选择要查询的日志文件,右键单击并选择“查看日志文件”;4. 浏览日志记录。其他查询日志方法:使用 Transact-SQL 查询、PowerShell Cmdlet。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor