Jeremy Jarrell
Jeremy Jarrell

Reputation: 679

Manually inserting varbinary data into SQL Server

We have a SQL Server table for user settings. Originally the settings were domain objects which had been serialized as XML into the table but we recently begun serializing them as binary.

However, as part of our deployment process we statically pre-populate the table with predefined settings for our users. Originally, this was as simple as copying the XML from a customized database and pasting it into an INSERT statement that was ran after the database was built. However, since we've moved to storing the settings as binary data we can't get this to work.

How can we extract binary data from a varbinary column in SQL Server and paste it into a static INSERT script? We only want to use SQL for this, we don't want to use any utilities.

Thanks in advance, Jeremy

Upvotes: 6

Views: 21400

Answers (5)

Lin
Lin

Reputation: 663

David M's suggestion of using the 0x prefixing works but i had to add an extra 0 at the end of varbinary data that i was trying to insert.

See the stackoverflow entry below to see the issue with additional 0 that gets added when converting to varbinary or saving to varbinary column

Insert hex string value to sql server image field is appending extra 0

Upvotes: 1

Rory
Rory

Reputation: 41917

From SQL Server 2008 onwards you can use Tasks > Generate Scripts and choose to include data. That gives you INSERT statements for all rows in a table which you can modify as needed.

Here's the steps for SQL 2008. Note that the "Script Data" option in SQL 2008 R2 is called "Types of data to script" instead of "Script Data".

Upvotes: 3

SurroundedByFish
SurroundedByFish

Reputation: 3100

If I understand you correctly, you want to generate a static script from your data. If so, consider performing a query on the old data that concatenates strings to form the SQL statements you'll want in the script.

First, figure out what you want the scripted result to look like. Note that you'll need to think of the values you're inserting as constants. For example:

INSERT INTO NewTable VALUES 'value1', 'value2'

Now, create a query for the old data that just gets the values you'll want to move, like this:

SELECT value1, value2
FROM OldTable

Finally, update your query's SELECT statement to produce a single concatenated string in the form of the output you previous defined:

SELECT 'INSERT INTO NewTable VALUES ''' + value1 + ''', ''' + value2 + ''''
FROM OldTable

It's a convoluted way to do business, but it gets the job done. You'll need a close attention to detail. It will allow a small (but confusing) query to quickly output very large numbers of static DML statements.

Upvotes: 0

Jeremy Smyth
Jeremy Smyth

Reputation: 23513

You may find it easier to store a template value in a config table somewhere, then read it into a variable and use that variable to fill your inserts:

DECLARE @v varbinary(1000)
SELECT @v = templatesettings from configtable

INSERT INTO usertable VALUES(name, @v, ....)

Upvotes: 4

David M
David M

Reputation: 72930

I presume you're OK with utilities like Query Analyzer/Mangement Studio?

You can just copy and paste the binary value returned by your select statement (make sure that you are returning sufficient data), and prefix it with "0x" in your script.

Upvotes: 1

Related Questions