Snowflake - First Proc - What is wrong with Syntax - procedure

CREATE OR REPLACE PROCEDURE "PUBLIC".PHONE_DATA_ROLLUP()
RETURNS VARCHAR
LANGUAGE javascript
AS
$$
var rs = snowflake.execute( { sqlText:
'INSERT INTO PROC_LOG ("PROC_NAME","EXEC_TIME") SELECT ''value 1'' AS "PROC_NAME", CURRENT_TIMESTAMP as "EXEC_TIME";'
});
return 'Done.';
$$;
CALL PHONE_DATA_ROLLUP();
Error:
SQL Error [100131] [P0000]: JavaScript compilation error: Uncaught
SyntaxError: Unexpected string in PHONE_DATA_ROLLUP at ' 'INSERT
INTO PROC_LOG ("PROC_NAME","EXEC_TIME") SELECT ''VALUE 1'' AS
"PROC_NAME", CURRENT_DATE as "EXEC_TIME";'' position 62

I would highly recommend using backquotes for SQL strings in a JavaScript procedure, as well as variable binding, eg
var rs = snowflake.execute( {
sqlText: `INSERT INTO PROC_LOG ("PROC_NAME","EXEC_TIME")
SELECT :1 AS "PROC_NAME", CURRENT_TIMESTAMP as "EXEC_TIME"`,
binds: ["value 1"]
});
The main advantages are:
You can use both 'single' and "double" quotes without any protection contamination
Strings can be multiline
Both features are highly useful with SQL code.

