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
Related
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()
I am trying to update paths in access mdb database using ADO and vb6 without success.
The scripts are below. The line Rs1(columnName) = Replace( Rs1(columnName),oldPath,newPath) causes vbscript runtime err invalid use of Null.
Put simply, I want to update all tables that have strings like \\server2 to \\DBSE-46\. I
am running the script on win7 64bit as
c:\windows\syswow64\cscript.exe C:\SQLTest\HarishScripts\DatabaseAccessProg6.vbs >> C:\SQLTest\HarishScripts\DatabaseAccessProg6.txt
Option Explicit
WScript.Echo "START of ADO access program...."
Dim DBpath
Dim tableName
Dim columnName
Dim oldPath
Dim newPath
'#######################################################################################
' Set all external variables here...
DBpath = "C:\DBTest;"
tableName = "Test"
columnName = "Path"
oldPath = "\\SERVER2\"
newPath = "\\DBSE-46\"
'#######################################################################################
Dim Rs1
Set Rs1 = CreateObject("ADODB.Recordset")
Dim i
Dim AccessConnect
AccessConnect = "Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=MedDataSource.mdb;" & _
"DefaultDir=" & DBpath & _
"Uid=Admin;Pwd=;"
'--------------------------
' Recordset Object Method
'--------------------------
' Recordset Open Method #4: Open w/o Connection & w/Connect String
Dim sqlStmt
sqlStmt = "SELECT * FROM " & tableName
' use LockTypeEnum.adLockOptimistic = 3. This allows update of the recordset.
Rs1.LockType = 3
Rs1.Open sqlStmt, AccessConnect
Do Until Rs1.EOF
'WScript.Echo Rs1("Path")
if (Rs1(columnName) = NULL) Then
End If
Rs1(columnName) = Replace( Rs1(columnName),oldPath,newPath)
Rs1.MoveNext
Loop
' Close the recordset...
Rs1.Close
Set Rs1 = Nothing
WScript.Echo "..."
WScript.Echo "..."
WScript.Echo "DONE!"
Use the IsNull function to check for Null values. In my older local .chm there is even a paragraph
Use the IsNull function to determine whether an expression contains a
Null value. Expressions that you might expect to evaluate to True
under some circumstances, such as If Var = Null and If Var <> Null,
are always False. This is because any expression containing a Null is
itself Null, and therefore, False.
Work on your understanding of the If clause. In
if (Rs1(columnName) = NULL) Then
End If
Rs1(columnName) = Replace( Rs1(columnName),oldPath,newPath)
the last statement will be executed regardless of the content of Rs1(columnName). So do
if Not IsNull(Rs1(columnName)) Then
Rs1(columnName) = Replace( Rs1(columnName),oldPath,newPath)
End If
I am experiencing difficulty with the following VBS
set conn = createobject("ADODB.Connection")
Conn.Provider = "ADsDSOObject"
Conn.Open "ADs Provider"
strQueryDL = "<LDAP://company.address.com/cn=address>;(&(objectCategory=person)(objectClass=user));distinguishedName,adspath;subtree"
set objCmd = createobject("ADODB.Command")
objCmd.ActiveConnection = Conn
objCmd.Properties("SearchScope") = 2 ' we want to search everything
objCmd.Properties("Page Size") = 500 ' and we want our records in lots of 500
objCmd.CommandText = strQueryDL
Set objRs = objCmd.Execute
While Not objRS.eof
wscript.echo objRS.Fields("distinguishedName")
' do something with objRS.Fields("distinguishedName")'
objRS.MoveNext
Wend
Please help me, I just started vbscripting and this was from an answer in this website.
wscript.echo objRS.Fields("distinguishedName")
The error was from the above line/code. How do I display out the field or convert it to display?
LDAP://company.address.com/cn=address is not a valid LDAP URL (see here). The search base must be a distinguished name, e.g.:
LDAP://company.address.com/cn=address,dc=address,dc=com
The distinguished name of the domain (dc=address,dc=com) can be obtained like this:
Set rootDSE = GetObject("LDAP://RootDSE")
WScript.Echo rootDSE.Get("defaultNamingContext")
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
here is my code
Dim Cn1 As ADODB.Connection
Dim iSQLStr As String
Dim field_num As Integer
Set Cn1 = New ADODB.Connection
Cn1.ConnectionString = _
"Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"DefaultDir=" & "C:\path\"
Cn1.Open
iSQLStr = "Select * FROM " & "file.txt" ' & " ORDER BY " & txtField.Text
field_num = CInt(1) - 1
Set Rs1 = Cn1.Execute(iSQLStr)
lstResults.Clear
While Not Rs1.EOF
DoEvents
Rs1.Fields(field_num).Value = "qaz"
If IsNull(Rs1.Fields(field_num).Value) Then
lstResults.AddItem "<null>"
Else
lstResults.AddItem Rs1.Fields(field_num).Value
End If
Rs1.MoveNext
Wend
The error i get is in this line
Rs1.Fields(field_num).Value = "qaz"
it says "The current recordset does not support updating", what is wrong in the code?
I'm not sure if this is valid for text files but with SQL Server you need to change the LockTypeEnum Value setting to allow editing see this link, the default is adLockReadOnly
Edit
According to this link it is not possible to edit a text file via ADO.