I'm trying to apply a routine to a database using MYSQL workbench but I'm having a few problems.
In the first image below, you see the mysql I'm using. This mysql has worked for someone else (i.e. the author of the book I'm following), but when I enter it, there's three error warnings (the Xs in the red boxes).
The other two images below show what happens after I hit apply the first time(showing me the SQL to be applied on the database), and then the second time (producing the error message)
Can anyone see how to fix this problem?
Note, the code that's being entered is a formula to calculate distance between two points, but, as said, it's worked for the author of the book I'm using (Larry Ullman's PHP 5)
I think it's to do with your DELIMITER declaration missing.
You need to add:-
DELIMITER $$ on line 1 before your function.
Then remove the space from END $$ so it becomes END$$
Related
I am trying to add a trigger through phpmyadmin 4.4
I get the error:
Why is this happening? How to declare a variable?
Start with BEGIN, end with END. Put the various statements between them.
Notice in the error how it is alread providing FOR EACH ..., but it is depending on you to do the rest. See http://dev.mysql.com/doc/refman/5.7/en/triggers.html (and other pages) for the complete syntax that phpmyadmin is "helping" you generate.
I'm trying to use a label statement in the MySQL exactly as described in doc.
I permanently get an error on the colon behind label :
What is wrong?
I already tried XAMPP with the MariaDB as well as WAMP with the MySQL. I try as simple procedure as possible. From SQL tab in phpMyAdmin as well as from file script in Import. All the same : Unexpected character near ":".
I found issue that the TAB in the procedure definition may cause problem. I have not there any TAB. I tried loop and LOOP, begin vs. BEGIN.... still same... Please help.
You have END LOOP try changing it to END LOOP loop1; see MySQL document.
Your code is confirmed to work fine in MySQL 5.6 -- here's a SQL Fiddle: http://sqlfiddle.com/#!9/c00911/1. I also suspect the phpMyAdmin client.
Try putting the label on its own line, or taking the numeric out of the label (take the word loop out of it, too -- maybe it's parsing it funny). Call it var_counter maybe.
In MySQL:
I have an interview table, and
when a new interview is created (a row inserted into interview),
I want to add rows to a child table interview_questions table linking to the question definitions in interview_questions_template.
I'm trying to do this in an AFTER INSERT trigger on interview, and mysql is saying I have a syntax error at the end of my INSERT INTO ... SELECT statement.
I've tried joining with NEW, thinking it might be a table, but that didn't work either. Gander at the code?
CREATE TRIGGER interview_AFTER_INSERT AFTER INSERT ON interview
FOR EACH ROW
BEGIN
INSERT INTO interview_questions (id_interview, id_candidate, id_question_def, s_userid)
SELECT NEW.id_interview, NEW.id_candidate, interview_question_template.id_question_def, NEW.s_userid
FROM interview_question_template;
END
The error mysqlworkbench is showing is "missing 'semicolon'", underlining interview_question_template after the FROM.
The execution error says there is an error on that line after ' '.
You are doing fine. Just wrap the whole thing in a delimiter block. The below survives the 1064 Error.
DELIMITER $$
CREATE TRIGGER interview_AFTER_INSERT AFTER INSERT ON interview
FOR EACH ROW
BEGIN
INSERT INTO interview_questions (id_interview, id_candidate, id_question_def, s_userid)
SELECT NEW.id_interview, NEW.id_candidate, interview_question_template.id_question_def, NEW.s_userid
FROM interview_question_template;
END
$$
DELIMITER ;
As for the importance of Delimiters, see the bottom of This Post of mine. I wrote it more eloquently elsewhere, just can't find a reference to it. And the mysql manual has little about it with any verbosity for the average human.
I am sorry for posting the obvious. Sometimes people just have to see it :P
The other thing I did was to simply remove the BEGIN/END and it also worked like a charm.
I'm still relatively new to SQL Server but love a lot of things about it, except for the array of "all-slightly-different-but-none-can-do-everything", "finicky-in-different-ways" scripting options where, just when you feel like you're starting to get a handle on things and are cruising, you slam into yet another roadblock. I've been down the dynamic SQL path (and have found the restrictions on variables having short lifetimes) and as per a previous suggestion that I received ( Script to create a schema using a variable ), am now trying to write sqlcmd scripts instead.
A lot of scripts work fine and dandy if you run them "naked". As soon as you put some of them into a Try / Catch block to implement error handling on them, however, you often run into ridiculous restrictions most notably DDL commands which "vant to be alone" and need to be the first/only statement in a batch. Go is useless in this context because if you put THAT anywhere inside a Try/Catch block it guarantees that you'll get a syntax error.
Obviously I've scoured the web on this (and have looked at some of the "similar questions" that appeared while editing this post) but keep coming up with examples which are either "naked" in the sense above, or are Try/Catch examples on code which doesn't have these restrictions.
In the case of creating a schema, I used the approach that had been suggested to me for dynamic SQL; I ran it through sp_executesql. That's not a problem since it's essentially one line of code, the problem is that I hit it again when I tried to create a trigger on a table (and am guessing that I will with some other Create commands).
CREATE TRIGGER MySchema.NoDelete
ON MySchema.MyTable
INSTEAD OF DELETE
AS
BEGIN
SET NOCOUNT ON;
RAISERROR ('Deletions are not allowed on this table', 16,1)
END
Run this by itself and it's fine. Put Begin Try before it and End Try and a Catch block after it and you get:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'TRIGGER'.
with a red squiggly line on BEGIN with the bogus tool tip "Incorrect syntax near Begin, expecting External".
I tried the sp_executesql path with this again but:
First, it also generated a bogus "Syntax error near $" error which is about as useful as telling me that there's some syntactical error, somewhere, between here and the planet Zargthorp" but more importantly:
Second, even if I did get it to work for a relatively trivial trigger like this one, I'm having nightmares trying to imagine packaging a complex, multi-line trigger in such a fashion but even if I DID get past that;
Third, it would make the code much more obscure and defeat one of the purposes of using scripts in the first place, that being self-documentation.
My questions therefore are:
Is Try/Catch effectively useless for commands which, for reasons best known to MS designers, need to live in isolated majesty like Create Trigger (and which aren't one-liners like Create Schema which can be neatly packaged up into sp_executesql); or
In my relative newbieness have I missed some other way of working around the kind of restriction that I've slammed into with Create Trigger?
Thanks in advance for any responses.
In a sql script that does sequential execution, is there a way one can introduce an IF THEN ELSE conditional to control the flow of query execution?
I happened to run into this http://www.bennadel.com/blog/1340-MySQL-Does-Not-Support-IF-ELSE-Statements-In-General-SQL-Work-Flow.htm
which says that the IF THEN ELSE will not work in a sql script.
Is there another way around?
Basically, I want to run a particular "select colName from table" command and check if colName corresponds to a particular value. If it does, proceed with the rest of the script. Else, halt execution.
Please advise.
I just wrap my SQL script in a procedure, where conditional code is allowed. If you'd rather not leave the statements lying around, you can drop the procedure when you're done. Here's an example:
delimiter //
create procedure insert_games()
begin
set #platform_id := (select id from platform where name = 'Nintendo DS');
-- Only insert rows if the platform was found
if #platform_id is not null then
insert into game(name, platform_id) values('New Super Mario Bros', #platform_id);
insert into game(name, platform_id) values('Mario Kart DS', #platform_id);
end if;
end;
//
delimiter ;
-- Execute the procedure
call insert_games();
-- Drop the procedure
drop procedure insert_games;
If you haven't used procedures, the "delimiter" keyword might need some explanation. The first line switches the delimiter to "//" so that we can include semi-colons in our procedure definition without MySQL attempting to interpret them yet. Once the procedure has been created, we switch the delimiter back to ";" so we can execute statements as usual.
After doing some research I think I may have found a way to work around this. I was looking for a way to verify if a script had already executed against a target database. This will be primarily for version control of my databases. I have a table created to keep track of the scripts that have been executed and wanted some flow inside my scripts to check that table first before execution. While I have not completely solved the problem yet I have created a simple script that basically does what I need, I just need to wrap the DDL into the selects based on the value of the variables.
step 1 - Setup a bit variable to hold the result
step 2 - do your select and set the variable if the result is found
step 3 - Do what you need to do on false result
step 4 - Do what you need to do on true result
Here is the example script
set #schemachangeid = 0;
select #schemachangeid := 1 from SchemaChangeLog where scriptname = '1_create_tables.sql';
select 'scriptalreadyran' from dual where #schemachangeid = 1;
select 'scriptnotran' from dual where #schemachangeid = 0;
I also recognize this is an old thread but maybe this will help someone out there trying to do this kind of thing outside of a stored procedure like me.