I'm using the Visual FoxPro OLE DB provider to query a VFP DB and I'm getting:
System.Data.OleDb.OleDbException
'Operator/operand type mismatch`
What could I be doing wrong?
In my where clause I had an int on one side and a char(15) on the other side
Table Schema
id int
Query
SELECT *
FROM [some-table]
WHERE id = 'some string'
In my case to avoid such kind of inconveniences I do the following I hope it works for you:
var_name = iif(vartype(var_name)=='N',var_name,Val(var_name))
so you avoid two possible errors, if it comes in character with value I convert it into number and if it comes in character without any value it becomes 0.
SELECT *
FROM [some-table]
WHERE id = ?Var_name
Related
I am trying to analyse the code used in the stored procs on our Netezza server.
First step is to get the definitions/code contained in the stored procs - this can easily be done by either of the following:
Using the system views
select
PROCEDURE,
PROCEDURESOURCE
from _v_procedure
where
PROCEDURE = 'MY_PROC'
;
Or using the base table [view points to this table]
select
PRONAME,
PROSRC as PROCEDURESOURCE
from
DEFINITION_SCHEMA."_T_PROC" P
where
PRONAME= 'MY_PROC'
Now, once I run some analysis on the PROCEDURESOURCE column and try to write this information out to a table, I always get the following error:
ERROR: Type 'bytea' not supported by IBM Netezza SQL
Easy way to replicate this error is simply doing the following
create table MY_SCHEMA.TEST_TMP as
with rs as
(
select
PRONAME,
PROSRC
from
DEFINITION_SCHEMA."_T_PROC" P
where
PRONAME = 'MY_PROC'
)
select * from rs
I have determined that there is a column in DEFINITION_SCHEMA."_T_PROC" of type bytea (column name = PROBIN)
I am however not selecting this column, so I am not sure why I am getting this error
Can anyone help with a workaround on how to copy the PROCEDURESOURCE into a new table and bypass the 'bytea' error
Thanks
3 suggestions:
1) Sometimes the ‘limit all’ trick helps: What are the benefits of using LIMIT ALL in a subquery?
2) Alternatively, do a ‘create external table’ and put your data into a file, then another statement to read it back from the file
3) last guess is that you may be able to explicitly cast the column to a more benign data type (Varchar() or similar)
I'm trying to use Contains() in a search procedure. The full text indexes are created and working. The issue arises because you cannot used Contains() call on a NULL variable or parameter, it throws an error.
This takes 9 sec to run (passing in non-null param):
--Solution I saw on another post
IF #FirstName is null OR #FirstName = '' SET #FirstName = '""'
...
Select * from [MyTable] m
Where
(#FirstName = '""' OR CONTAINS(m.[fname], #FirstName))
This runs instantly (passing in non-null param)
IF #FirstName is null OR #FirstName = '' SET #FirstName = '""'
...
Select * from [MyTable] m
Where
CONTAINS(m.[fname], #FirstName)
Just by adding that extra 'OR' in front of the 'contains' completely changed the Query Plan. I have also tried using a 'case' statement instead of 'OR' to no avail, I still get the slow query.
Has anyone solved this the problem of null parameters in full text searching or experience my issue? Any thoughts would help, thanks.
I'm using SQL Server 2012
You are checking value of bind variable in SQL. Even worse, you do it in OR with access predicate. I am not an expert on SQL Server, but it is generally a bad practice, and such predicates lead to full table scans.
If you really need to select all values from table when #FirstName is null then check it outside of SQL query.
IF #FirstName is null
<query-without-CONTAINS>
ELSE
<query-with-CONTAINS>
I believe, in the majority of times #FirstName is not null. This way you will access table using your full text index most of the time. Getting all the rows from table is a lost cause anyway.
From a logical standpoint, the first query takes longer to execute because it has to evaluate 2 conditions:
#FirstName = '""'
and ,in case the first condition fails, which should be the majority of the time,
CONTAINS(m.[fname], #FirstName)
My guess is that in your table, you don't have any null or empty FirstName, that's why the results are the same. Otherwise, you would have a few "" in the result set as FirstName.
Maybe you should try reversing the order to see if it makes any difference:
WHERE (CONTAINS(m.[fname], #FirstName) OR #FirstName = '""')
I'd like to use UUID as an identifier, provide the first 8 digits to find out if it exists in the database.
normally I can do this without a problem:
select * from TABLE where id = 'e99aec55-9e32-4c84-aed2-4a0251584941'::uuid
but this gives me error:
select * from TABLE where id LIKE 'e99aec55%#'::uuid
error:
ERROR: invalid input syntax for uuid: "e99aec55%#"
LINE 1: select * from TABLE where id LIKE 'e99aec55...
^
Query failed
PostgreSQL said: invalid input syntax for uuid: "e99aec55%#"
Is there a way to query first n digits for a UUID type in postgresql?
Since you are searching for the highest bits of uuids, you can actually use between (because uuid comparison is well-defined in PostgreSQL):
...
where some_uuid between 'e99aec55-0000-0000-0000-000000000000'
and 'e99aec55-ffff-ffff-ffff-ffffffffffff'
UUIDs are not stored as strings in Postrges, they are stored as a 16-byte long binary values. So the only way to query it in the way you want is to convert it to string at first, but the performance of such conversion will be worser than just performing an equality comparison.
Also you will need to maintain an index on those string representation of the UUIDs, so it just doesn't make sense.
Why not just cast your uuid column using id::varchar like so:
select * from TABLE where id::varchar LIKE 'e99aec55%'
Worked for me.
I have the following sample code. The objective is to run SQL statement with multiple input parameters.
[<Literal>]
let connectionString = #"Data Source=Localhost;Initial Catalog=Instrument;Integrated Security=True"
[<Literal>]
let query = "SELECT MacroName, MacroCode FROM Instrument WHERE MacroCode IN (#codeName)"
type MacroQuery = SqlCommandProvider<query, connectionString>
let cmd = new MacroQuery()
let res = cmd.AsyncExecute(codeName= [|"CPI";"GDP"|]) |> Async.RunSynchronously
However, codeName is inferred to be string type instead of an array or list and give me an error.
Alternatively, I could run the query without where statement and filter based on the result. However, in lots of other cases that returns millions of rows, I would prefer filter data at the SQL server level to be more efficient.
I didn't find any relevant samples on the documentation of fsharp.data.sqlclient. Please help!
"See Table-valued parameters (TVPs)" section in the documentation:
http://fsprojects.github.io/FSharp.Data.SqlClient/configuration%20and%20input.html
If you have an upper bound n on the values in the IN list, you could just make n parameters. If that's unmanageable, I'm afraid the TVP suggestion is your best option. The reason the FSharp.Data.SqlClient library is unlikely to ever support this directly is because the types are generated based on results from sp_describe_undeclared_parameters; there's no T-SQL parser. We had a single digit upper bound in this scenario and were loathe to change the database, so this option worked for us.
You can use STRING_SPLIT to abstract away the use of table-valued parameters. It seems you have to also declare the param first.
DECLARE #param varchar(1000) = #commaSeparatedList
SELECT Col1 FROM MyTable
WHERE Col2 IN (
SELECT value FROM STRING_SPLIT(#param, ',')
)
Hey I want to select records with name O'Neil, How can i do that in Informix.
select * from name_table where lastname = 'O'Neil' --> Doesn't Work.
You must escape single quote with another single quote:
select * from name_table where lastname = 'O''Neil';
With Informix, you generally have two choices.
The Standard SQL technique (described by Michal Niklas) always works and is the simple, recommended solution. All appearances of a single quote in a string are doubled up:
SELECT * FROM Name_Table WHERE LastName = 'O'Neill';
An alternative technique that works unless you have set DELIMIDENT in your environment is to use double quotes around the string:
SELECT * FROM Name_Table WHERE LastName = "O'Neill";
If delimited identifiers are enabled by DELIMIDENT, then this has a different meaning; the DBMS will be looking for a column called "O'Neill" in the table (because it is an identifier, not a string).
If you have both quotes in a string, then you have to be be careful:
SELECT * FROM QuoteTable WHERE Quote = 'He said, "Don''t"!';
SELECT * FROM QuoteTable WHERE Quote = "He said, ""Don't""!';
My 2nd answer with proposition of technology change.
I don't know what technology you use, but maybe you can use PreparedStatements. Example in Jython (Python that can use JDBC drivers):
db = DriverManager.getConnection(jdbc_url, usr, passwd)
pstm = db.prepareStatement("select * from name_table where lastname=?")
pstm.setString(1, "O'Neil")
rs = pstm.executeQuery()
while (rs.next()):
print('[%s]' % (rs.getString(1)))
As you see PreparedStatement use ? as a placeholder and then you must fill it using setString() method. It is very useful if you have to do many similar selects, inserts etc. Have a look at Java6 documentation for this: http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html
Similar techniques can be user for ODBC or other technologies.