I am basically trying to do this MySQL script in BigQuery:
CREATE PROCEDURE `test` (IN tab_name CHAR(40) )
BEGIN
SET #query =
CONCAT(
'SELECT *
FROM ', tab_name );
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
In theory, this should let me run this script:
CALL test(`my_dataset.my_table`)
and it will perform my "SELECT * FROM" script on that table.
So far, this isn't working for me because BQ doesn't want to accept the quotation marks breaking across lines, so it says there is an "Unclosed string literal".
Any idea how I can accomplish this in BQ?
Here is my attempt:
CREATE OR REPLACE PROCEDURE `mydataset.test`(tableName STRING)
BEGIN
DECLARE queryString STRING;
SET queryString = " SELECT * FROM mydataset."||tableName||"";
EXECUTE IMMEDIATE queryString;
-- SELECT queryString;
END;
Execute the procedure:
CALL `mydataset.test`('mytable');
Related
Is there any way I can use print statements within BigQuery stored procedure? I have a stored procedure like below, I like to see how SQL statement is generated to debug the issue or any other better way to debug what stored procedure is producing etc.
CREATE OR REPLACE PROCEDURE `myproject.TEST.check_duplicated_prc`(project_name STRING, data_set_name STRING, table_name STRING, date_id DATE)
BEGIN
DECLARE sql STRING;
set sql ='Select date,col1,col2,col3,count(1) from `'||project_name||'.'||data_set_name||'.'||table_name|| '` where date='||date_id ||' GROUP BY date,col1,col2,col3 HAVING COUNT(*)>1';
--EXECUTE IMMEDIATE (sql);
print(sql)
END;
There are number of approaches for debugging / troubleshooting stored proc
One of the simplest - to see how SQL statement is generated - slightly adjust your stored proc as in below example
CREATE OR REPLACE PROCEDURE `myproject.TEST.check_duplicated_prc`(project_name STRING, data_set_name STRING, table_name STRING, date_id DATE, OUT sql STRING)
BEGIN
-- DECLARE sql STRING;
set sql ='Select date,col1,col2,col3,count(1) from `'||project_name||'.'||data_set_name||'.'||table_name|| '` where date='||date_id ||' GROUP BY date,col1,col2,col3 HAVING COUNT(*)>1';
--EXECUTE IMMEDIATE (sql);
END;
Then, you can run below to see generated SQL
DECLARE sql STRING;
CALL `myproject.TEST.check_duplicated_prc`('project_name', 'data_set_name', 'table_name', '2020-11-24', sql);
SELECT sql;
with output
As you can see here - you are missing apostrophes here where date=2020-11-24 so you can fix your stored proc
I try to produce a stored procedure that allows me to update many tables in my database but when I try to execute it with a columnstore engine, i get an error of procedure syntax not supported. I have looked for it in the web but do not manage to find where is the issue. If you have an idea, I let you the procedure here.
DELIMITER $$
CREATE PROCEDURE update_sp_aggregated()
BEGIN
DECLARE finished INTEGER DEFAULT 0;
DECLARE tableName varchar(255) DEFAULT "";
DECLARE cursor_update
CURSOR FOR
SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_name like 'sp_aggr%' ;
OPEN cursor_update;
updateAggregated: LOOP
FETCH cursor_update into tableName;
IF finished = 1 THEN
LEAVE updateAggregated;
END IF;
SET #sql = CONCAT('ALTER TABLE ', tableName, ' ADD COLUMN col2 varchar(5)');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET #sql = CONCAT('UPDATE ', tableName, ' SET col2= LEFT(col1, 1)');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP updateAggregated;
CLOSE cursor_update;
END $$
-- declare NOT FOUND handler
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET finished = 1;
DELIMITER ;
CALL update_sp_aggregated();
Finally found, so as tell in my comment, the issue was the update command was not allowed.
to solve it, you need to change the variable infinidb_vtable_mode by setting it to 0.
All is described here
and here
Here is the procedure:
CREATE PROCEDURE show #faculty VARCHAR(20)
AS
BEGIN
SELECT *
FROM tutor
WHERE title LIKE #faculty+'%'
END
Here is the procedure execution:
EXEC show 'FI'
How can I rewrite this for SQL Developer?
With Oracle 12 you can do this:
CREATE PROCEDURE show(p_faculty varchar)
AS
cur SYS_REFCURSOR;
BEGIN
OPEN cur FOR
SELECT *
FROM tutor
WHERE title LIKE p_faculty || '%';
DBMS_SQL.RETURN_RESULT(cur);
END;
/
Then run it with:
exec show_x ('FI');
I'm trying to use the following stored procedure.
DELIMITER $$
CREATE DEFINER=`root`#`localhost`
PROCEDURE `DeleteField`( IN _TABLENAME Text, IN _FIELDNAME text)
BEGIN
if exists (select * from information_schema.Columns
where table_name = _TABLENAME and column_name = _FIELDNAME)
then
alter table _TABLENAME drop column _FIELDNAME;
end if;
END
So I do Call('anytable','Anyfield') and I get the Error
Error Code:1146Table'Database._tablename'doesn't exist
This _tablename should be my parameter, not a string.
Plz some help before I hang myself, I love my life far too much.
I expect you will need to create a dynamic SQL query to do this.
An example of how to do this is at:
http://www.java2s.com/Code/SQL/Procedure-Function/Createadynamicstatementinaprocedure.htm
This would be the alter table replacement, though I have tested this.
DECLARE l_sql VARCHAR(4000);
SET l_sql=CONCAT_ws(' ',
'ALTER table ',_TABLENAME,' drop column ',_FIELDNAME);
SET #sql=l_sql;
PREPARE s1 FROM #sql;
EXECUTE s1;
DEALLOCATE PREPARE s1;
I have insert statement in DB2 database table column and i want to execute that in store procedure
Table:
TB_SQL
(
DATA_SQL VARCHAR(12000)
)
DATA_SQL column contains INSERT statements. for example :
SELECT * FROM TB_SQL
INSERT INTO ADDRESS (COL1, COL2)
SELECT COL1, COL2 FROM DEMO WHERE TYPE='ADDRESS'
How to PREPARE and EXECUTE the INSERT STATEMENT from DATA_SQL Column in DB2 Store Procedure ?
Code I tried:
SET v_SQL=
'SET ?=(
SELECT
DATA_SQL
FROM TB_SQL
WHERE TBNAME='''||v_TBNAME||'''
)
';
PREPARE SQL_QUERY FROM v_SQL;
EXECUTE SQL_QUERY ;
In reality the above PREPARE AND EXECUTE will only prepare SELECT statement and execute SELECT statement.
What i want is, I want to execute the statement that comes as output from SELECT statement.
I also have below option to store the INSERT statement again in another variable
SET v_SQL=
'SET ?=(
SELECT
DATA_SQL
FROM TB_SQL
WHERE TBNAME='''||v_TBNAME||'''
)
';
PREPARE SQL_QUERY FROM v_SQL;
EXECUTE SQL_QUERY INTO v_INSERT_STATEMENT;
PREPARE SQL_INSERT FROM v_INSERT_STATEMENT;
EXECUTE SQL_INSERT;
I believe now i have insert statement stored in v_INSERT_STATEMENT
And again prepared SQL from variable and then executed. But this is not working.
you don't need two dynamic statements...one static and one dynamic makes much more sense.
select data_sql into v_Insert_statement
from tb_sql
where tbname = v_TBNAME;
PREPARE SQL_INSERT FROM v_INSERT_STATEMENT;
EXECUTE SQL_INSERT;
SET v_SQL=
'SET ?=(
SELECT
DATA_SQL
FROM TB_SQL
WHERE TBNAME='''||v_TBNAME||'''
)
';
PREPARE SQL_QUERY FROM v_SQL;
EXECUTE SQL_QUERY INTO v_INSERT_STATEMENT;
PREPARE SQL_INSERT FROM v_INSERT_STATEMENT;
EXECUTE SQL_INSERT;