I want to build dynamic query parameter based on declared variable for EXTERNAL_QUERY.
So I declare STRING variable via concatenation 2 strings:
DECLARE str STRING DEFAULT "SELECT * FROM public.stats WHERE import_date >= "||"'2021-11-29'";
Then trying to set this variable as argument into EXTERNAL_QUERY:
SELECT * FROM EXTERNAL_QUERY("dataset.location.conn_name", str);
It's returns an error:
Query error: Invalid table-valued function EXTERNAL_QUERY Connection argument in EXTERNAL_QUERY must be a literal string or query parameter
What I do wrong?
Not perfect, but works for me.
DECLARE str STRING DEFAULT '''"SELECT * FROM public.stats WHERE import_date >= "'''||"'2021-11-29'";
EXECUTE IMMEDIATE """
SELECT * FROM EXTERNAL_QUERY(\"dataset.location.conn_name\",?);
"""
USING str
Thanks #Timogavk for your answer, I spent a while looking for a solution.
Though in my case I had to change the DECLARE for something more like this, without the additional quotations for it to work (I was working on dynamically querying different tables):
DECLARE str STRING DEFAULT 'SELECT * FROM ' || <table_name> || ' Limit 10';
Related
I need to generate a query like this
SELECT max(batch_sk) INTO ln_batch FROM test_app.table WHERE categ_cd = 'ABC' ;
This 'ABC' is coming as an input parameter in the procedure(in_code).
I have tried different version of the below mentioned query , but not getting the correct query .
SET lv_query ='SELECT max(batch_sk) INTO ln_batch FROM test_app.table
WHERE categ_cd = ''''||in_code||'';
How to include those ' in the query?
Several things wrong here.
Mismatched / un-escaped quotes
Concatenation operates on character strings and there is no implicit
conversion from TIMESTAMP to VARCHAR. You would need to convert with explicit CAST or TO_CHAR, etc. first
SELECT … INTO is not allowed as dynamic SQL. Perhaps parameterized static SQL with a host variable?
SELECT max(batch_sk) INTO ln_batch FROM test_app.table WHERE categ_cd=:in_code;
otherwise you need a dynamic cursor to supply the value as a literal
DECLARE lv_cur CURSOR for lv_stmt;
SET lv_query ='SELECT max(batch_sk) FROM test_app.table WHERE categ_cd = '''||in_code||'''';
PREPARE lv_stmt FROM lv_query;
OPEN lv_cur;
FETCH lv_cur INTO ln_batch;
CLOSE lv_cur;
i want to use parameters for my dynamic queries. I have a statements like so:
RETURN QUERY EXECUTE 'SELECT * FROM boards AS b WHERE b.slug = $1 AND $2'
USING filter_slug, parent_id_query;
I get a ERROR: argument of AND must be type boolean, not type text
if i do it like this:
RETURN QUERY EXECUTE 'SELECT * FROM boards AS b WHERE b.slug = ''' || filter_slug || ''' AND ' || parent_id_query;
it works though.
I feel like i am missing something / not understanding something. Please help.
What you are missing is how parameters are used. Parameters are not macros that replace arbitrary text inside a SQL statement. Instead, they are literal values assigned to "variables" inside the code. These values are typically numbers, strings, or dates.
In particular, parameters cannot be used for:
identifiers (columns names and table names)
function names
operators
SQL keywords
general expressions
So, unfortunately, you have to construct that part of the query without a generic parameter (although you can have $2 = $3)
String str = 'ce765e1bc7:abc879:53:7011:2'
How to split the string using sybase query function to value 7011
I have working stored proc for this. But wanted to know if sybase provides any inbuilt function to do so.
If you know the position of the first character and length of the required pattern, you can use 'substring'
Syntax - substring(expression, start, length)
select substring('ce765e1bc7:abc879:53:7011:2',22,4)
If you only have string and the pattern to find but not sure about the length, you can additionally use 'charindex' and 'char_length' as shown in the example below:
BEGIN
DECLARE #stpos INT, #stlen INT
SELECT #stpos = charindex('7011', 'ce765e1bc7:abc879:53:7011:2')
SELECT #stlen = char_length('7011')
SELECT substring('ce765e1bc7:abc879:53:7011:2',#stpos, #stlen)
END
I'm trying to pass some values from a vba function to an SQL.
This is my SQL
SELECT *
FROM Hierarchy3
WHERE ID IN (getList("1 and 2"));
this is the definition of the vba function:
Function getList(Measure As String) As String
when I call the function I get: 1,2,3 as a String.
if I run the SQL as
SELECT *
FROM Hierarchy3
WHERE ID IN (1,2,3);
it works fine, but combining the two doesn't work. So I guess the String type is wrong, can you please help?
Your problem is that your query does not work like you expect it. Your function returns a string, so the query performed is:
SELECT *
FROM Hierarchy3
WHERE ID IN ("1,2,3");
Notice the quotation marks. Basically you are comparing an integer to a string and so doesn't return any results.
What you can do is use the INSTR function to see if the ID can be found in the string:
SELECT *
FROM Hierarchy3
WHERE INSTR(getList("1 and 2"),ID);
Now it will work because ID 1 can be found in the string "1,2,3" so the INSTR function will return the position where it can be found.
Is anyone familiar with using the LOCATE with a passed in string varialbe? I am trying to use it to determine if there is a comma in a string that was set in the stored procedure but have not been able to get it to work properly. My code looks something like this
DECLARE string VARCHAR(10);
DECLARE comma_found INT;
SET string = 'hello, world';
SET comma_found = SELECT LOCATE(',',string);
IF( comma_found <> 0 ) THEN
...execute code....
END IF;
This code will not complie because of the SELECT LOCATE and I can not figure out what is wrong. Is it my syntax? Usage? Is there any other string maniuplation function I can use to accomplish this? I am doing this within a stored procedure in Mysql.
I haven't used MySQL in a long time so I'm not completely familiar with the newer syntax but from some googling, I believe
SET comma_found = SELECT LOCATE(',',string);
should be
SELECT #comma_found := LOCATE(',',string);
then you can use #comma_found eg:
SELECT #comma_found
It seems that you're trying to assign a data set to the variable rather than the result of the LOCATE
See here: http://dev.mysql.com/doc/refman/5.0/en/user-variables.html
Well the syntax for LOCATE is LOCATE(substring, string). Your query literally says LOCATE(',', string) where string is a varchar(10). Your string is actually more than 10 characters long though =P