types mismatch in VBScript - vbscript

I am trying to get number of table's rows in db and output it to console using VBScript, but when I execute following code I get type mismatch error, what should I change in my code to force it execute without errors
Dim loop_lim
Const DB_CONNECT_STRING = "Provider=SQLOLEDB.1;Data Source=BUG\SQLSERVER2005;Initial Catalog=test;user id ='sa';password='111111'"
Set myConn = CreateObject("ADODB.Connection")
Set myCommand = CreateObject("ADODB.Command" )
myConn.Open DB_CONNECT_STRING
Set myCommand.ActiveConnection = myConn
myCommand.CommandText = "select count(*) from oferty o inner join rep_oferta ro on o.indeks = ro.srcdoc inner join rep_pozycje rp on o.indeks = rp.srcdoc"
loop_lim = myCommand.Execute
WScript.Echo loop_lim

Change
loop_lim = myCommand.Execute
to
Set loop_lim = myCommand.Execute
because .Execute returns a recordset object. Then think about how to get values from the recordset rsp. it's fields.

Related

Using SQL command in VBS Script [duplicate]

This question already has answers here:
Troubleshooting a Parameterized SQL Statement in asp
(2 answers)
Closed 3 years ago.
I'm not very familiar with VBS but I want to use this SQL Query into VBS script. Here is my SQL query :
declare #names nvarchar(4000)
select #names = coalesce(#names+';','')+ email from myTable where sendbox=1
Here is my vbs code :
Set Recordset= CreateObject("ADODB.recordset")
ConnString="DRIVER={SQL Server};SERVER=MYSERVER\INSTANCE;Trusted_Connection = yes;DATABASE=MyBase"
Dim Query, Dest
Query = "select #names = coalesce(#names+';','')+ email from myTable where sendbox=1"
Recordset.open Query,ConnString
Dest = Recordset(0).value
Recordset.close
As you can see, I don't know where how I could declare my var #name.
I found another way that's working for what i should do.
Here is my solution if it can help :
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "DRIVER={SQL Server};SERVER=MyServer\Instance;Trusted_Connection = yes;DATABASE=MyBase"
Set objRecordset = objConnection.Execute("SELECT Email email from myTable where sendbox=1")
i = 0
dim Dest
objRecordset.MoveFirst
Do While Not objRecordset.EOF
Dest = Dest & objRecordset(0).Value & ";"
objRecordset.MoveNext
i = i + 1
Loop
objRecordset.Close
Set objRecordset = Nothing
Set objConnection = Nothing

Oracle VBAscript connection error

Dim strDBDesc As String
strDBDesc = "(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = ##)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = ##)))"
cn.Open "Provider=OraOLEDB.Oracle;Data Source=" & strDBDesc & ";User ID=##;Password=##;"
query1 = ""
query1 = ERM.Sheets("query").Range("A10")
query1 = Replace(query1, "v_job_name", v_field1)
'Set OraDynaSet = objdatabase.DBCreateDynaset(query1, 0&)
Dim OraDynaSet As ADODB.Recordset
Set OraDynaSet = CreateObject("ADODB.Recordset")
OraDynaSet.ActiveConnection = cn
OraDynaSet.Open query1, cn, adOpenStatic
I am getting error message as ORA-00933 sql command not ended properly
Error Message
Query 1
select
job_name,Status
from (
select Distinct a.job_name,
a.description description,Decode (job_type,98,'Box',99,'Command Job',102,'File watcher job',job_type) job_type,
substr(decode(d.status,1,'Running',
3,'Starting',
4,'Success',
5,'Failed',
6,'Terminated',
7,'On Ice',
8,'Inactive',
9,'Activated',
11,'On Hold',
12,'Que Wait',
d.status),1,9) status,
a.mach_name,a.owner,g.command,g.std_err_file,g.std_out_file,f.days_of_week,f.start_times,f.start_mins,f.run_calendar,f.max_run_alarm,profile
from AEDBADMIN.ujo_job a,
AEDBADMIN.ujo_job_runs c,
AEDBADMIN.ujo_job_status d,
(select joid,max(STARTIME) startime,
max(endtime) endtime
from AEDBADMIN.ujo_job_runs group by joid) e,
AEDBADMIN.ujo_command_job g,
AEDBADMIN.ujo_sched_info f
where a.joid = c.joid(+)
and a.joid = d.joid(+)
and a.joid = e.joid(+)
and a.joid = f.joid(+)
and a.joid = g.joid(+)
and (c.startime = e.startime or c.startime is null)
and job_name ='v_job_name'// job name replaces
and a.is_active =1
);
I think the problem is this one
and job_name ='v_job_name'// job name replaces
In SQL comments are done by -- ... (single line) or /* ... */ (multi line).
However, perhaps ADODB does not support comments at all, I recommend to remove it entirely.
Or it is the semicolon ; at the end - remove it.
Just a note, you should rewrite your query and use ANSI join syntax instead of old Oracle join syntax - especially for OUTER JOINS.

Parameterized query in VBScript referring OracleDB

