mysql varbinary 长度,SQL查询来存储文本数据的VARBINARY(最大值)

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值