Teradata deletes multiline comments when creating/replacing procedure - teradata-sql-assistant

I`ll forgive to help with teradata 16.20 Maybe somebody faced such a problem
When i try to create/replace procedure in TD it works but every time i lost multiline commens.
Example:
REPLACE PROCEDURE test.RAS_TEST ()
BEGIN
DECLARE QQQQ INTEGER;
SET QQQQ = 0;
/*
DELETE FROM test.table
WHERE DAY_ID BETWEEN BEGIN_DATE AND END_DATE;
COMMIT;
*/
-- TEST
/*
TEST.WITH.STRING
*/
END;
And after:
SHOW PROCEDURE test.RAS_TEST;
Result:
SHOW PROCEDURE test.RAS_TEST;
REPLACE PROCEDURE test.RAS_TEST ()
BEGIN
DECLARE QQQQ INTEGER;
SET QQQQ = 0;
-- TEST
END;
When another person who will do SHOW PROCEDURE will see the same BUT if another person copies the code and executes it, the result of SHOW will be complete, with all the lines, including multiline comments.

Related

MySQL Workbench stored procedure END with delimiter not working

I'm using MariaDB to create a stored procedure using this code:
DROP PROCEDURE IF EXISTS P1;
DELIMITER // /* Set delimiter */
CREATE PROCEDURE P1(IN `date` DATE, IN `customer` INT, IN `bookingLocation` INT)
BEGIN /* Begin procedure */
/* Declare variables */
DECLARE currDate DATE;
DECLARE customerPlan INT;
DECLARE locCapacity INT;
DECLARE existingBookings INT;
SET currDate=CURRENT_DATE();
IF `date` < currDate THEN /* If date is in past */
SELECT "DATE CANNOT BE IN PAST" AS error;
ELSE
SELECT plan INTO customerPlan FROM `customers` WHERE customer_ID = `customer`; /* Get the customer's plan */
SELECT capacity INTO locCapacity FROM `locations`;
SELECT COUNT(*) FROM `desk_bookings` WHERE location=bookingLocation;
END IF;
END// /* End procedure */
DELIMITER ;
The code runs correctly using the command line, but when I run the code in MySQL Workbench, I receive an error:
"/" is not valid at this position, expecting EOF, '/'
I was able to resolve this by removing the comment on the DELIMITER // /* Set delimiter */ line.

Why won't MYSQL procedure properly iterate new ROWS in TABLE using WHILE statement?

I am creating this basic procedure using MySQL Workbench to accept a single input parameter.
The table "unique_days" has a single PRIMARY KEY column called "dayid" which currently has a single ROW with a value of 1.
DROP PROCEDURE IF EXISTS dayid_iteration;
DELIMITER $$
CREATE PROCEDURE dayid_iteration(maxdate_final INT)
BEGIN
DECLARE maxdate_current INT;
SET #maxdate_current = (SELECT (MAX(dayid) + 1) FROM unique_days);
DELETE FROM unique_days WHERE dayid > 1;
WHILE (maxdate_current > maxdate_final) DO
INSERT INTO unique_days (dayid) VALUES (maxdate_current);
SET maxdate_current = (maxdate_current+1);
END WHILE;
END$$
DELIMITER ;
The procedure is then called with an integer parameter.
CALL dayid_iteration(11);
The variables are setting properly because I can run a select statement with the variable and it shows the correct new value. The deletion of dayid > 1 also works (Tested by manually adding additional rows, and then running procedure). However, I can't seem to get the WHILE statement to insert new rows with the value provided.
Any help is much appreciated. I searched multiple other questions, and countless forums, but everything looks like it should be working.
I am expecting the code to CREATE 9 ROWS for a total of 10 ROWS.
The following is included just so you can see the starting values of the table.
SELECT * FROM unique_days;
For anyone who finds this question, the following code functions correctly. The input variable on the parameter was not setting properly. Once the parameter had "IN" placed in front of the variable name, it correctly received the parameter.
CREATE PROCEDURE dayid_iteration(IN maxdate_final INT)
DROP PROCEDURE IF EXISTS dayid_iteration;
DELIMITER $$
CREATE PROCEDURE dayid_iteration(IN maxdate_final INT)
BEGIN
DECLARE maxdate_current INT;
SET maxdate_current = (SELECT (MAX(dayid) + 1) FROM unique_days);
WHILE (maxdate_current <= maxdate_final) DO
INSERT INTO unique_days (dayid) VALUES (maxdate_current);
SET maxdate_current = (maxdate_current+1);
END WHILE;
END$$
DELIMITER ;
CALL dayid_iteration(1000);
This call procedure now properly works.
CALL dayid_iteration(1000);

Using a cursor in a stored procedure to loop rows MySQL

Scenario: I have a stored procedure that gets data from a table based on 2 inputs: a date and a string (which is a column name). The first procedure is called from another procedure which uses a cursor to loop through rows of a table and pass each row to the string of the first procedure (column names to be checked). My input for the second procedure (which is the one to be called directly) is the date.
Issue: My first procedure is running fine when I call it on its own. My second procedure is throwing some syntax errors that I don't know how to fix.
Obs: I already check some other answers here on this topic
such as Using Cursor in a Loop of a stored procedure and How can I loop through all rows of a table? (MySQL) . Actually my second procedure is now a modified version of a query I found on SE https://dba.stackexchange.com/questions/138549/mysql-loop-through-a-table-running-a-stored-procedure-on-each-entry
Issue: Currently, the code is throwing an error at line 5, in my declare of #colval.
Code:
-- Procedure for looping through rows of `wanted_columns` table:
delimiter $$
drop procedure if exists `data_check_loop` $$
create procedure `data_check_loop`(`wanted_date` date)
begin
set #dateval = `wanted_date`;
declare colval string default null;
-- boolean variable to indicate cursor is out of data
declare done tinyint default false;
-- declare a cursor to select the desired columns from the desired source table
declare cursor1
cursor for
select t1.c1
from `wanted_columns` t1;
-- catch exceptions
declare continue handler for not found set done = true;
-- open the cursor
open cursor1;
my_loop:
loop
fetch next from cursor1 into colval;
if done then
leave my_loop;
else
call `set_column_stats`(colval, dateval);
end if;
end loop;
close cursor1;
end $$
delimiter ;
Question: Any ideas on how to fix this?
You have a couple of problems in your procedure. Firstly, as described in the manual:
DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.
So you need to move your
set #dateval = `wanted_date`;
after all the DECLAREs (including the cursor and continue handler).
Secondly, your declaration of colval is incorrect, string is not a valid data type and should be replaced with text:
declare colval text default null;

Handling unfound data in mySQL procedure loop

I think I'm narrowing in on my issue. I have a loop that is only firing once:
DELIMITER $$
DROP PROCEDURE IF EXISTS `thread_updates` $$
CREATE PROCEDURE `thread_updates`()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE my_curr_id INT DEFAULT NULL;
-- DECLARE other vars
DECLARE fixer_cursor CURSOR FOR
SELECT DISTINCT(id)
FROM log
WHERE date >= '2018-01-01';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN fixer_cursor;
REPEAT
FETCH fixer_cursor INTO my_curr_id;
IF NOT done THEN
SELECT data FROM table WHERE id = my_curr_id; -- This may not exist
-- Do other stuff with 'data' or NULL from above
END IF;
UNTIL done END REPEAT;
CLOSE fixer_cursor;
END $$
DELIMITER ;
I think the issue may be that inside the IF NOT done THEN loop, I have a few select statements that may be trying to select results that don't exist (not found).
This is fine (for me) as the logic continues along using NULL values in those spots, but I suspect that my CONTINUE HANDLER FOR NOT FOUND is catching the NOT FOUND warning that these selects throw inside the loop and are thus stopping the entire loop prematurely.
How can I listen for NOT FOUND warning on my cursor only?
Or, how can I suppress the NOT FOUND warning in my MAYBE FOUND select statements inside my loop so my loop continues?
I think I have solved the issue by implementing a counter in a loop rather than relying on the NOT FOUND handler:
DELIMITER $$
DROP PROCEDURE IF EXISTS `thread_updates` $$
CREATE PROCEDURE `thread_updates`()
BEGIN
DECLARE my_total INT DEFAULT NULL; -- Declare total
DECLARE my_counter INT DEFAULT 0; -- Declare counter starting at 0
DECLARE my_curr_id INT DEFAULT NULL;
-- DECLARE other vars
DECLARE fixer_cursor CURSOR FOR
SELECT DISTINCT(id)
FROM log
WHERE date >= '2018-01-01';
OPEN fixer_cursor;
SELECT FOUND_ROWS() INTO my_total; -- Get total number of rows
my_fixerloop: LOOP
FETCH fixer_cursor INTO my_curr_id;
IF my_counter >= my_total THEN -- Compare counter to total
CLOSE fixer_cursor;
LEAVE my_fixerloop;
END IF;
SET my_counter = my_counter + 1; -- Increment by one for each record
SELECT data FROM table WHERE id = my_curr_id; -- This may not exist
-- Do other stuff with 'data' or NULL from above
END LOOP;
END $$
DELIMITER ;

Displaying the records from a mysql stored procedure

I am just starting to learn how to write stored procedures in MYSQL and I've hit a roadblock.
I wrote the following code:
DELIMITER $$
DROP PROCEDURE IF EXISTS `emscribedx`.`countcodes` $$
CREATE PROCEDURE `emscribedx`.`countcodes` ()
BEGIN
declare doneprocessing int default 0;
declare thisaccount varchar(50);
declare countcursor cursor for select acct from patientid where patienttype='P';
declare continue handler for not found
set doneprocessing = 1;
Fetch countcursor into thisaccount;
Repeat
select * from doc_table where acct = thisaccount;
until doneprocessing = 1
END repeat;
close countcursor;
END $$
DELIMITER ;
I would like to display the results of the select statement that occurs after the repeat statement. But how do I do that? When I execute the stored procedure, nothing happens?
Thank you,
Elliott
The procedure can only return the results of a single query, why not use this instead?
SELECT doc_table.*
FROM doc_table
INNER JOIN patientid
ON patientid.acct = doc_tableacct
WHERE patientid.patienttype='P';
It can be put inside of a procedure if you want.