开发者

MySQL user-defined variables compared to MSSQL-server user-defined variables

So, the first thing I see as different now that I've moved from a SQL-server shop to a MySQL shop is that user-defined variables are a little wonky. I found it a little annoying to have to declare all my variables in sql-server, but user-defined variables seem really fast and loose in comparison.

For example, if I use an undeclared variable in sql-server, i get an error. If I use an undeclared variable in MySQL, I get a bunch of null values inserted into my table. Is there any开发者_StackOverflow中文版 way to make variables behave more like they way they do in sql-server?


There's not an "in-the-box" way that I'm aware of. I'd assume you're writing scripts or procedures. I'd recommend that you validate all variables where appropriate. Then, you can raise an error (using SIGNAL) when the variable is null. You have to code it, regretfully, but it should do what you want, and you should get an error back to your application that you can then parse.

http://dev.mysql.com/doc/refman/5.5/en/signal.html

CREATE PROCEDURE foo (VAL INT)
BEGIN
    IF val IS NULL THEN
        SIGNAL SQLSTATE '45000'
        SET MESSAGE_TEXT = 'An error occurred';
    END IF;
END
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