SQL查询来存储文本数据的VARBINARY(最大值) [英] SQL Query to store text data in a Varbinary(max)

查看:1429
本文介绍了SQL查询来存储文本数据的VARBINARY(最大值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,使VARBINARY接受SQL Server的文本数据?

下面是我的情况。我有一个相当大的XML,我计划在存储在一个压缩格式。 (这意味着VARBINARY。)

然而,当我调试,我希望能够翻转以纯文本格式的配置交换机和存储这样我就可以从数据库故障排除(即没有客户端应用程序所需的未压缩)。

是否有可能在插入正常的文本为varbinary(最大值)?

解决方案
  

是否有可能在插入正常的文本为varbinary(最大值)?

,只是确保你正在存储等什么你知道如何找回来了。这可能揭示了一些轻:

   - 设置测试表
声明@test表(
    数据VARBINARY(最大)不为空,
    数据类型为varchar(10)NOT NULL
)

 - 插入VARCHAR
插入@test(数据,数据类型)选择投(ASDF为varbinary(最大值)),'VARCHAR
 - 插入NVARCHAR
插入@test(数据,数据类型)选择投(N'asdf为varbinary(最大值)),'为nvarchar

 - 查看结果
选择数据,数据类型从@test
从@test选择投(数据为varchar(MAX))为data_to_varchar,数据类型
从@test选择投(数据为nvarchar(max))为data_to_nvarchar,数据类型
 

更新: 所有这些假设,当然,你不希望使用SQL Server的本机XML数据类型的EX pressive力量。 XML数据类型也似乎相当有效地存储的内容。在我的数据库中,我经常看到它的短短VARCHAR的字符串相等的一半大小,根据DATALENGTH()。这可能不是所有的科学,当然,因人而异。

Is there a way to make a varbinary accept text data in SQL Server?

Here is my situation. I have a fairly large amount of XML that I plan on storing in a "zipped" format. (This means a Varbinary.)

However, when I am debugging, I want to be able to flip a configuration switch and store in plain text so I can troubleshoot from the database (ie no client app to un-zip needed).

Is it possible to insert normal text in to a varbinary(max)?

解决方案

Is it possible to insert normal text in to a varbinary(max)?

Yes, just be sure of what you are storing so you know how to get it back out. This may shed some light on that:

-- setup test table
declare @test table (
    data varbinary(max) not null,
    datatype varchar(10) not null
)

-- insert varchar
insert into @test (data, datatype) select cast('asdf' as varbinary(max)), 'varchar'
-- insert nvarchar
insert into @test (data, datatype) select cast(N'asdf' as varbinary(max)), 'nvarchar'

-- see the results
select data, datatype from @test
select cast(data as varchar(max)) as data_to_varchar, datatype from @test
select cast(data as nvarchar(max)) as data_to_nvarchar, datatype from @test

UPDATE: All of this assumes, of course, that you don't want to utilize the expressive power of SQL Server's native XML datatype. The XML datatype also seems to store its contents fairly efficiently. In my database I regularly see that it's as little as half the size of an equal string of varchar, according to datalength(). This may not be all that scientific, and of course, YMMV.

这篇关于SQL查询来存储文本数据的VARBINARY(最大值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