I am trying to use Database trigger and procedure to send sms I have create the table and I can insert data by:
use sms;
INSERT INTO sms_out (id, sender,receiver,msg)
VALUES ('','MyName','25578200000',"test");
I have created trigger as;
use sms;
CREATE TRIGGER push_message_trigger AFTER INSERT ON sms_out
FOR EACH ROW
CALL push_message(NEW.sender, NEW.receiver, NEW.msg);
and I have created procedure as;
use sms;
DELIMITER $$
CREATE PROCEDURE push_message
(p1 TEXT,
p2 DOUBLE,
p3 TEXT)
BEGIN
DECLARE cmd CHAR(255);
DECLARE result CHAR(255);
SET cmd = CONCAT('curl "http://www.sms.co.tz/api.php?do=sms&username=Myusername&password=MyPassword&senderid=MyName&dest=25571500000&msg=test"');
SET result = sys_eval(cmd);
END$$;
The above does not work but if I replace
SET cmd = CONCAT('curl "http://www.sms.co.tz/api.php?do=sms&username=Myusername&password=MyPassword&senderid=MyName&dest=25571500000&msg=test"');
With
SET cmd = CONCAT('touch /var/lib/mysql/Advo');
It works!!
Whats wrong with curl??
I guess the problem it's not the curl literal is how are you using mysql CONCAT funcion.
To concat strings you should do something like (if you don't use commas you aren't concatening strings):
CONCAT(str1, str2, ....);
Seems that the problem are the quotes, try to escape the double quotes like this \"
* Show the error if you want people to know what's really happening
Related
I want to return a value from Stored Procedure which I can use in my Spring Boot server in JPA repository.
Here's my Stored Procedure,I want to know how do I return String from the Procedure.
DELIMITER //
CREATE PROCEDURE GetAllProducts(out query varchar(20))
BEGIN
SET query=SELECT title FROM course where description="just a course";
RETURN #query;
END //
DELIMITER ;
I am selecting 1 value from the query table to return it.
Can someone Help?
Thanks!
If you want to use your SP as datasource (like you execute SELECT, not CALL, in external program) you must select to output stream, not to variable:
CREATE PROCEDURE GetAllProducts()
SELECT title
FROM course
WHERE description="just a course";
Got the Solution,Its working now!
Just tweaked the Procedure a little bit:
DELIMITER //
CREATE PROCEDURE GetAllProdcuts(OUT output VARCHAR(50))
BEGIN
SELECT title into output
FROM course
WHERE description="just a course";
END //
I need to write a test for some download operation. This operation call procedure from MSSQL database, take result set and java make some stuf. For test I use hsqldb.
My procedure:
CREATE PROCEDURE map.Get1(IN packageName varchar(100),
IN downloadDate DATE)
READS SQL DATA DYNAMIC RESULT SETS 1 BEGIN ATOMIC
DECLARE result CURSOR WITH RETURN FOR SELECT * FROM map.tvschedule FOR READ ONLY;
OPEN result;
END
This procedure wan't work, i have an exception
call map.GET1('Genre','2018-03-10');
[42561][-5561] incompatible data type in conversion
java.lang.RuntimeException: org.hsqldb.HsqlException: incompatible data type
in conversion
But this(without date parameter) work well:
CREATE PROCEDURE map.Get1(IN packageName varchar(100))
READS SQL DATA DYNAMIC RESULT SETS 1 BEGIN ATOMIC
DECLARE result CURSOR WITH RETURN FOR SELECT * FROM map.tvschedule FOR READ ONLY;
OPEN result;
END
call map.GET1('Genre');
first needed row
second needed row
I am not going to use input parameter, but i need this procedure to be looking i am going to.
My question is How to use date input parameter with hsqldb procedures?
UPDATE1:
I used TO_DATE and now it works well, but i have no data in my result set, my java code is:
try (CallableStatement callableStatement = connection.prepareCall("{ call
map.GetGenreProtocol( ?, ? ) }")) {
callableStatement.setString(1, packageName);
callableStatement.setDate(2, date);
callableStatement.execute();
ResultSet resultSet = callableStatement.getResultSet();
while (resultSet.next()) {
Interval Interval = new Interval();
Interval.setDuration(resultSet.getInt("duration"));
Interval.setMappingTargetId(resultSet.getInt("mappingTargetId"));
Interval.setGenreId(resultSet.getInt("genreId"));
Interval.setStart(resultSet.getLong("start"));
Interval.setCategoryId(resultSet.getInt("categoryId"));
Interval.setCategoryName(resultSet.getString("categoryName"));
Interval.setGenreName(resultSet.getString("genreName"));
Interval.setDescription(resultSet.getString("description"));
Intervals.add(Interval);
}
}
Use the TO_DATE function.
For example:
call map.GET1('Genre', TO_DATE('2018-03-10', 'YYYY-MM-DD'));
I guess you need to create a function that returns a table instead of a procedure:
CREATE FUNCTION map.Get1(IN packageName VARCHAR(100),
IN downloadDate DATE)
RETURNS TABLE(.....)
READS SQL DATA
BEGIN ATOMIC
....
END;
I've spent hours and I can't understand why this is highlighted red and I don't know what my error is.
CREATE FUNCTION Student_Section_Count(StudentID INT)
Returns INT
Begin
DECLARE section_Registred INT;
SET section_Registred= (Select COUNT(DISTINCT Section.ID) as 'Students Registration Count'
FROM Section
Inner Join
Registration on registration.StudentID=Section.ID
Where registration.StudentID=StudentID);
Return Section_Registred;
END$$
Delimiter;
It highlights END and Delimiter, as well as INT from Return INT
By default semi-colon ; is the delimiter in mysql. So when using a mysql client, the function definition that is passed to server is only up to the first semi-colon i.e. DECLARE section_Registred INT;. Rest of the definition of function is not passed to server. This produces an error. To incorporate semi-colon in function definition you need to change your delimiter from semi-colon to some thing else. So before defining the function write the below sql to change the delimiter:
DELIMITER $$.
After the above sql write sql for your function. Append the new delimiter $$ to END at the end of definition of your function. Now the whole definition of function is passed to server up to the new delimiter. This will fix your error. Change the delimiter back to default after your function definition ends as below:
DELIMITER ;.
Also remember to choose a delimiter that is not present anywhere inside your function definition.
Well, I guess I figured it out on my own.
DELIMITER $$
CREATE FUNCTION Student_Section_Count(StudentID INT)
Returns INT
Begin
DECLARE section_Registred INT;
SET section_Registred= (Select COUNT(DISTINCT Section.ID) as 'Students Registration Count'
FROM Section
Inner Join
Registration on registration.StudentID=Section.ID
Where registration.StudentID=StudentID);
Return Section_Registred;
END$$
I am making a MySQLand getting an error I dont know how to fix it. What the function should be doing is asking the user for input, this input should be stored in the corresponding variables and return a variable. But when i try run the function I am getting errors on the lines that store the variables. I am quite new to mySQL so i might be doing something stupid. Any help will be great thank you
This is is the mysql function
create or replace FUNCTION AD_AGENCY_INFO(agency_id in agency_id%type)
RETURN agency_id
DECLARE
v_no_of_runs AD_AGENCY.NO_OF_AD_RUNS%TYPE = '&enter_number_of_Runs';
v_credit_worthy AD_AGENCY.CREDIT_WORTHY%TYPE = '&enter_credit_worthy';
v_available_slots AD_AGENCY.AVAILABLE_SLOTS%TYPE = '&enter_available_slots';
v_status AD_AGENCY.STATUS%TYPE = '&enter_status';
BEGIN
insert into ad_agency values (agency_id, v_no_of_runs, v_credit_worthy,v_available_slots, v_status);
insert into ad (agency_id) values (agency_id);
commit;
RETURN (agency_id));
END AD_AGENCY_INFO;
The error that i am getting is the following and is the same for lines 7,8,9
Error(6,45): PLS-00103: Encountered the symbol "=" when expecting one of the following: := ( ; not null range default character The symbol ":= was inserted before "=" to continue.
Try changing your declare statement(s) to be
v_no_of_runs AD_AGENCY.NO_OF_AD_RUNS%TYPE := '&enter_number_of_Runs'
delimiter //
CREATE DEFINER=root#localhost PROCEDUREgetData(IN templateName VARCHAR(45),IN templateVersion VARCHAR(45),IN userId VARCHAR(45))
BEGIN
set #version = CONCAT("SELECT saveOEMsData_answersVersion FROMsaveOEMsData WHERE saveOEMsData_templateName = '",templateName,"' ANDsaveOEMsData_templateVersion = ",templateVersion," AND saveOEMsData_userId= ",userId);
PREPARE s1 from #version;
EXECUTE S1;
END // delimiter ;
I am retrieving saveOEMsData_answersVersion, but I have to use it in an IF loop, as in if the version == 1, then I would use a query, else I would use something else. But I am not able to use the version. Could someone help with this?? I am only able to print but not able to use the version for data manipulation. The procedure works fine but I am unable to proceed to next step which is the if condition.
The if condition would have something like the below mentioned.
IF(ver == 1) THEN SELECT "1";
END IF;
IF is allowed inside a Procedure, you can not use IF inside SQL, in SQL you can use CASE, but that doesn't have the same abilities.
Build a second procedure to handle the IF/Then for the results of getData