We have connected redshift DB in sql-workbench/aginity. I am able to create function but I couldn't EXECUTE that created function in workbench. Below is the sample syntax we tried to call the function.
execute public.test_function
execute test_function
By the way, is the correct syntax to execute a function?
As documented in the manual execute is used to run a prepared statement - not a function.
To "execute" a function, call it with a select statement:
select test_function();
Related
Trying to create trigger which is supposed to use NEW and OLD and also accept table name as param, but always getting error:
You cannot directly call a trigger function from SQL or PL/pgSQL. So this line is wrong
EXECUTE function_update(table_name );
for three reasons:
EXECUTE takes a string containing an SQL function as argument. So PostgreSQL wants to call your function and execute the result as an SQL statement. You mean PERFORM function_update(table_name);, but that is also wrong for the following reasons.
You supply an argument to the function, but you defined it without parameters. This causes the error message.
You are trying to call a trigger function in an SQL statement. That will always fail.
You shouldn't have defined function_update as trigger function, but as normal function that can be called from SQL.
Is it possible to issue a "Show Shares" function call in either a FUNCTION or PROCEDURE in Snowflake? Since there isn't a metadata object in the Informational schema to query information about shares we are looking for a way to create a UDF to obtain this metadata and act on the results.
When attempting to execute the following code:
CREATE OR REPLACE PROCEDURE get_share_metadata()
RETURNS VARIANT
LANGUAGE javascript
AS
$$
var x=snowflake.execute( { sqlText: "SHOW SHARES" } );
$$
;
call get_share_metadata();
The following error is returned:
Execution error in store procedure GET_SHARE_METADATA: Stored procedure execution error: Unsupported statement type 'SHOW SHARE'. At Snowflake.execute, line 2 position 20
You can execute a SHOW .... command inside of a stored procedure ONLY if the procedure is setup to be EXECUTE AS CALLER.
When executing as caller, the role executing the procedure must have privileges to execute the commands within the procedure.
Examples:
SHOW SHARES is only accessible to the ACCOUNTADMIN role (technically any role can run the command successfully but results are limited to only the accountadmin).
SHOW USERS is only able to be successfully executed with any role that has the MANAGE GRANTS privilege on the account.
Documentation:
https://docs.snowflake.net/manuals/sql-reference/stored-procedures-usage.html#label-owners-rights-stored-procedures
I am not sure about function vs. stored procedure for this, but depending on what you're trying to do, have you looked into just using RESULT_SCAN?
SHOW SHARES;
SELECT * FROM TABLE(RESULT_SCAN(last_query_id()));
Is there a fundamental difference in DBeaver between executing a script and executing the same statements as "Execute Statement" instead?
With the following PostgreSQL script
SELECT TXID_CURRENT();
SELECT TXID_CURRENT();
If I execute both as part of a single "Statement", it looks like they are executed within the same transaction
While if I execute the "script", they seem to be fired individually (not inside a common transaction)
Is this a feature of DBeaver or PostgreSQL?
From the manual:
-> Execute SQL Statement. This executes the SQL query under cursor or selected text and fills the results pane with the query results.
-> Execute SQL Script on the main menu or in the main toolbar. This executes all queries in the current editor (or selected queries) as a script. DBeaver parses queries one by one using a statement delimiter (“;” by default) and executes them consecutively.
I have the following MySQL script which I want to implement in PostgreSQL.
SET #statement = search_address_query;
PREPARE dynquery FROM #statement;
EXECUTE dynquery;
DEALLOCATE PREPARE dynquery;
How can I define user defined variable "#statement" using PostgreSQL.
Postgres does not normally use variables in plain SQL. But you can do that, too:
SET foo.test = 'SELECT bar FROM baz';
SELECT current_setting('foo.test');
Read about Customized Options in the manual.
In PostgreSQL 9.1 or earlier you needed to declare custom_variable_classes before you could use that.
However, You cannot EXECUTE dynamic SQL without a PL (procedural language). You would use a DO command for executing ad-hoc statements (but you cannot return data from it). Or use CREATE FUNCTION to create a function that executes dynamic SQL (and can return data in any fashion imaginable).
Be sure to safeguard against SQL injection when using dynamic SQL.
Related:
Is there a way to define a named constant in a PostgreSQL query?
I am new to postgres and running following dynamic query
EXECUTE 'Select * from products';
I get following response.
ERROR: syntax error at or near "'Select * from products'"
LINE 1: EXECUTE 'Select * from products';
I Know this would be something basic I m missing
There is the EXECUTE statement of plpgsql, which would do what you are trying to do - execute an SQL query string. You tagged dynamic, so this may be what you are looking for.
Only works inside plpgsql functions or DO statements (anonymous code blocks). The distinction between EXECUTE and SQL-EXECUTE made clear in the fine manual:
Note: The PL/pgSQL EXECUTE statement is not related to the EXECUTE SQL
statement supported by the PostgreSQL server. The server's EXECUTE
statement cannot be used directly within PL/pgSQL functions (and is
not needed).
If you want to return values from a dynamic SELECT query as your example indicates, you need to create a function. DO statements always return void. More about returning values from a function in the very fine manual.
From the fine manual:
Synopsis
EXECUTE name [ ( parameter [, ...] ) ]
Description
EXECUTE is used to execute a previously prepared statement.
So EXECUTE doesn't execute a string of SQL, it execute a prepared statement that is identified by a name and you need to prepare the statement separately using PREPARE:
=> prepare stmt as select * from products;
=> execute stmt;
-- "select * from products" output goes here...