in Javascript like this you can escape single quotes with a backslash (\') instead of double single quotes ('')
...
'INSERT INTO PROC_LOG ("PROC_NAME","EXEC_TIME") SELECT \'value 1\' AS "PROC_NAME", CURRENT_TIMESTAMP as "EXEC_TIME";'
...

Related

Firedac select working with Firebird returns no records

Hello I'm working with Firedac (Delphi Seattle) using Firebird (2.5) as a database, when I run this query using a TFDQuery, no records are returned:
SELECT ID FROM USERS WHERE PWD = 'êHÆ–!+'
The same query within a Database program as IbExpert return one record. Is there some parameter with Firedac components to configure that can solve this issue. Thanks.
It's in the query string and it's the ! char. By default, query strings are preprocessed, and you must escape constant chars like !, &, :, ?, { or }, otherwise they are used as special chars.
Your best option is using parameters. That will (except other benefits) get rid of that ! char from the preprocessed command:
FDQuery.SQL.Text := 'SELECT ID FROM USERS WHERE PWD = :Password';
FDQuery.ParamByName('Password').AsString := 'êHÆ–!+';
FDQuery.Open;
Another option is escaping that constant char or disable macro preprocessor. For more information see the Special Character Processing topic.

Figuring out what this Trim() function is doing?

I have a constraint on a transformer with this:
Trim(CollectFrom.collect_from,"-","A")<=TheDate
Here is what collect_from looks like:
'2017-02-27'
And here is what TheDate looks like:
'20170227'
I am unsure exactly how this Trim() function works. My first guess is that it is giving it the same format as 'TheDate' however I don't understand the "A" argument. Could somebody explain it?
The manual page for TRIM() says that shouldn't work.
When I try to run what you show, I get errors:
SQL[2405]: select trim('2017-02-01', '-', 'A') from dual;
SQL -674: Routine (trim) can not be resolved.
SQLSTATE: IX000 at /dev/stdin:1
SQL[2406]: select trim('2017-02-01', '-') from dual;
SQL -674: Routine (trim) can not be resolved.
SQLSTATE: IX000 at /dev/stdin:2
The manual says you need TRIM({BOTH|LEADING|TRAILING} [char] FROM source):
SQL[2407]: select trim(both '-' from '2017-02-12') from dual;
2017-02-12
SQL[2408]: select trim(both '-' from '2017-02-12-') from dual;
2017-02-12
SQL[2409]: select trim(both '-' from '-2017-02-12-') from dual;
2017-02-12
SQL[2410]:
(The SQL command interpreter used is my SQLCMD.)
There's a chance that someone has defined a TRIM function that takes three arguments — that's not detectable from here. You'd have to look in the system catalog of your database to find that.
OTOH, that doesn't seem to be allowed, either:
SQL[2411]: create function trim(a varchar(10), b varchar(20), c varchar(30)) returning varchar(64);
return trim(leading from a) || ' ' || trim(both from b) || ' ' || trim(trailing from c);
end function;
SQL -9710: Overloading of built-in functions is not allowed.
SQLSTATE: IX000 at /dev/stdin:8
SQL[2412]:
Informix 11.50 has build in trim() function: https://www.ibm.com/support/knowledgecenter/SSGU8G_11.50.0/com.ibm.sqls.doc/ids_sqs_1556.htm But it looks little different from trim() in your question, so I think you use trim() function build by some user.
Normally trim() takes care only at chars at beginning or at end of the string, but in your example trim() had to remove - chars that are in the middle of the string. I guess that last argument A tells trim() to remove all such characters from source string.
To find out what trim() function really do you must find it source. You can do it with some GUI database tools like SQirreL SQL Client (it uses JDBC), you can do it with dbschema Informix utility or with my Python/Jython program that uses ODBC/JDBC: https://code.activestate.com/recipes/576621-dump-informix-schema-to-text
It would be helpful to tell us what version of Informix you use.
Trim(CollectFrom.collect_from,"-","A")
Here A means to Remove all the occurrences of '-' from the input string CollectFrom.collect_from . Hence You are getting '20170227' . Here is the document which would give further more information on Trim function.
https://www.ibm.com/support/knowledgecenter/en/SSZJPZ_11.5.0/com.ibm.swg.im.iis.ds.parjob.dev.doc/topics/r_deeref_String_Functions.html

psql adding blank spaces to a text

Hi I'm new to psql and I have the problem that I have a VARCHAR as variable in a function and I want to add blank spaces before and after it. Something like this:
CREATE FUNCTION func(word VARCHAR)
RETURNS VARCHAR
AS
$word2$
DECLARE
word VARCHAR;
word2 VARCHAR;
BEGIN
word2 = ' ' word ' ' ;
RETURN word2;
END;
$word2$
LANGUAGE plpgsql ;
Is there a quick and easy way?
Thanks.
you declare:
word VARCHAR;
and yet you have IN argument with same name:
CREATE FUNCTION func(word VARCHAR)
What happens is: function ignores argument word, because you redeclare it. From docs:
If the DEFAULT clause is not given then the variable is initialized to
the SQL null value
You don't have := 'come value' after word VARCHAR so it becomes null.
On the other hand concatenation operator || does not ignore NULL as concat() function, so the result of ' ' word ' ' always be NULL, no matter what.
Here is example of working function:
t=# CREATE OR REPLACE FUNCTION func(word VARCHAR)
RETURNS VARCHAR
AS
$word2$
BEGIN
RETURN ' '||word||' ';
END;
$word2$
LANGUAGE plpgsql ;
CREATE FUNCTION
Time: 13.018 ms
t=# select func('text');
func
--------
text
(1 row)
Time: 0.260 ms

Add days to date in SAP HANA stored procedure

I need to add days to date in HANA stored procedure, but I am getting error message
ERROR WHILE parsing DATE/ TIME
I use this statement where p_end_date is parameter of my stored procedure.
v_end_date_plus := add_days (TO_DATE(' || p_end_date || ' , 'YYYY-MM-DD' ), 90)
Is there is any other way or what I am doing wrong in it ?
Even though you didn't post what error you receive, I guess that the problem in your code is the way you referenced your input variable.
v_end_date_plus := add_days ( :p_end_date , 90);
With the colon (:) in front of the parameter you should be able to use it without having to cast it into a different data type.
#LarsBr. is correct that you need a colon (:) to reference the variable, and that if it is really a DATE type, you don't need to convert TO_DATE again.
But additionally, in your example you have some mixup with quotes and concatenation that makes me think that you actually want to construct some character string using p_end_date. This would need conversion to a date first:
p_end_date := '2016-05-03'; -- for example
v_end_date_plus := add_days(TO_DATE( :p_end_date , 'YYYY-MM-DD' ), 90);
The part ' || p_end_date || ' in your example also looks a bit like the whole code was actually part of string to be used in EXEC or similar. If that's the case, you need to have escaped single-quotes for both parameters, e.g.
exec 'v_end_date_plus := add_days(TO_DATE(''' || :p_end_date || ''', ''YYYY-MM-DD'' ), 90)';
p_end_date should be a varchar field, or the appropriate string literal used in your technology. It should not be enclosed in quotes.
v_end_date_plus := add_days (TO_DATE(p_end_date , 'YYYY-MM-DD' ), 90)
EXPLANATION USING ORACLE AS REFERENCE :
In Oracle database, the default date format is dd-MON-RR or dd-MON-YYYY.
So, if I use correct date format to p_end_date variable, I am able obtain output.
However, if I diverge from this default format,my attempt would error out.
So, if I want the flexibility to redefine p_end_date in format of my choice, and not as per default settings, it should be a String literal.(varchar in Oracle ).
EDIT:
The essence of this answer was just to suggest that the variable should be passed as a varchar. Borrowing from Lars Br's suggestion below to modify the p_end_date variable's syntax:
v_end_date_plus := add_days (TO_DATE(:p_end_date , 'YYYY-MM-DD' ), 90)

Execute immediate 'Create or replace view' gives 00900. 00000 - "invalid SQL statement" error

I have been stuck with this error. Can you please suggest what the mistake is in the code below? This is written inside a procedure that takes input for the variables. Thanks.
execute immediate 'Create or replace view '||p_viewname||' AS
select * from (
select NAME, CODETYPE, CODE
from HLIDEV_VIEWS.V_CONCEPT
WHERE HLIDEV_VIEWS.V_CONCEPT.CONCEPTTYPE = '''||p_concepttype||'''
)
pivot
(
max(code)
for codetype in ('||all_codetypes||')
)';
Update: Below is the error I am seeing.
EXEC "HLIDEV_VIEWS"."FinalCreatePOAView" ('POA_CONCEPT_TYPE','PresentOnAdmission')
Error report -
ORA-00911: invalid character
ORA-06512: at "HLIDEV_VIEWS.FinalCreatePOAView", line 39
ORA-06512: at line 1
00911. 00000 - "invalid character"
*Cause: identifiers may not start with any ASCII character other than
letters and numbers. $#_ are also allowed after the first
character. Identifiers enclosed by doublequotes may contain
any character other than a doublequote. Alternative quotes
(q'#...#') cannot use spaces, tabs, or carriage returns as
delimiters. For all other contexts, consult the SQL Language
Reference Manual.
*Action:
What you are trying to do is perfectly doable using execute immediate. You just have a syntax error somewhere or the table really doesn't exist.
Try writing the string you are building to DBMS_OUTPUT.PUT_LINE instead of sending it to EXECUTE IMMEDIATE. Then, try to run that string as a command in SQL*Plus. That should highlight the error.
FWIW, I don't have your objects in my database, but I wrote an EXECUTE IMMEDIATE with the same form as yours and it works fine in Oracle 11g2.
DECLARE
p_view_name VARCHAR2(61) := 'VXSMIMMCP.MATT_V';
p_region_code VARCHAR2(30) := 'R01';
p_all_port_codes VARCHAR2(400) := '''P01'',''P02'',''P03''';
BEGIN
EXECUTE IMMEDIATE
'Create or replace view '
|| p_view_name
|| ' AS select * from ( select REGION_CODE, account_number , port_code from APPS.XXCUS_ACCOUNTS WHERE APPS.XXCUS_ACCOUNTS.REGION_CODE = '''
|| p_region_code
|| ''' ) pivot ( min(account_number) for port_code in ('
|| p_all_port_codes
|| ') )' ;
END;
Thank you #MatthewMcPeak and #MahendarMahi for your inputs. Suddenly my code started working fine and it created the view the way I wanted. The only thing I did was, as #MahendarMahi suggested, I removed pivot and ran it to see the same error and then replaced pivot and the error was gone. I am sure there is no change in the code but anyhow it weirdly works now. Thank you again for your time.

Resources