如何为具有 VARBINARY(MAX) 字段的表生成 INSERT 脚本?
我有一个带有 VARBINARY(MAX)
字段的表(带有 FILESTREAM
的 SQL Server 2008)
我的要求是,当我部署到生产时,我只能提供我的 IT一组按一定顺序执行的 SQL 脚本。我在生产中制作的新表具有此 VARBINARY(MAX)
字段。通常对于新表,我会编写 CREATE TABLE
脚本。而且,如果我有需要处理的数据,我将编写 INSERT
脚本。不太复杂。
但使用 VARBINARY(MAX) 时,我用来生成 INSERT 语句的存储过程在该表上失败了。我尝试选择该字段、打印它、复制它、转换为十六进制等。我遇到的主要问题是它没有选择该字段中的所有数据。我检查了 DATALENGTH([FileColumn])
,如果源行包含 1,004,382 字节,则再次插入时我可以获得复制或选定的数据的最大值是 8000。所以基本上它被截断(即无效) )数据......
我怎样才能做得更好?我疯狂地尝试谷歌搜索,但我一定错过了一些东西。请记住,我无法访问文件系统。这一切都必须有脚本。
I have a table with a VARBINARY(MAX)
field (SQL Server 2008 with FILESTREAM
)
My requirement is that when I go to deploy to production, I can only supply my IT team with a group of SQL scripts to be executed in a certain order. A new table I am making in production has this VARBINARY(MAX)
field. Usually with new tables, I will script out the CREATE TABLE
script. And, if I have data I need to go with it, I will then script out the INSERT
scripts. Not too complicated.
But with VARBINARY(MAX)
, the Stored Procedure I was using to generate the INSERT
statements fails on that table. I tried selecting that field, printing it, copying it, converting to hex, etc. The main issue I have with that is that it doesn't select all the data in the field. I do a check DATALENGTH([FileColumn])
and if the source row contains 1,004,382 bytes, the max I can get the copied or selected data when inserting again is 8000. So basically it is truncated (i.e. invalid) data.....
How can I do this better? I tried Googling this like crazy but I must be missing something. Remember, I can't access the filesystem. This has to be all scripted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果这是一次性(或很少)要做的事情,您可以尝试从 SSMS 向导中编写数据脚本,如下所述:
http://sqlblog.com/blogs/eric_johnson/archive/2010/03/08/script-data-in -sql-server-2008.aspx
或者,如果您需要经常执行此操作并希望使其自动化,您可以尝试 SQL# SQLCLR 库(我编写的,虽然其中大部分是免费的,但您需要的功能不是)。执行此操作的函数是DB_DumpData,它还生成
INSERT
语句。但同样,如果这是一次性或不频繁的任务,请尝试 Management Studio 中内置的数据导出向导。这应该允许您创建可以在生产中运行的 SQL 脚本。我刚刚在一个包含 3,365,964 字节数据的
VARBINARY(MAX)
字段的表上进行了测试,生成脚本向导生成了一个INSERT
语句,其中包含 673 万的整个十六进制字符串该值的字符。更新:
另一种快速简便的方法是,将值转换为
XML< /代码>。首先,您将使用可选样式“1”将
字段。请参见以下示例:VARBINARY
转换为VARCHAR(MAX)
,这会为您提供以“0x”开头的十六进制字符串”。获得二进制数据的十六进制字符串后,您可以将其连接到 INSERT 语句中,并且整个内容在转换为 XML 时可以包含整个 VARBINARYIf this is a one time (or seldom) thing to do, you can try scripting the data out from the SSMS Wizard as described here:
http://sqlblog.com/blogs/eric_johnson/archive/2010/03/08/script-data-in-sql-server-2008.aspx
Or, if you need to do this frequently and want to automate it, you can try the SQL# SQLCLR library (which I wrote and while most of it is free, the function you need here is not). The function to do this is DB_DumpData and it also generates
INSERT
statements.But again, if this is a one time or infrequent task, then try the data export wizard that is built into Management Studio. That should allow you to then create the SQL script that you can run in Production. I just tested this on a table with a
VARBINARY(MAX)
field containing 3,365,964 bytes of data and the Generate Scripts wizard generated anINSERT
statement with the entire hex string of 6.73 million characters for that one value.UPDATE:
Another quick and easy way to do this in a manner that would allow you to copy / paste the entire INSERT statement into a SQL script and not have to bother with BCP or SSMS Export Wizard is to just convert the value to
XML
. First you wouldCONVERT
theVARBINARY
toVARCHAR(MAX)
using the optional style of "1" which gives you a hex string starting with "0x". Once you have the hex string of the binary data you can concatenate that into anINSERT
statement and that entire thing, when converted toXML
, can contain the entireVARBINARY
field. See the following example:不要从 SSMS
bcp 数据输出/输入编写脚本,或使用类似 用于生成 INSERT 语句的 SSMS 工具
Don't script from SSMS
bcp the data out/in, or use something like SSMS tools to generate INSERT statements
它有点混乱,但在过去和网络上我已经看到这是使用 base64 编码的字符串完成的。您可以使用 xml 值来包装字符串,然后可以将其转换为 varbinary。这是一个例子:
http://blogs.msdn.com/b/sqltips/archive/2008/06/30/converting-from-base64-to-varbinary-and-vice-versa.aspx
我个人无法发言不过,这是否有效或性能如何,尤其是对于较大的值。因为它充其量只是一个丑陋的 hack,所以我会将其隐藏在 UDF 中的某个地方,这样,如果找到更好的方法,您可以轻松更新它。
It more than a bit messed up, but in the past and on the web I've seen this done using a base64-encoded string. You use an xml value to wrap the string and from there you can convert it to a varbinary. Here's an example:
http://blogs.msdn.com/b/sqltips/archive/2008/06/30/converting-from-base64-to-varbinary-and-vice-versa.aspx
I can't speak personally to how effective or performant this is, though, especially for large values. Because it is at best an ugly hack, I'd tuck it away inside a UDF somewhere, so that if a better method is found you can update it easily.
我以前从未尝试过类似的操作,但从 SQL Server 2008 R2 的文档来看,这听起来像是使用 SUBSTRING 将获取整个 varbinary 值,尽管您可能必须使用 使用 .WRITE 子句更新以附加数据。
希望这有帮助。
I have never tried anything like this before, but from the documentation for SQL Server 2008 R2, it sounds like using SUBSTRING will work to get the entire varbinary value, although you may have to work with it in chunks, using UPDATEs with the .WRITE clause to append the data.
Hope this helps.