查找包含指定名称

时间:2016-06-29 11:22:46

标签: sql sql-server sql-server-2012 erd

我是否有可能查询包含名称为

的表的所有ERD(实体关系图)的名称
Like '%mytable%'

这样的事情:

select * 
from <ERD objects> 
where tableName like '%%'

实际上,我有一大片Database,有很多ERDs。因此,要了解表格的范围,我想浏览该特定表格的ERDs

1 个答案:

答案 0 :(得分:1)

据我所知,您需要一个包含在数据库图表中的表列表。在那里你可以帮助article。我将在此处添加部分内容:

图表本身存储在二进制字段中。并且您无法毫无困难地将其转换为可读形式。为了反序列化该字段(称为definition),我们需要2个函数:

CREATE 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

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] ()
WHERE diagram_ASCII LIKE '%TableToFind%'

例如,我创建了图表TestDiagram,其中包含2个名为whateverIE_Stat的表。在回复查询中:

  

Root EntrypfoBCompObj_!“#$%&amp;'()* +, - 。123456789 :(} 5n] 4o [\ 0V?[?i ??? V?[?i ?? T ,,, 4” )-bH''Uu94941#xV4XdboIE_StatMicrosoft DDS表格2.0嵌入式Object9q&amp; sch_labels_visibled(ActiveTableViewMode1 TableViewMode:0:4,0,28DdsStreamSchema UDV默认&amp; / DSREF-SCHEMA-CONTENTS,0Schema UDV默认发布V66; 4,0,2310,1, 1890,5,1260 TableViewMode:12,0,284,0,2805 TableViewMode:22,0,284,0,2310 TableViewMode:32,0,284,0,2310 TableViewMode:4&gt; 4,0,284,0,2310,12,2730,11, 1680(ActiveTableViewMode1 TableViewMode:0:4,0,284,0,2310,1,1890,5,1260 TableViewMode:12,0,284,0,2805 TableViewMode:22,0,284,0,2310 TableViewMode:32,0,284,0,2310 TableViewMode :4&gt; 4,0,284,0,2310,12,2730,11,1680NaQW9 LHEData Source = ********; Initial Catalog = test; Integrated Security = True; MultipleActiveResultSets = False; TrustServerCertificate = True; Packet Size = 4096;应用程序名称=“Microsoft SQL Server Management Studio”TestDiagram&amp; whateverdbo $ IE_StatdbokE7d2pN {1634CDD7-0888-42E3-9FA2-B6D32563B91D} bR

你可以看到两个表名。

相关问题