I have a parameterized query which is giving
"ORA-01008: not all variables bound" error.
Dim Conn
Dim Cmd
Dim RS
Dim strID
Dim param
strID = Request.QueryString("id")
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open strConnect
Set Cmd = Server.CreateObject("ADODB.Command")
Cmd.CommandText = "SELECT column_name FROM table WHERE (id = :id)"
Set param = Cmd.CreateParameter("id", adVarChar , adParamInput ,50 , strID)
Cmd.Parameters.Append param
Cmd.CommandType = adCmdText
Set Cmd.ActiveConnection = Conn
Set RS = Cmd.Execute()
I'm trying to modify in syntax in several ways, then it is giving
ORA-00936: missing expression
Please help me to get out of this. For your information, there is no problem with connection as i am able to connect with normal query.
a few things to check:
1) try hard coding a value for strID, so instead of:
strID = Request.QueryString("id")
try
strID = 100
2) double check your column definitions and make sure you're selecting from a varchar(50) field
3) make sure you have adovbs.inc referenced on your page for the ADO constants definitions
Thanks #Lankymart, luckily i got solution for this as below. It is working fine for me and sorry for the delay in posting the answer, my issue resolved 2 hours ago.
Dim Conn
Dim Cmd
Dim RS
Dim strID
Dim param
strID = Request.QueryString("id")
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open strConnect
Set Cmd = Server.CreateObject("ADODB.Command")
With Cmd
.CommandText = "SELECT column_name FROM table WHERE id = ?"
.Parameters.Append .CreateParameter(,200, 1 ,50 ,strID)
Set .ActiveConnection = Conn
End With
Set RS = Cmd.Execute()

VBscript RecordCount property

I am newbie in VBScript and I've come across with the following problem. I want get data from sql server db and to allow RecordCount properties. Next code get data but RecordCount is disabled. How can I enable this properties
Const DB_CONNECT_STRING = "Provider=SQLOLEDB.1;Data Source=BUG\SQLSERVER2005;Initial Catalog=test;user id ='sa';password='111111'"
Set myConn = CreateObject("ADODB.Connection")
Set myCommand = CreateObject("ADODB.Command" )
myConn.Open DB_CONNECT_STRING
Set myCommand.ActiveConnection = myConn
myCommand.CommandText = ("select * from klienci k where k.indeks = " & oferty(16))
Set klienci = myCommand.Execute
AFAIK you can't change the cursor type when using the Execute method of the Command object, and you can't change the cursor type after you retrieved the recordset. Something like this might work, though:
Const DB_CONNECT_STRING = "Provider=SQLOLEDB.1;Data Source=BUG\SQLSERVER2005;Initial Catalog=test;user id ='sa';password='111111'"
Set myConn = CreateObject("ADODB.Connection")
myConn.Open DB_CONNECT_STRING
query = "select * from klienci k where k.indeks = " & oferty(16)
Set klienci = CreateObject("ADODB.Recordset")
klienci.CursorLocation = 3 'adUseClient
klienci.CursorType = 3 'adOpenStatic
klienci.LockType = 1 'adLockReadOnly
klienci.Open query, myConn
I don't think this is a VBScript issue- I think it is an ADO issue.
I think you are using a default forward-only cursor which won't work with recordcount.
I think you should stick a cursortype=adOpenStatic in there but I'm having a little trouble determining if you are specifying a recordset object - klienci?
If so try
klienci.cursortype=adOpenStatic

How to add picture into database?

I have created a table mypics that contains one column of BLOB datatype.
Now I need to implement a vb6 code to Select/Insert/Update data in this table, but I don`t know how to deal with the BLOB column...
SQL> desc mypics
Name Null? Type
PID NOT NULL NUMBER(38)
PNAME CHAR(10)
IMAGE BLOB
Please help
Here is some example code to get you started. Assume a table named tblImages with 3 fields.
Field Data Type Size
Picture Image
ID Int 4
To add an image from a file on disk to the database
Set strStream = New ADODB.Stream
strStream.Type = adTypeBinary
strStream.Open
strStream.LoadFromFile strFileName
strSQL = "SELECT ID, Picture FROM tblImages"
Set rs = New ADODB.Recordset
With rs
.ActiveConnection = cn
.Source = strSQL
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open
End With
rs.AddNew
rs.Fields("ID").Value = ID
rs.Fields("Picture").Value = strStream.Read
rs.Update
rs.Close
Set rs = Nothing
To extract the file from database to a disk file :
strSQL = "SELECT Picture FROM tblImages WHERE ID = " & ID
Set rs = New ADODB.Recordset
With rs
.ActiveConnection = cn
.Source = strSQL
.Open
End With
If Not (rs.BOF And rs.EOF) Then
Set strStream = New ADODB.Stream
strStream.Type = adTypeBinary
strStream.Open
strStream.Write rs!Picture
strStream.SaveToFile TempPath, adSaveCreateOverWrite
strStream.Close
Set strStream = Nothing
End If
rs.Close
Set rs = Nothing
I hope this helps.

Resources