Solved

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

Posted on 2016-11-04
10
61 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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