ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

反序列化 sqlserver 中的 sysdiagrams,找到其中包含的表的信息

2022-04-03 02:33:37  阅读:289  来源: 互联网

标签:sysdiagrams dbo -- Text sqlserver id diagram 序列化 DECLARE


转载于:Script SQL Server 2005 diagrams to a file - CodeProject

/**
<summary>
Based on ufn_VarbinaryToVarcharHex by Clay Beatty.
Used by Tool_ScriptDiagram2005

Function has two 'parts':

PART ONE: takes large VarbinaryValue chunks (greater than four bytes) 
and splits them into half, calling the function recursively with 
each half until the chunks are only four bytes long

PART TWO: notices the VarbinaryValue is four bytes or less, and 
starts actually processing these four byte chunks. It does this
by splitting the least-significant (rightmost) byte into two 
hexadecimal characters and recursively calling the function
with the more significant bytes until none remain (four recursive
calls in total).
</summary>
<author>Craig Dunn/Christian Coppes</author>
<remarks>
Clay Beatty's original function was written for Sql Server 2000.
Sql Server 2005 introduces the VARBINARY(max) datatype which this 
function now uses.
This slightly changed version outputs the binary field as text.

References
----------
1) MSDN: Using Large-Value Data Types
http://msdn2.microsoft.com/en-us/library/ms178158.aspx

2) Clay's "original" Script, Save, Export SQL 2000 Database Diagrams
http://www.thescripts.com/forum/thread81534.html or
http://groups-beta.google.com/group/comp.databases.ms-sqlserver/browse_frm/thread/ca9a9229d06a56f9?dq=&hl=en&lr=&ie=UTF-8&oe=UTF-8&prev=/groups%3Fdq%3D%26num%3D25%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26group%3Dcomp.databases.ms-sqlserver%26start%3D25
</remarks>
<param name="VarbinaryValue">binary data to be converted to Hexadecimal </param>
<returns>Hexadecimal representation of binary data, using chars [0-0a-f]</returns>
*/
ALTER FUNCTION [dbo].[Tool_VarbinaryToVarchar_Text]
(
    @VarbinaryValue    VARBINARY(max),
    @bitASCIIOnly    BIT = 0
)
RETURNS VARCHAR(max) AS
    BEGIN
    DECLARE @NumberOfBytes     INT

    SET @NumberOfBytes = DATALENGTH(@VarbinaryValue)
    -- PART ONE --
    IF (@NumberOfBytes > 4)
    BEGIN
        DECLARE @FirstHalfNumberOfBytes INT
        DECLARE @SecondHalfNumberOfBytes INT
        SET @FirstHalfNumberOfBytes  = @NumberOfBytes/2
        SET @SecondHalfNumberOfBytes = @NumberOfBytes - @FirstHalfNumberOfBytes
        -- Call this function recursively with the two parts of the input split in half
        RETURN dbo.Tool_VarbinaryToVarchar_Text(CAST(SUBSTRING(@VarbinaryValue, 1                            , @FirstHalfNumberOfBytes)  AS VARBINARY(max)),@bitASCIIOnly)
             + dbo.Tool_VarbinaryToVarchar_Text(CAST(SUBSTRING(@VarbinaryValue, @FirstHalfNumberOfBytes+1 , @SecondHalfNumberOfBytes) AS VARBINARY(max)),@bitASCIIOnly)
    END
    
    IF (@NumberOfBytes = 0)
    BEGIN
        RETURN ''    -- No bytes found, therefore no 'hex string' is returned
    END
    
    -- PART TWO --
    DECLARE @HighByte         INT
    -- @NumberOfBytes <= 4 (four or less characters/8 hex digits were input)
    --                         eg. 88887777 66665555 44443333 22221111
    -- We'll process ONLY the right-most (least-significant) Byte, which consists
    -- of eight bits

    -- 2. Carve off the rightmost eight bits/single hex digit (ie 22221111)
    --    Divide by 16 does a shift-left (now processing 2222)
    SET @HighByte = CAST(@VarbinaryValue AS INT) & 255
    IF @bitASCIIOnly = 1 AND (@HighByte < 32 OR @HighByte > 126) SET @HighByte=13;

    -- 3. Trim the byte (two hex values) from the right (least significant) input Binary
    --    in preparation for further parsing
    SET @VarbinaryValue = SUBSTRING(@VarbinaryValue, 1, (@NumberOfBytes-1))

    -- 4. Recursively call this method on the remaining Binary data, concatenating the text 
    --    'value' we just decoded as their ASCII character representation
    --    ie. we pass 88887777 66665555 44443333 back to this function, adding X to the result string
    RETURN dbo.Tool_VarbinaryToVarchar_Text(@VarbinaryValue,@bitASCIIOnly) + CHAR(@HighByte)
