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.
Related
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.
I'm working on a desktop-application, database, webserver-access combination which someone wrote some years ago. My task is to do some optimications/refactoring and to introduce new features to this application(s). I have not much experience in developing web applications, so it's quite difficult for me to find a solution to my problem described below, hoping someone can help me.
The webapplication is written with ASP and VBScript having some small javascript functions which do not affect my question. It uses ADODB for communicating with the database.
The database is a MS-SQLserver 2008 database.
The desktop-application is written in C++/CLI using the .Net built-in features for communicating with the database. With this application everything is working.
For introducing some features I need to add new columns to tables in the database. Inserting and updating of the main table is done with stored procedures. I added a column named "internal" of type "bit":
ALTER TABLE maintable
ADD internal bit
GO
I altered the stored procedures for inserting and updating, just by adding the internal column and a parameter for it. I made only these changes:
1x line for the parameter
added internal in the column and values list for the insert
added setting internal column with parameter for the update
In the vbscript which already was working before any changes I added the code for appending the value for internal as new parameter (its the same for insert & update):
sqlcmd.Parameters.Append(sqlcmd.CreateParameter("#internal",11,1))
sqlcmd.Parameters("#internal")=0
After these changes the update und insert procedures of the vbscript stopped working. I tried several datatypes for the parameter and also changing the column (different name, different datatype). Nothing worked. The stored procedures themself are working fine when executed directly in the database and also when used by the desktop-application.
I started to debug everything with printing some debuginformations ect. I added try/catch in the stored procedures and a outparameter to get some errors according to this answer and I selected all input-parameters into a varchar outparameter. This caused the next strange results.
While updating "worked" (because the stored procedure didn't cause a database error and I got the input-parameter informations which didn't show wrong values, but no update was performed) inserting didn't workin any way. My tracing outputs where printed till the sqlcmd.Execute, this line seems to crash since the trace outputs after this line wheren't printed. As long as I did not use the outparameter the insert itself didn't work, but all trace outputs got printed. I tried to retrieve information about a possible database error directly after the execution of the code with:
DECLARE #ErrorVariable INT;
SET #ErrorVariable = ##ERROR;
SELECT #ErrorVariable AS ErrorID,
text
FROM sys.messages
WHERE message_id = #ErrorVariable;
GO
There was no database error.
Everything works fine from the desktop-application side. As mentioned the stored procedures executed directly in the database will work properly. I suppose the the error is somewhere in web-scripting-stuff. So now here are the concrete questions:
Why would the stored procedure not work (properly) when adding a new column to the database (it is there) and no syntax errors in the stored procedures or vbscript?
Why the sqlcmd.Execute stops working when adding a outparameter to the insert stored procedure? The try-block in the stored procedures includes everything between "AS BEGIN" and "END" having the catch-block directly before "END". Syntax is here also correct.
try to catch the error at the asp end.
On Error Resume Next
sqlcmd.Execute
for each objerr in yourconnection.Errors
Response.write objerr.Description & "<br/>"
next
On Error GoTo 0
Please check this link:
ADO Connection Object Errors Collection
I've declared an handler which will handle SQLEXCEPTION, in the clean up code i had it do SELECT 'My Handle';, i ran a script that fails because of a primary key violation and it worked cause i got my output.
the problem with DECLARE ... HANDLE FOR SQLEXCEPTION is that when there is an error it will run though it's doesn't say what error triggered it, so i want to output the error
How do i output the SQL Error using a MySQL Query, i don't care if i can only output the error code/id i just need something to output giving me an indication on what the error is so i can fix the problem
EDIT: in case if it's not obvious, this code is in an SQL Procedure
There is nothing in the DECLARE HANDLER documentation on this functionality. You could handle the errors MySQL returns to your application and print or log them that way.
I actually haven't used any handles, but i think you can make your code into a stored procedure and then run the stored procedure!! You might find MySQL accepts what you are trying to do then.
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.
I want to abort/quit/return from a stored procedure when a condition is not met.
I'm not sure how to do it. Googling didn't help me much because:
The version of MySql on our servers is 5.1 (<5.5 and hence no signal sqlstate).
I don't want to put everything in if..then..else statements (there will be multiple levels of nesting in my case)
I don't want to call an_invalid_procedure to raise an exception.
Are there alternatives?
Something wrong with....
...
BEGIN
<do stuff>
IF (<your condition>) THEN
<do more stuff>
END IF;
END$$
What I do in such cases is to:
Create table called errors with one column containing textual error messages and UNIQUE index on it. I fill the table with errors I want to support.
When I need specific error to raised, I insert the error message into errors table. Since it's already there, I am violating a UNIQUE key so error is raised. In my application I can watch for MySQL error #1062 (that's the code for 'Duplicate value for key...' error and use regexp to extract my error message to show it to user/log it/whatever;