Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate an INSERT script for a table with a VARBINARY(MAX) field?

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.

like image 384
Issa Fram Avatar asked Apr 12 '11 03:04

Issa Fram


People also ask

How do I create a script insert in SQL?

In the results grid in SSMS, highlight the required columns then right-click and choose Script As INSERT. This will open a new query window in SSMS, containing a script that inserts the results into a temporary table.

What is the best way to auto generate insert statements for a SQL Server table?

Steps To Auto Generate INSERT StatementsFrom the right-click menu, go to Tasks >> Generate Scripts... In the Generate and Publish Scripts pop-up window, press Next to choose objects screen. Now, the choose objects screen, choose Select specific database objects and choose the tables you want to script.


1 Answers

If 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 an INSERT 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 would CONVERT the VARBINARY to VARCHAR(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 an INSERT statement and that entire thing, when converted to XML, can contain the entire VARBINARY field. See the following example:

DECLARE @Binary VARBINARY(MAX) = CONVERT(VARBINARY(MAX),
                                         REPLICATE(
                                           CONVERT(NVARCHAR(MAX), 'test string'),
                                           100000)
                                        )

SELECT 'INSERT INTO dbo.TableName (ColumnName) VALUES ('+
       CONVERT(VARCHAR(MAX), @Binary, 1) + ')' AS [Insert]
FOR XML RAW;
like image 65
Solomon Rutzky Avatar answered Sep 20 '22 19:09

Solomon Rutzky