如何使用 sql server management studio 将 blob 插入数据库
如何轻松地将 blob 插入 varbinary(MAX)
字段?
举个例子:
我要插入的是:c:\picture.png
该表是 mytable
该列是 mypictureblob
这个地方是recid=1
How can I easily insert a blob into a varbinary(MAX)
field?
As an example:
thing I want to insert is: c:\picture.png
the table is mytable
the column is mypictureblob
the place is recid=1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
好吧...这花了我太长时间。 sql-management studio工具只是不能完成像这样的简单事情(我之前在寻找在哪里设置查询超时时注意到这一点,并且它是在4个不同的位置完成的)
我下载了一些其他sql编辑器包(在我的例子中是 sql maestro)。看哪,它包括一个 Blob 编辑器,您可以在其中查看 Blob,并将新的 Blob 加载到这些字段中。
感谢您的投入!
Ok... this took me way too long. The sql-management studio tool is just not up to simple things like this (which I've noticed before when looking for where to set the timeout on queries, and it was done in 4 different locations)
I downloaded some other sql editor package (sql maestro in my case). And behold it includes a blob editor where you can look at blobs, and load new blobs into these field.
thanks for the input!
您可以在 SQL Server Management Studio 中使用 T-SQL(特别是使用 OPENROWSET 命令)插入 varbinary(max) 字段。
例如:
请查看以下文档,了解一个很好的示例/演练
使用大值类型
请注意,本例中的文件路径是相对于目标 SQL 服务器的,而不是相对于运行此命令的客户端。
You can insert into a varbinary(max) field using T-SQL within SQL Server Management Studio and in particular using the OPENROWSET commmand.
For example:
Take a look at the following documentation for a good example/walkthrough
Working With Large Value Types
Note that the file path in this case is relative to the targeted SQL server and not your client running this command.
MSDN 有一篇文章使用大值类型,
它试图解释导入部分是如何工作的,但它可能会有点令人困惑,因为它同时做两件事。
我在这里提供一个简化版本,分为两部分。假设有以下简单表:
如果您运行(在 SSMS 中):
它将显示结果看起来像一个表,其中有一列名为
BulkColumn
。这就是为什么您可以在 INSERT 中使用它,如下所示:剩下的只是将其放入具有更多列的插入中,您的表可能有也可能没有。如果您命名
select FOO
的结果,那么您可以在表中其他字段的常量之后使用SELECT Foo.BulkColumn
和as
。更棘手的部分是如何将该数据导出回文件中,以便您可以检查它是否仍然正常。如果你在命令行上运行它:
它将开始抱怨 4 个额外的“参数”,并给出误导性的默认值(这将导致文件发生变化)。您可以接受第一个,将第二个设置为 0,然后接受第三个和第四个,或者明确地说:
然后会询问:
下次运行它时添加
-f C:\Test\bcp_2.fmt
它将停止抱怨: -)节省大量时间和悲伤。
MSDN has an article Working With Large Value Types,
which tries to explain how the import parts work, but it can get a bit confusing since it does 2 things simultaneously.
Here I am providing a simplified version, broken into 2 parts. Assume the following simple table:
If you run (in SSMS):
it will show, that the result looks like a table with one column named
BulkColumn
. That's why you can use it in INSERT like:The rest is just fitting it into an insert with more columns, which your table may or may not have. If you name the result of that
select FOO
then you can useSELECT Foo.BulkColumn
andas
after that constants for other fields in your table.The part that can get more tricky is how to export that data back into a file so you can check that it's still OK. If you run it on cmd line:
It's going to start whining for 4 additional "params" and will give misleading defaults (which will result in a changed file). You can accept the first one, set the 2nd to 0 and then assept 3rd and 4th, or to be explicit:
Then it will ask:
Next time you have to run it add
-f C:\Test\bcp_2.fmt
and it will stop whining :-)Saves a lot of time and grief.
使用 TSQL SELECT BLOB 有两种方法:
以及:
注意 FROM 子句后面的相关名称,这是强制性的。
然后您可以通过执行 INSERT SELECT 将其插入。
您还可以使用第二个版本进行更新,如我在 如何使用 TSQL 更新 SQL SERVER 中的 BLOB 。
There are two ways to SELECT a BLOB with TSQL:
As well as:
Note the correlation name after the FROM clause, which is mandatory.
You can then this to INSERT by doing an INSERT SELECT.
You can also use the second version to do an UPDATE as I described in How To Update A BLOB In SQL SERVER Using TSQL .
但是,您可以简单地从 SQL Server 计算机上的磁盘读取文件:
以十六进制形式在管理应用程序 (Management Studio) 中查看它。
因此,您可以例如将数据库备份到文件(服务器本地),然后通过上面的语句将其下载到其他地方。
However you can simply read a file from disk on SQL server machine:
to see it in management application in hex form (Management Studio).
So, you can, for example, backup database to file (locally on server) and then download it to other place by the statement above.
你需要从 mgmt studio 来做吗?下面是我们从 cmd 行执行此操作的方法:
“C:\Program Files\Microsoft SQL Server\MSSQL\Binn\TEXTCOPY.exe” /S <服务器> /D <数据库> /T mytable /C mypictureblob /F "C:\picture.png" /W"where RecId=" /I
Do you need to do it from mgmt studio? Here's how we do it from cmd line:
"C:\Program Files\Microsoft SQL Server\MSSQL\Binn\TEXTCOPY.exe" /S < Server> /D < DataBase> /T mytable /C mypictureblob /F "C:\picture.png" /W"where RecId=" /I