在mysql中使用用户定义的变量

时间:2015-10-26 00:56:52

标签: mysql sql-server

我在MSSQL下面有以下设置语句,它完美无缺:

Declare @uid INT;
Declare @ct int;
Declare @code int;
Declare @usertype int;
Declare @flag char;
Declare @max_itemid int;
Declare @ulname varchar(10);
Declare @ufname varchar(10);
set @code= 123;
set @ulname='Will';
set @ufname='Smith';
set @max_itemid=(select max(itemid) from registration;

set @uid = (select uid  from users where ulname=@ulname and ufname=@ufname)

set @ct=(select count(*) from usercodes where itemId=@uid)
set @usertype=(select usertype from users where uid=@uid)

If(@usertype=1)
begin
If (@uid>0)
Begin 
    select * from users where uid=@uid; 
    select * from usercodes where itemId=@uid;

        if(@ct=0)
        begin
            if (@max_itemid=2 or @max_itemid=0)
            set @flag='D';
            else 
            set @flag='O';

        insert into usercodes (itemId,CODE,Flag) values(@uid,@code,@flag);
        select * from usercodes where itemId=@uid;

        else
        begin
            PRINT 'UserCode is already added and below is result:';
            select * from usercodes where itemId=@uid;
        end

end
Else
    PRINT 'No User Found';

end

else
  begin
    print 'User not added as usertype is ';PRINT  @usertype;
end

现在我是Mysql的新手,想要根据Mysql约定重写上述语句。我想出了语法,并将声明更改为:

SET @CODE='S10_12345' ;
SET @NAME='Will Smith';

等等。 但是一旦进入IF声明,执行就会停止。有人可以帮我找出我错在哪里吗?

P.S。我没有使用任何过程/函数 - 我不能存储这些语句,因为我必须自定义select语句,因为DB每次都可以更改。

0 个答案:

没有答案