查找所有包含具有指定名称的 table 的 ERD
Find all ERDs containing table with specified name
是否可以查询包含 table 且名称为
的所有 ERD(实体关系图)的名称
Like '%mytable%'
像这样:
select *
from <ERD objects>
where tableName like '%%'
实际上,我有一个很大的 Database
和很多 ERDs
。因此,要了解 table 的范围,我想浏览那个特定 table.
的 ERDs
据我了解,您需要包含在数据库图表中的 table 列表。在那里你可以帮助这个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 个 table,名为 whatever
和 IE_Stat
。在 return 查询中:
Root EntrypfoBCompObj_ !"#$%&'()*+,-.123456789:(}5n]4o[[=17=]V?[?i???V?[?i??T,,,4") -bH''Uu94941#xV4XdboIE_StatMicrosoft DDS Form 2.0Embedded Object9q&sch_labels_visibled(ActiveTableViewMode1 TableViewMode:0:4,0,28DdsStreamSchema UDV Default&/DSREF-SCHEMA-CONTENTS,0Schema UDV Default Post 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>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>4,0,284,0,2310,12,2730,11,1680NaQW9 LHEData Source=********;Initial Catalog=test;Integrated Security=True;MultipleActiveResultSets=False;TrustServerCertificate=True;Packet Size=4096;Application Name="Microsoft SQL Server Management Studio"TestDiagram&whateverdbo$IE_StatdbokE7d2pN{1634CDD7-0888-42E3-9FA2-B6D32563B91D}bR
您可以看到两个 table 名字。
是否可以查询包含 table 且名称为
的所有 ERD(实体关系图)的名称Like '%mytable%'
像这样:
select *
from <ERD objects>
where tableName like '%%'
实际上,我有一个很大的 Database
和很多 ERDs
。因此,要了解 table 的范围,我想浏览那个特定 table.
ERDs
据我了解,您需要包含在数据库图表中的 table 列表。在那里你可以帮助这个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 个 table,名为 whatever
和 IE_Stat
。在 return 查询中:
Root EntrypfoBCompObj_ !"#$%&'()*+,-.123456789:(}5n]4o[[=17=]V?[?i???V?[?i??T,,,4") -bH''Uu94941#xV4XdboIE_StatMicrosoft DDS Form 2.0Embedded Object9q&sch_labels_visibled(ActiveTableViewMode1 TableViewMode:0:4,0,28DdsStreamSchema UDV Default&/DSREF-SCHEMA-CONTENTS,0Schema UDV Default Post 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>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>4,0,284,0,2310,12,2730,11,1680NaQW9 LHEData Source=********;Initial Catalog=test;Integrated Security=True;MultipleActiveResultSets=False;TrustServerCertificate=True;Packet Size=4096;Application Name="Microsoft SQL Server Management Studio"TestDiagram&whateverdbo$IE_StatdbokE7d2pN{1634CDD7-0888-42E3-9FA2-B6D32563B91D}bR
您可以看到两个 table 名字。