Using BQ Command line create View with UDF in BigQuery - javascript

I want to create View in BigQuery using UDF and BQ command line.
BQ command :
bq query --use_legacy_sql=false --project_id="myProject" \
'CREATE OR REPLACE FUNCTION udfFunc(str STRING) RETURNS STRING LANGUAGE js AS
"""
data = fromLib(str);
return JSON.stringify(data);
""" OPTIONS(library = "gs://<bucket>/lib_file.js");
SELECT'
col1,
col2,
udfFunc(col2) as new_col
FROM
`myProject:mySataset.table`'
I am getting an error
Invalid value: Routine name "udfFunc" missing dataset while
no default dataset is set in the request.

From your query (and the comment on the question), it seems that you only need a temp function during the query time, this is the query that you:
define a temp function which is only visible in this query
use the temp function immediately in the query
CREATE TEMP FUNCTION udfFunc(str STRING) RETURNS STRING LANGUAGE js AS
"""
data = fromLib(str);
return JSON.stringify(data);
""" OPTIONS(library = "gs://<bucket>/lib_file.js");
SELECT
col1,
col2,
udfFunc(col2) as new_col
FROM
`myProject:mySataset.table`

Related

SQL compilation error: syntax error line 1 at position 4 unexpected 'S'. Getting this error while calling the procedure

