反序列化 sqlserver 中的 sysdiagrams,找到其中包含的表的信息
转载于: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] ()
反序列化 sqlserver 中的 sysdiagrams,找到其中包含的表的信息的更多相关文章
- 【转载】 Sqlserver中通过Select Into语句快速单表备份
在Sqlserver数据库中,备份数据的方式有很多种,可以使用整个数据库备份,也可使用导出包含数据和架构的脚本文件的方式来进行单表或多表数据的备份,其实还有一种Select Into的方式可以快速备份 ...
- SQLServer中使用扩展事件获取Session级别的等待信息以及SQLServer 2016中Session级别等待信息的增强
本文出处:http://www.cnblogs.com/wy123/p/6835939.html 什么是等待 简单说明一下什么是等待:当应用程序对SQL Server发起一个Session请求的时候, ...
- SQLServer中获取所有数据库名、所有表名、所有字段名的SQL语句
----1. 获取所有的数据库名----- SELECT NAME FROM MASTER.DBO.SYSDATABASES ORDER BY NAME -----2. 获取所有的表名------ S ...
- SQLServer中的CTE(Common Table Expression)通用表表达式使用详解
概述 我们经常会编写由基本的 SELECT/FROM/WHERE 类型的语句派生而来的复杂 SQL 语句.其中一种方案是需要编写在 FROM 子句内使用派生表(也称为内联视图)的 Transact-S ...
- 在myql sqlserver里边怎么快速找到带有关键字的表
sql server 全部库: ),) set @id=(select count(*) from master..sysdatabases) drop table #t create table # ...
- hdu6003 Problem Buyer 贪心 给定n个区间,以及m个数,求从n个区间中任意选k个区间,满足m个数都能在k个区间中找到一个包含它的区间,如果一个区间包含了x,那么 该区间不能再去包含另一个数,即k>=m。求最小的k。如果不存在这样的k,输出“IMPOSSIBLE!”。
/** 题目:hdu6003 Problem Buyer 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6003 题意:给定n个区间,以及m个数,求从n个区 ...
- 使用Visual Studio进行单元测试-Shim类中无法找到参数包含CancellationTokenSource的方法
Shim类中无法找到参数包含CancellationTokenSource的方法,这句话有点绕口,看例子. 一.代码 public class CancellationDemo { public in ...
- SqlServer还原数据库时提示:异常终止,不能在此版本的SQL Server中启动,因为它包含分区函数
场景 在SqlServer Management中进行数据库还原时提示: 数据库不能在此版本的SQL Server中启动,因为它包含分区函数. 点击左下角的查看详细信息 实现 电脑上安装的是SQL S ...
- Sqlserver中查找包含某一列的所有的表
select cols.name,cols.id,objs.name,objs.id from syscolumns cols INNER JOIN sysobjects objs on cols.i ...
- 解决反序列化(Deserialize)无法找到程序集的错误
http://blog.csdn.net/w_s_q/article/details/5677536 在使用.NET序列化对象时,会将程序集信息也包含进去.如果将序列化之后的字节数组通过网络(或其他传 ...
随机推荐
- Python 常见报错类型整理(一)
很多初学者会遇到很多奇奇怪怪的报错信息,在这里,我为大家已经准备好一部分报错信息的分析以及解决办法. 一.TypeError:类型错误,对象用来表示值的类型非预期类型时发生的错误 错误例子: age= ...
- 你的ASP.NET Pages项目编译时为何总是很慢慢慢~?
摘要 很多同学在运行同一个Asp.net Pages项目解决方案时会发现,有时候很快,有时候超级慢,甚至时间超过10几分钟才可以完全编译完,随后才能调试! 其实这都是跟配置有关 有句话说的话,约定 ...
- 全国IP段列表
http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest Linux下执行如下命令可将中国大陆ip格式化并导出 wget -c htt ...
- 网线接口调试,Android ADB网络调试!
ADB网络调试,网线接口调试 没有USB接口,照样可以调试,可通过网线接口调试步骤! 一.第一步连接WIFI 查看wifi 的IP win+R 键 打开运行 ,输入cmd 二.输入ipc ...
- (K8s学习笔记五)Pod的使用详解
1.Pod用法 K8s里使用的容器不能使用启动命令是后台执行程序,如:nohup ./start.sh &,该脚本运行完成后kubelet会认为该Pod执行结束,将立刻销毁该Pod,如果该Po ...
- Linux下查找并杀死 zombile 和 stopped 进程
用top命令查看系统运行情况,突然发现stopped和zombile进程个数居然不是0. [root@myos software]# top top - 11:20:17 up 60 days, 17 ...
- 如何把腾讯地图左边的搜索结果导出成excel里?
最近,又有朋友让我再写一个 腾讯地图 采集商家的地址,电话信息. 原理应该差不多,我就查阅了下腾讯地图的采集规则,编写了专门针对腾讯地图的采集软件. 界面上和百度地图的采集软件基本类似,但是不用选择区 ...
- 如何把百度地图左边的搜索列表导出成excel里?
有很多人问我,怎么样能够快速的把BAIDU地图左边的搜索列表里的商家地图,电话,导出到EXCEL里. 我就开发了一个小软件,专门为快速的实现导出数据到EXCEL. 为了使用方便,已经将全国的所又省份, ...
- (四)kafka基础术语
1 Topic Kafka消息分类的标签,是一个逻辑概念. 2 Partion 主题作为消息的归类,可以细分为一个或多个分区,分区可以看做是对消息的二次归类.分区可以有一个至多个副本,每个副本对应一个 ...
- 【2】locust性能测试原理分析+登录测试脚本+总结
login_per_test1.py #对登录功能进行单点性能测试(一组测试数据)#发送首页请求,通过locust进行性能测试from locust import HttpLocust,task,T ...