In BigQuery we usually pass parameters to stored procedures and functions by position something like this:
CREATE TEMP FUNCTION FullName(firstName STRING, lastName STRING)
RETURNS STRING
AS(
firstName || ' ' || lastName
);
SELECT FullName("Fred", "Flintstone")
I would like to be able to not care about the position of the parameters when calling the function or stored procedure. Perhaps with something like this:
# This does not work
SELECT FullName("Flintstone" AS lastName, "Fred" AS firstName)
Unfortunately, this results in a syntax error. I have also tried a few other possibilities such as FullName(lastname="Flintstone", firstName="Fred"), etc and tried to search in the docs but came up blank.
Is there a mechanism for passing parameters by name in an arbitrary order like this?
You were very close (I think) - Use below
CREATE TEMP FUNCTION FullName(name ANY TYPE)
RETURNS STRING
AS(
name.firstName || ' ' || name.lastName
);
SELECT
FullName(STRUCT("Fred" AS firstName, "Flintstone" AS lastName)),
FullName(STRUCT("Flintstone" AS lastName, "Fred" AS firstName))
with output
Related
If I try to call a stored procedure to check for mail addresses containing a #mail it fails because it sees the #mail as a variable instead of a string literal
Is this what you want?
where email like '%' || #mail || '%'
|| here is the standard operator for string concatenation. This can very depending on the database.
You can use
Concat function
SELECT * FROM TableA WHERE Email LIKE CONCAT('%',#Email,'%')
I ended up creating a variable which holds the like #domainname placed it in the exec statement and it started working. So I guess the variable gets parced, the value is added to the query and its accepted as text in stead of searching for a variable
Here is my one of my tries without sucess:
var myVar VARCHAR;
exec :myVar:= 'm';
select * from users where lower(name) like lower(':myVar%')
myVar is the variable i want to inject in the string
Hope to have explained my question well. Thanks in advance.
Don't put the variable inside the quotes:
select * from users where lower(name) like lower(:myVar) || '%'
A few things.
var myVar VARCHAR2(2); --varchar2, not varchar
exec :myVar:= 's%'; -- put the wildcard here, it's just easier
select * from employees where lower(first_name) like lower(:myVar)
run everything via F5
I want to execute a dynamic SQL statement, which searches for names whose last name is always a constant and first name is a variable.
Here is a query I have written for selecting a row with name='Test lastname'.
EXECUTE 'SELECT name FROM users
WHERE name=$1 lastname'
USING ('Test');
This generates a syntax error. Is it possible to do this?
I think you need something like this:
EXECUTE 'SELECT user_id FROM users
WHERE name=$1'
USING Test||' lastname' ;
Here Test is variable and 'lastname' is hard coded value
Also another way is as #JorgeCampos mentioned:
...WHERE name=$1 || '' lastname''' USING 'Test';
I'm having problem with executing select statement while using match and and/or operators while using parameters.
The statement looks like this:
SELECT ColumnName1, ColumnName2
FROM TableName
WHERE TableName MATCH 'ColumnName1:#param1 AND ColumnName2:#param2';
But when I change #param1 and #param2 with real values, for example like this
SELECT ColumnName1, ColumnName2
FROM TableName
WHERE TableName MATCH 'ColumnName1:Value AND ColumnName2:Value';
it works fine.
I guess the problem is those single quotes around my AND statement. Is there any way to avoid them but still be able to use and/or operators or is problem somewhere else?
You can't use SQL parameters to replace parts of an argument, but perhaps you may be able to use string concatenation (I didn't test this):
... MATCH 'ColumnName1:' || #param1 || ' AND ColumnName2:' || #param2;
Else you'll have to go the canonical way, build the string in your application and provide it as a single parameter:
... MATCH #param;
I am testing contains method in store procedure. I would like to see data result like:
Example Result for John:
John Nick
Papa John
Harly John Fredy
I don't want to use LIKE method. Can we use starts with or ends with (*) operator with parameters in SP?
CONTAINS(name,#name) // query is working
but if i can try like this:
CONTAINS(name, '"john" OR "john*"') // query is working
CONTAINS(name,'"#name" OR "#name*"') // query is not working
With parameters same query is not working in SP. Is it posibble to do this in SP?
Thanks,
With CONTAINS, in order to pass a wildcard through with a variable, you need to make the wildcard a part of the variable.
The easiest way to do it within a stored procedure is adding a variable (or modifying your current variable).
For example,
DECLARE #nameX NVARCHAR(4000) = '"' + #name + '" OR "' + #name + '*"'
...
CONTAINS(name, #nameX)