Solved

Need to insert text into a IMAGE field as Double Byte from vbscript into SQL table

Posted on 2016-11-04
10
42 Views
Last Modified: 2016-11-09
I had a vbscript (macro) that inserted records into a IMAGE field in a SQL Server database,

              pmspec = pmspec & vbCrLf & "Reason for Obsoletion: " & result & " By: " & Network_User & " - " & Now()
              Set db = CreateObject("ADODB.Connection")
              db.Open SCONNUPD
              SET RSu = CREATEOBJECT("ADODB.RECORDSET")
              SSQLUPD = "INSERT INTO PART_MFG_BINARY ([PART_ID],[TYPE],[BITS],[BITS_LENGTH]) VALUES ('" & ID & "'" & chr(44) & "'D'" & chr(44) & "'" & pmspec & "'" & chr(44) & Len(pmspec) & ")"
              Set RSu = db.Execute(SSQLUPD)

Open in new window

Now our ERP vendor has upgraded all of their fields to DoubleByte (including the BITS (IMAGE) field above.

I can not figure out how to write the insert now to convert my text to DoubleByte.

I do know that I will need to change my Len(pmspec) to LenB(pmspec)  (the table stores the length in bytes of the field into BITS_LENGTH)

Any help is greatly appreciated.
0
Comment
Question by:BFanguy
  • 6
  • 2
10 Comments
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 41874722
put in N in front of the BITS value.

something like this:

              pmspec = pmspec & vbCrLf & "Reason for Obsoletion: " & result & " By: " & Network_User & " - " & Now()
              Set db = CreateObject("ADODB.Connection")
              db.Open SCONNUPD
              SET RSu = CREATEOBJECT("ADODB.RECORDSET")
              SSQLUPD = "INSERT INTO PART_MFG_BINARY ([PART_ID],[TYPE],[BITS],[BITS_LENGTH]) VALUES ('" & ID & "'" & chr(44) & "'D'" & chr(44) & " N'" & pmspec & "'" & chr(44) & Len(pmspec) & ")"
              Set RSu = db.Execute(SSQLUPD)

Open in new window

0
 

Author Comment

by:BFanguy
ID: 41874737
odbc sql server driver Operand type clash: nvarchar is incompatible with image
0
 

Author Comment

by:BFanguy
ID: 41874738
i am guessing i have to convert it to unicode double byte before i try to insert???
0
 

Author Comment

by:BFanguy
ID: 41874745
tried from ssms:

INSERT INTO PART_MFG_BINARY ([PART_ID],[TYPE],[BITS],[BITS_LENGTH]) VALUES ('BM305-0295800-1600-1','D', N'test', Len('test')*2)

Msg 206, Level 16, State 2, Line 1
Operand type clash: nvarchar is incompatible with image
0
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
LVL 12

Assisted Solution

by:Jeff Darling
Jeff Darling earned 250 total points
ID: 41874850
ok, data type in the database is image.

https://msdn.microsoft.com/en-us/library/ms187993.aspx

Then do a conversion cast as image

https://msdn.microsoft.com/en-us/library/ms191530.aspx

INSERT INTO PART_MFG_BINARY ([PART_ID],[TYPE],[BITS],[BITS_LENGTH]) VALUES ('BM305-0295800-1600-1','D', cast('test' as image), Len('test')*2)

Open in new window

0
 

Author Comment

by:BFanguy
ID: 41877028
thanks, tried your statement, the app show chinese characters.

tried
INSERT INTO PART_MFG_BINARY ([PART_ID],[TYPE],[BITS],[BITS_LENGTH]) VALUES ('BM305-0295800-1600-1','D', cast( N'test' as image), Len('test')*2)

Open in new window


Explicit conversion from data type nvarchar to image is not allowed.
0
 

Accepted Solution

by:
BFanguy earned 250 total points
ID: 41877040
Tried this and it worked.  thanks.

INSERT INTO PART_MFG_BINARY ([PART_ID],[TYPE],[BITS],[BITS_LENGTH]) VALUES ('BM305-0295800-1600-1','D',CAST(CAST('test' AS NVARCHAR(MAX)) AS VARBINARY(MAX)), Len('test') * 2 )

Open in new window

0
 

Author Comment

by:BFanguy
ID: 41877455
Jeff, sorry i thought i could give you the points even if i picked best solution.  trying to figure out what i have to do to give you the points
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now