literal array in SQL statement? - arrays

I'm sure there must be a really easy way to do this, but I can't figure out how, and a search hasn't yielded a solution. Here's what I want to do, but it's illegal:
update test_table set q_name = {'SKELETAL','LEWIS','MECHANISM', ...}[q_type];
That is, q_type has an integer value, and I want to populate q_name with a string value that depends on the value of q_type. I can do it with a separate set statement for every value of q_type, but I'd rather do it with a single call. Any ideas how to do it without getting into PL/SQL?

The way that I would handle that is using the xml datatype and just populate that xml variable and then update the table from that.
here's the reference on the xml data type

Related

Binding for in clause from inside snowflake procedure

I want to execute a query
select *
from table
where column1 in (?)
where the value of ? is a java script array object. How can I bind this array to a clause value?
I am looking a cleaner way available out of the box from snowflake, instead of preparing the in clause value by iterating the array and building the string.
You cannot bind a Javascript array object as currently only Javascript variables of type number, string and SfDate can be bound.
For more information see this link.

ORA-22835: Buffer too small and ORA-25137: Data value out of range

We are using a software that has limited Oracle capabilities. I need to filter through a CLOB field by making sure it has a specific value. Normally, outside of this software I would do something like:
DBMS_LOB.SUBSTR(t.new_value) = 'Y'
However, this isn't supported so I'm attempting to use CAST instead. I've tried many different attempts but so far these are what I found:
The software has a built-in query checker/validator and these are the ones it shows as invalid:
DBMS_LOB.SUBSTR(t.new_value)
CAST(t.new_value AS VARCHAR2(10))
CAST(t.new_value AS NVARCHAR2(10))
However, the validator does accept these:
CAST(t.new_value AS VARCHAR(10))
CAST(t.new_value AS NVARCHAR(10))
CAST(t.new_value AS CHAR(10))
Unfortunately, even though the validator lets these ones go through, when running the query to fetch data, I get ORA-22835: Buffer too small when using VARCHAR or NVARCHAR. And I get ORA-25137: Data value out of range when using CHAR.
Are there other ways I could try to check that my CLOB field has a specific value when filtering the data? If not, how do I fix my current issues?
The error you're getting indicates that Oracle is trying to apply the CAST(t.new_value AS VARCHAR(10)) to a row where new_value has more than 10 characters. That makes sense given your description that new_value is a generic audit field that has values from a large number of different tables with a variety of data lengths. Given that, you'd need to structure the query in a way that forces the optimizer to reduce the set of rows you're applying the cast to down to just those where new_value has just a single character before applying the cast.
Not knowing what sort of scope the software you're using provides for structuring your code, I'm not sure what options you have there. Be aware that depending on how robust you need this, the optimizer has quite a bit of flexibility to choose to apply predicates and functions on the projection in an arbitrary order. So even if you find an approach that works once, it may stop working in the future when statistics change or the database is upgraded and Oracle decides to choose a different plan.
Using this as sample data
create table tab1(col clob);
insert into tab1(col) values (rpad('x',3000,'y'));
You need to use dbms_lob.substr(col,1) to get the first character (from the default offset= 1)
select dbms_lob.substr(col,1) from tab1;
DBMS_LOB.SUBSTR(COL,1)
----------------------
x
Note that the default amount (= length) of the substring is 32767 so using only DBMS_LOB.SUBSTR(COL) will return more than you expects.
CAST for CLOB does not cut the string to the casted length, but (as you observes) returns the exception ORA-25137: Data value out of range if the original string is longert that the casted length.
As documented for the CAST statement
CAST does not directly support any of the LOB data types. When you use CAST to convert a CLOB value into a character data type or a BLOB value into the RAW data type, the database implicitly converts the LOB value to character or raw data and then explicitly casts the resulting value into the target data type. If the resulting value is larger than the target type, then the database returns an error.

Find rows with exact geography coordinates

I have a SQL table with a geography column. When I look at one of the rows the geography shows as a long hex string: 0xE6100....C0.
I want to write a query that finds all other rows in my database that have this same value. How can I do this?
I tried adding WHERE location = '0xE6100....C0' with and without quotes but I get an error:
Invalid operator for data type. Operator equals equal to, type equals geography.
Note: I'm just doing this query in an ad-hoc fashion I'm not really looking for a optimal solution or a way to parameterize this in any way. I just have a row that I'd like to find related values.
Looks like you need to use .STEquals
Check the documentation here

sql query replace all specific values with a different value

I am attempting to replace specific values in one column with a new value but it does not work (no errors, no replacement of values, nothing happen).
UPDATE Components
SET Unit='kg'
WHERE Unit='КГ'
How can I replace all values "КГ" with "kg" in column Unit?
I thing that your Unit column is NVarChar() data type. Try following query:
UPDATE Components
SET Unit=N'kg'
WHERE Unit=N'КГ'
Another reason: If you have instead of update trigger on Components table and not update this column on it, your update not affected and no raise error too.
I suggest to use Quotename to resolve this, it is used for this type of string.
UPDATE Components
SET Unit=QUOTENAME('kg')
WHERE Unit=QUOTENAME('КГ')
It is simple and direct query which you run, then its ok. Other wise as #Mehdi said, I also fever that statement.
http://www.c-sharpcorner.com/Blogs/7515/quotename-function-in-sql-server.aspx

How do I cast on an update? The data field is set to vchar(4) and I would like to cast it as an int

This is what I have come up with so far but I am getting a syntax error
update t_Provider set(CAST(f_Postnr as int)) = '66886' where f_Name= 'Test1'
By your question, I assume column f_Postnr is a NVARCHAR(4).
You cannot insert data into this column with a greater character length then 4.
Any solution would require you to edit the column length.
Please look at the syntax for an update in MSDN..
The left hand side of the assignment has to be a known memory location (variable or column/field). What you are doing is trying to set and expression (unknown location) to a constant and filter by a where clause.
Use this code to do what you want.
-- Code from user
update t_Provider
set f_Postnr = 66886
where f_Name= 'Test1'
Why not pass the constant in the update without stating it as a string ''? That's what the above snippet does. This assumes f_Postnr is a INT which can easily handle a 66K number (domain constraint).
If you use '66886', SQL Server will implicit cast the string to an integer if there are no errors.
Here is a huge chart for MSDN on implicit casting matrix.
I hope this helps with your future TSQL endeavors!
What about using...
UPDATE t_Provider
SET f_Postnr = CAST('66886' AS INT)
WHERE f_Name = 'Test1'

Resources