END
/**
<summary>
Script Sql Server 2005 diagrams
(inspired by usp_ScriptDatabaseDiagrams for Sql Server 2000 by Clay Beatty)
</summary>
<example>
--NOTE: Scalar-valued Function [Tool_VarbinaryToVarchar_Text] must exist before this script is run
SELECT * FROM [dbo].[fnTool_ScriptDiagram2005_Text] () WHERE diagram_ASCII LIKE '%tblUser%'
(Lists all diagrams which contains "tblUser")
</example>
<author>Craig Dunn</author>
<remarks>
Helpful Articles
----------------
1) Upload / Download to Sql 2005
http://staceyw.spaces.live.com/blog/cns!F4A38E96E598161E!404.entry

2) MSDN: Using Large-Value Data Types
http://msdn2.microsoft.com/en-us/library/ms178158.aspx

3) "original" Script, Save, Export SQL 2000 Database Diagrams
http://www.thescripts.com/forum/thread81534.html
http://groups-beta.google.com/group/comp.databases.ms-sqlserver/browse_frm/thread/ca9a9229d06a56f9?dq=&hl=en&lr=&ie=UTF-8&oe=UTF-8&prev=/groups%3Fdq%3D%26num%3D25%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26group%3Dcomp.databases.ms-sqlserver%26start%3D25
</remarks>
<param name="name">Name of the diagram in the Sql Server database instance</param>
*/
CREATE FUNCTION [dbo].[fnTool_ScriptDiagram2005_Text]()
RETURNS 
@tblOut TABLE 
(
    -- Add the column definitions for the TABLE variable here
    diagramname        NVARCHAR(128), 
    diagram_id        INT PRIMARY KEY,
    diagram_text    VARCHAR(MAX),
    diagram_ASCII    VARCHAR(MAX)
)
AS
BEGIN
    DECLARE @name            NVARCHAR(128);
    DECLARE @diagram_id        INT;
    DECLARE @index            INT;
    DECLARE @size            INT;
    DECLARE @chunk            INT;
    DECLARE @line            VARCHAR(MAX);
    DECLARE @lineASC        VARCHAR(MAX);
    DECLARE @CurrentPos        INT;
    SELECT @CurrentPos = MIN(diagram_id) FROM dbo.sysdiagrams;

    WHILE (@CurrentPos IS NOT NULL)
    BEGIN
        -- Set start index, and chunk 'constant' value
        SET @index = 1;        -- 
        SET @chunk = 32;    -- values that work: 2, 6
                            -- values that fail: 15,16, 64
        
        SELECT    @diagram_id = diagram_id,
                @size = DATALENGTH(definition),
                @name = name
          FROM dbo.sysdiagrams 
         WHERE diagram_id = @CurrentPos;

        -- Now with the diagram_id, do all the work

        SET @line = '';
        SET @lineASC = '';
        WHILE @index < @size
        BEGIN
            -- Output as many UPDATE statements as required to append all the diagram binary
            -- data, represented as hexadecimal strings
            SELECT  @line = @line + dbo.Tool_VarbinaryToVarchar_Text(SUBSTRING (definition, @index, @chunk),0),
                    @lineASC = @lineASC + dbo.Tool_VarbinaryToVarchar_Text(SUBSTRING (definition, @index, @chunk),1)
              FROM    dbo.sysdiagrams 
             WHERE    diagram_id = @CurrentPos;
             
            SET @index = @index + @chunk;
        END
        INSERT INTO @tblOut    (diagramname, diagram_id, diagram_text, diagram_ASCII)
             VALUES            (@name,         @diagram_id, @line,        REPLACE(@lineASC,CHAR(13),''));
        SELECT @CurrentPos = MIN(diagram_id)
          FROM dbo.sysdiagrams
         WHERE diagram_id > @CurrentPos;
    END
    RETURN;
END

SELECT * FROM [dbo].[fnTool_ScriptDiagram2005_Text] ()

 

标签:sysdiagrams,dbo,--,Text,sqlserver,id,diagram,序列化,DECLARE
来源: https://www.cnblogs.com/ly45/p/16095020.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有