I am passing two (2) parameters in the URL, and building the following SQL:
mQry = "SELECT DISTINCT name FROM link3 WHERE invoice_num =" & request.querystring("num") & "AND name LIKE" & request.querystring("nam")
I got an error message:
Microsoft OLE DB Provider for Oracle error '80040e14'
ORA-00933: SQL command not properly ended
What would be the right syntax for this?
You need to put quotes around the LIKE clause. Also, you could consider using percents for wildcard matching
mQry = "SELECT DISTINCT name FROM link3 WHERE invoice_num =" & request.querystring("num") & " AND name LIKE '%" & request.querystring("nam") & "%' "
Part of your problem may be the improper spacing around the quotes where you're inserting the values. This:
mQry = "SELECT DISTINCT name FROM link3 WHERE invoice_num =" & request.querystring("num") & "AND name LIKE" & request.querystring("nam")
Will most likely result in sending this to the database:
SELECT DISTINCT name
FROM link3
WHERE invoice_num =2AND name LIKEsomeothervalue
If you add proper spacing like this:
mQry = "SELECT DISTINCT name FROM link3 WHERE invoice_num = " & request.querystring("num") & " AND name LIKE " & request.querystring("nam")
It would give you a more properly formatted result like this:
SELECT DISTINCT name
FROM link3
WHERE invoice_num = 2 AND name LIKE someothervalue
Any time I get errors that indicate problems with SQL formatting/structure I tend to log the SQL that is sent before it goes to the database. This helps spot odd issues like that.
Also, sharpguru is probably right - the LIKE clause probably isn't formatted correctly either. You need to enclose text values in single-quotes, and the % is a wildcard matching 0 or more characters - making this more like what you are probably looking for:
mQry = "SELECT DISTINCT name FROM link3 WHERE invoice_num = " & request.querystring("num") & " AND name LIKE '%" & request.querystring("nam") & "%'"
Now, this does all assume that invoice_num is some sort of numeric value - which is implied in your question and code. However, if it is not (as suggested by your comment and other questions), you would need to put the value in single quotes - just like any other text field in almost all RDBMSs:
mQry = "SELECT DISTINCT name FROM link3 WHERE invoice_num = '" & request.querystring("num") & "' AND name LIKE '%" & request.querystring("nam") & "%'"
The above would also be used if the data type of the database column invoice_num was set to a non-numeric data type. Just because the data is somehting that could be called numeric, doesn't mean it's automatically treated as numeric. If the data type of the column is text, ntext, or any other non-numeric type, then you will need to surround the value in quotes just like any other text value.
And, while not related to the question, I'm hoping this is an over-simplified example and you're not directly inserting QueryString values into the SQL statement. If you haven't been told yet, that's opening you up to a wide variety of security problems - look up some information on SQL Injection.
Related
I have one Table called tblEmployees - ID, First, Last. One unbound form called frmSearch with a textbox named Searchbox and a search button that searches by ID.I have one more unbound form called frmDisplay that displays the search result in it which I would like to edit when necessary. The textbox fields for this form are EID, Fname, Lname.
The problem I am having is when I enter the ID# in the searchbox and click the search button (where I linked both ID fields in the button wizard) it keeps on displaying the second record in my table. This is the code I currently have running
Private Sub Form_Load()
Dim rst As Recordset
Set rst = CurrentDb.OpenRecordset("SELECT * from tblEmployees WHERE ID=ID")
EID.Value = rst!ID
Fname.Value = rst!First
Lname.Value = rst!Last
end sub
When I change the code to read
("SELECT * tblEmployees WHERE ID=" & ID")
I get a syntax error missing operator in query expression ID="
ID must have some value. Then concatenate ID:
"Select * From tblEmployees Where ID = " & ID & ""
The recordset that's currently being opened will include all the records in your table, because for every record the condition ID = ID will always be true. It's purely coincidence that it's displaying the second record from your table. You didn't specify a sort on the recordset query so it could randomly pick up any record as the first one in the results. What you actually need is
Set rst = CurrentDb.OpenRecordset("SELECT * from tblEmployees WHERE ID = " & frmSearch!Searchbox)
I'm using ORACLE database. How to detect duplicate data even the text is lower or uppercase.
Assuming on my table already inserted : Production
Now I want to add: production (with lower case), it should be detect duplicate. In my current case, it was not detected and inserted.
Here is the sample query:
SELECT * FROM tb_departments WHERE DEPARTMENT_NAME = '" . $getDepartmentName . "';
Anyone have an idea?
You can use the UPPER (or LOWER) function, which capitalize your string, i.e.
SELECT * FROM tb_departments WHERE UPPER(DEPARTMENT_NAME) = UPPER('" . $getDepartmentName . "');
As small variation you could capitalize your input string in the code and use
SELECT * FROM tb_departments WHERE UPPER(DEPARTMENT_NAME) = '" . $yourUpperDepartmentName . "';
Moreover I suggest you use query parameters, instead of injecting directly the parameters string ($getDepartmentName ) in your query.
I have a drop down box in my classic ASP page and using MS SQL for database. The drop down list includes the brand names starting with lower case, upper case and starting brand name with numbers.
For instance, itcosmetics, Colorpop Cosmetics and 5 Hour energy respectively. Currently, this dropdown is showing/displaying brands alphabetically but by case i.e. lower case, number, upper case.
In other words, it is displaying all lower case brand name alphabetically first then the brand with numbers alphabetically and finally brand names starting with uppercase alphabetically.
However, what I am trying is : I want to display my options according to the alphabetical orders without worrying about the case of the brand name.
for example: if the brand names are 1 apple, applea, Appleb, 3 fans, balla, Ballb, cat, Doll.
Currently, its displaying drop down option as following:
applea
balla
cat
1 apple
3 fans
Appleb
Ballb
Doll
But I want something like following:
1 apple
3 fans
applea
Appleb
balla
Ballb
cat
Doll
How can I do that?
My code :
<select>
<OPTION value=0>-- SELECT --</OPTION>
<%
DIM RS, varQueryBrand, varBrand
IF Request.QueryString("brandID") <> "" THEN
varQueryBrand = Request.QueryString("brandID")
ELSE
varQueryBrand = "SELECT"
End IF
SQL = "SELECT DISTINCT(brand) as brand FROM tblproduct"
SET RS=objConn.Execute(SQL)
IF NOT (RS.BOF and RS.EOF) THEN
WHILE NOT RS.EOF
varBrand = RS("brand")
IF LCase(varQueryBrand) = LCase(varBrand) THEN
Response.Write "<option selected value=""" & replace(RS("brand")," & ","#") & """>" & RS("brand") & "</option>"
ELSE
Response.Write "<option value=""" & replace(RS("brand")," & ","#") & """>" & RS("brand") & "</option>"
End IF
RS.MoveNext
WEND
END IF
RS.close
SET RS = nothing
%>
</select>
I tried writing ORDER BY brand at the end of SQL statement, but there was no chage in the output. So can you please help me?
You are likely using a Collation with the CS designation in it like Latin1_General_CS_AS for example.
To avoid the Unicode Sorting, switch to a Binary Collation to get the expected sort order.
SELECT DISTINCT brand
FROM tblproduct
ORDER BY brand COLLATE Latin1_General_bin
A slightly messier approach is to use a sub query to wrap a lowercase version of the Brand column and use that for sorting.
SELECT x.brand
FROM (
SELECT DISTINCT brand, LOWER(brand) [brand_lower]
FROM tblproduct
) x
ORDER BY x.brand_lower
As #Martha point's out don't forget to specify an ORDER BY in your SQL string in Classic ASP.
Useful Links
ORDER BY … COLLATE in SQL Server
From what it looks like, you aren't currently sorting your results at all, so you're getting whatever default order SQL Server cares to come up with.
Most databases are automatically set to case-insensitive sorting, so unless your database is set up strangely, the following should work:
SQL = "SELECT DISTINCT brand FROM tblproduct ORDER BY brand"
If that doesn't work and you don't feel like messing around with the COLLATION settings, you can sort by an all-lowercase (or all-uppercase) version of the field:
SQL = "SELECT DISTINCT brand FROM tblproduct ORDER BY LOWER(brand)"
when I try to insert a value to recordset in the 'Description' field. it showing a error like
runtime error '-2147217887(80040e21)'
Multiple- Step operation generated errors. check each status value.
sql = "SELECT * FROM vePODetail WHERE vePOID=" & Str(ado_veReceive.Recordset("vePOID")) & " ORDER BY vePODetailID"
rs.ActiveConnection = g_cnnCompany
rs.Open sql
Do While Not rs.EOF
ado_veReceiveDetailWF.Recordset.AddNew
ado_veReceiveDetailWF.Recordset("vePODetailID") = rs("vePODetailID")
ado_veReceiveDetailWF.Recordset("prMasterID") = rs("prMasterID")
ado_veReceiveDetailWF.Recordset("Description") = rs("Description")
ado_veReceiveDetailWF.Recordset("QuantityReceived") = rs("QuantityOrdered") -rs("QuantityReceived")
ado_veReceiveDetailWF.Recordset.Update
rs.MoveNext
Loop
rs.Close
the field in the recordset acccepts only 50 char.
Please tell how to increase the size/length of the field in the recordset.
If the field is 50 chars long, you must change the DB's definition of the field from 50 to whatever you need. You cannot do that through a recordset
Assuming you're using SQL Server, you can change your query using a CAST operation:
sql = "SELECT vePODetailID,prMasterID,CAST(Description as VARCHAR(100)) AS Description, QuantityReceived FROM vePODetail WHERE vePOID=" & Str(ado_veReceive.Recordset("vePOID")) & " ORDER BY vePODetailID"
That should set the length of the Description field in the recordset to 100 characters. You can do this in other db platforms as well, but the syntax may be different for the CAST.
Hey guys, just want to ask you a simple question that I know you're familiar with... I am using VB6, I just want to get sets of records from my database. What I mean is that I have UserID and with a part of code provided below, it only gets a single set of record. Like for instance, the value of UserID is A12, and so, all sets of records with the UserID of A12 must display in Textboxes respectively with the aid of datPayroll.Recordset.MoveNext.
With datPayroll
.RecordSource = "select * from tblpayroll where empid like '" & UserID & "'"
.Refresh
Me.txtRegularHours.Text = .Recordset.Fields!reghours
End With
-datPayroll : DataControl
-txtRegularHours : Textbox
-UserID : Variable
You probably want to look at MoveFirst, MoveNext, etc. and also EOF
Here is a link or two to get you started:
EOF, BOF
MoveFirst, MoveNext
You need to check that you have some data in your Recordset using EOF, then MoveFirst to move to the first record, and loop through using MoveNext.