My Proc
CREATE OR REPLACE PROCEDURE Swapna_test_proc20)
RETURNS VARCHAR
LANGUAGE JAVASCRIPT
EXECUTE AS CALLER
AS
$$
var err="
var procResult2 = DATA SUCCESSFULLY LOADED":
try
{
var table_load = 'Select "WHOLESALE_ID" from
CPH DB DEV ENRICH BV ANALYTICS_CH GALEXIS GUTSCHRIFT OUTBOUND BONUSANDDISCOUNT_INVOI
var table load stmt = snowflake.createStatement ((sqlText: table load)).
var exe_table_load_stmt = table_load_stmt.execute();
var columnValue load = exe_table_load_stmt.getColumnValue("WHOLESALE_ID');
var substring load qry= select substring(1,1,3) as extracted string from '+columnValue_load
SQL compilation error: syntax error line 1 at position 4 unexpected 'S'.
Getting this error while calling the procedure

Snowflake Stored Procedure multiple statements list parameters

I would like to create a Snowflake stored procedure with multiple SQL statements, and input multiple parameters which happen to be a list. Having trouble. Is it possible?
My attempt:
CREATE OR REPLACE PROCEDURE SP_MULTI_STATEMENT(NAME string , AGE string)
returns string not null
language javascript
as
$$
var cmd = `
update TABLE_1 set SOMETHING1 = 'OK' where NAME in (${NAME});
update TABLE_2 set SOMETHING2 = 'GREAT' where AGE in (${AGE});
`
var sql = snowflake.createStatement({sqlText: cmd});
var result = sql.execute();
return 'DONE';
$$;
When I call my SP I would like it to be:
call SP_MULTI_STATEMENT('Andy, Brian, Christa','21,23,24');
Most of it looks good, but you need to add more quoted escapes to the list of names.
This example works:
CREATE OR REPLACE PROCEDURE SP_MULTI_STATEMENT(NAMES string , AGES string)
returns string not null
language javascript
as
$$
var cmd = `
select *
from (select 'a' name, 33 age)
where NAME in (${NAMES})
and AGE in (${AGES});
`
var sql = snowflake.createStatement({sqlText: cmd});
var result = sql.execute();
if(result.next()) {
return result.NAME;
}
return 'no results'
$$;
call SP_MULTI_STATEMENT('\'a\', \'b\', \'c\'', '30, 31, 32, 33');

Inserting parameters of stored procedure into a table using Merge in snowflake

I am trying to insert the parameter values of a stored procedure into the table using merge function in sql. The parameters consists of DB and schema name. I have written a stored procedure for that but, I don't understand where I'm doing wrong.
Here is my attempt:
CREATE TABLE TABL(DBName VARCHAR, SCName VARCHAR) // creating table
REATE OR REPLACE PROCEDURE repo(DB VARCHAR,SC VARCHAR) //need to push DB, SC INTO TABL
RETURNS type
LANGUAGE JAVASCRIPT
AS
$$
//Inserting parameters into table as values but didn;t work
var sql_command = "merge TABL as t using (SELECT +"DB"+ as database,+"SC" as schema) as s on t.DBName = s.DB and t.SCName = s.schema when matched then update set t.DBName = t.DBName when not matched then insert (DBName, SCName) VALUES ('"+DB+"','"+SC +"')";
snowflake.execute({sqlText: sql_command});
return type;
$$;
You can use binds:
CREATE TABLE TABL(DBName VARCHAR, SCName VARCHAR); // creating table
CREATE OR REPLACE PROCEDURE repo(DB VARCHAR,SC VARCHAR)
RETURNS string
LANGUAGE JAVASCRIPT
AS
$$
var sql_command = `merge into TABL as t
using (SELECT :1 as database,:2 as schema) as s
on t.DBName = s.database
and t.SCName = s.schema
when matched then update
set t.DBName = t.DBName
when not matched then insert
(DBName, SCName) VALUES (:1,:2)`;
snowflake.execute({sqlText: sql_command, binds: [DB, SC]});
return 'success';
$$;
call repo('a', 'b');
See https://docs.snowflake.com/en/sql-reference/stored-procedures-usage.html#binding-variables for more info.

Storing multi line html encoded string into sql

I want to store html code into sql database which I've encoded by using encodeURI() but it showing me multiple errors as below
I'm using dataType as <CLOB> also tried using NVARCHAR(MAX) but is showing same error.
Sharing my encoded html code below in string format tobe store into sql.
%3Cp%3Ethis%20kind%20of%20text%20i'm%20storing%20into%20database%3C/p%3E%3Cpre%20class=%22code-pre%22%3Evar uri%20= %22my%20test.asp?name=st%C3%A5le&car=saab%22;%0Avar enc%20=%20encodeURI(uri);%0Avar dec%20=%20decodeURI(enc);%0Avar res%20=%20enc%20+ %22<br>%22 +%20dec;%0A%3C/pre%3E
INSERT INTO "Mytable" VALUES(
8/*ID <INTEGER>*/,
'Return matching objects from array of objects'/*QUESTION <NVARCHAR(200)>*/,
'%3Cp%3Ethis%20kind%20of%20text%20i'm%20storing%20into%20database%3C/p%3E%3C pre%20class=%22code-pre%22%3Evar uri%20= %22my%20test.asp ?
name=st%C3%A5le&car=saab%22;%0Avar enc%20=%20encodeURI(uri);%0Avar
dec%20=%20decodeURI(enc);%0Avar res%20=%20enc%20+ %22<br>%22
+%20dec;%0A%3C/pre%3E'/*QUESTION_DESC <CLOB>*/,
'20170508'/*CREATED <TIMESTAMP>*/,
0/*USERID <INTEGER>*/,
1/*TAGID <INTEGER>*/
);
Above command i'm using for pushing data to db. QUESTION_DESC string i've encoded.original string is
<p>this kind of text i'm storing into database</p><pre class="code-
pre">var uri = "my test.asp?name=ståle&car=saab";
var enc = encodeURI(uri);
var dec = decodeURI(enc);
var res = enc + "<br>" + dec;
</pre>
Help will be appriciated
This was quite simple when i tried posting html code from middle ware.
The problem is when i tried to post an html code to database it was showing error because of some random double quotes. So while sending that html code from middle ware i just replaced the double quotes to ignore double quotes. i did like
my html code to be stored in db
var htmlCodeToBeStored =
"<p>this kind of text i'm storing into database</p><pre class="code-
pre">var uri = "my test.asp?name=ståle&car=saab";
var enc = encodeURI(uri);
var dec = decodeURI(enc);
var res = enc + "<br>" + dec;
</pre>"
I replaced above string with as below
htmlCodeToBeStored = htmlCodeToBeStored.replace(/"/g, "\"")
with that simple change i'm able to store my ans into data base.

JSON Dump of raw SQL query returning empty dictionary

I am sending a string through http get request to my views.py and then send back to my javascript but I am not getting my desired data, and instead, I am getting an empty dict. I am on Django 1.8 as well.
views.py:
def getData(request):
some_data = request.GET.get('selectionvalue')
cursor = connection.cursor()
cursor.execute("SELECT SW_ID FROM sw WHERE SWName = %s ", [some_data])
row = cursor.fetchall()
return JsonResponse({"Item" : list(row)})
#return JsonResponse({"Hello" : "world"}) works.
I've been stuck on this problem for the past couple hours and would love any help. Thank you.
I tried this but still no luck:
def getData(request):
some_data = request.GET.get('selectionvalue')
cursor = connection.cursor()
cursor.execute("SELECT SW_ID FROM sw WHERE SWName = %s ", [some_data])
row = cursor.fetchall()
data = {"Item" : row}
return JsonResponse(data)
UPDATE:
I have now converted the query dict that i originally had to a string. I am now trying to use that string variable within my query but it is not working. If a different query without the variable it works perfectly. I can't seem to figure this out.
Have you tried to print "row" variable? Is there any result?
Because maybe this is not a problem from the query, but the problem is that row data is not json serializable, and so JsonResponse can't render it.
i. e., I believe cursor.fetchall() will deliver your data in the following format: [(1,),(5,),(7,)], because it returns a list of tuples. And when you call list(row), response is the same (as a list of tuples is already a list).
Just to be sure, try this:
def getData(request):
some_data = request.GET.get('selectionvalue')
cursor = connection.cursor()
cursor.execute("SELECT SW_ID FROM sw WHERE SWName = %s ", [some_data])
row = cursor.fetchall()
items = []
for r in row:
items.append(r[0])
return JsonResponse({"Item" : items})
Okay after playing around for a bit, I finally fixed it. My problem was that I needed to search for an exact string name in one of the tables and columns in my database but I was retrieving my sent data in views.py as a query dictionary. The thing is that this query dictionary had my single string that I passed in through my request as the key of the dictionary, not the value. So i needed to convert it into a single string variable then use my query. I also needed to not have the ' ' around my %s inside the query.
Views.py:
def getData(request):
some_data = request.GET
for key in some_data:
SWVALUE = key
cursor = connection.cursor()
cursor.execute("SELECT SW_ID FROM sw WHERE SWName = %s ", (SWVALUE))
= %s ", %ItemfromSW)
row = cursor.fetchall()
json_data = json.dumps(list(row))
return HttpResponse(json_data, content_type = "application/json")

Categories