Saving values into a Blob (text field)

We are using classic ASP/VB script and passing some values from our database to a different system.
To do this we declare variables and assign the value to them, then pass them to the other system. So far works great.

Problem is from the other system it sends us back two values. the name of the 'field' and the 'value'

Say for example.

name: address1
value:  street name goes here

name: zipcode1
value:  33044

and so on, they can be 200 values. We need to save the name of the field AND the value in one text field in the database, so that later on we can parse it and send it back to the system again.

I have no clue on how to concatenate this values and save them as one big Blob into the text field of our Blobtbl
And then the second step how to un-concatenate them so that they can be passed back again like we do with the other variables. Right now we do it like:

Dim ownFName
ownFName= (rs_Beneficiary.Fields.Item("FirstNm").Value)
Dim ownLName
ownLName= (rs_Beneficiary.Fields.Item("LastNm").Value)
Dim ownMName
ownMName= (rs_Beneficiary.Fields.Item("MiddleNm").Value)
Dim ownDOB
ownDOB= (rs_Beneficiary.Fields.Item("Dob").Value)
Dim ownCitizenship
ownCitizenship= (rs_Beneficiary.Fields.Item("ResCntry").Value)

But those are for variables we KNOW the names and values, the ones coming back from the other system may be 10 or 100 variable names with their values. So on our end we need to un-concatenate each one and separate them as above so that we can send them back later on.

I hope I make sense. We are using MS SQL 2008 if it makes any difference.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Daniel WilsonConnect With a Mentor Commented:
If the original data is coming in as some kind of key-value pairs, storing it all in a text blob (e.g. varchar(max)) is really not a very nice solution.  Is the raw data XML?  JSON?  XML has its own data type in SQL Server 2008.

If, however, you really must concatenate the values then something like this could work.

Dim MyBigText
for i = 0 to rs_Beneficiary.fields.count -1
  MyBigText = MyBigtext & rs_Beneficiary.fields(i).name & ":" & rs_Beneficiary.fields(i).value & vbcrlf
next i

Now you have a monster string you can store the way you usually store your data.
AleksAuthor Commented:
What would be a different solution to save all this data to my database to then later on retrieve it and send it back ?
Big MontyConnect With a Mentor Senior Web Developer / CEO of Commented:
I agree using a BLOB field isn't the way to go. If all you're storing is pieces of text, either store it in a TEXT field or parse out the name/values before saving it to the database in it's own table (this is the way I would go). BLOB fields are for storing files directly to the database.

To elaborate on this, you'll need to create an associative table, a table where you can store data and then associate some kind of ID that'll link that data to another tables record. Without knowing your exact needs, I'll just give you this example.

Say you have a PAGE table, and you want to be able to associate a dynamic number of fields to that table. In your PAGE table, you have a PageID that is your primary key, and then you have all of your other basic data for the page. Next, create a table called FIELDS, that would look something like this:

create table Fields {
    fieldID int IDENTITY(1,1),
    fieldName varchar(50),
    fieldValue varchar(50), 
    pageID int

Open in new window

Here, you'll see that I created a table called FIELDS where you can now store all of the fields that you get back from the other service, all you have to do is associate the pageID with it.

parsing the data is relatively easy as well, assuming you're getting them as a POST or GET (if it's another way, we'll need to tweak the code below):

dim fldsFrom3rdParty
fldsFrom3rdParty = Request.QueryString           '-- change to Request.Form if the data is coming via POST

for each fldName in fldsFrom3rdParty
     fldValue = Request( fldName )
     '-- build sql to insert data into FIELDS table, then do the insert

Open in new window

if you have any questions on how this code works, feel free to ask!
Daniel WilsonConnect With a Mentor Commented:
Big Monty's solution -- entity-attribute-value -- appears very appropriate.
AleksAuthor Commented:
I did mean a text field  my bad. Thanks for the above info. I will test and let you know how that worked ... then I will have to read it and parse it back to an xml to then send it to the other system. BUt Ill get to that later, first I have to save it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.