in mysql, if creating a view WITH CHECK OPTION, is it possible to use a user variable in the clause?
For example, if user variable @usr_id
is consistently set to the current user, would like to have a view that would limit user access and dml (via updatable views) to the current user.
However, the statement
create view trans_usr as select * from trans where
usr_id=@usr_id with check option;
yields
ERROR 1351 (HY000): View's SELECT contains a variable or parameter
any good way to get around this? would rather not have to redefine views for each web session.
as per @Damien_The_Unbeliever, wrapped the user variable inside a function, and works ok
create function cur_usr() returns integer not deterministic return @usr_id;
create view tr开发者_StackOverflow社区ans_usr as select * from trans where usr_id =
cur_usr() with check option;
works, but perhaps not expected:
insert into trans_usr( description, amount ) values( 'something', 150 );
does not automatically sets usr_id
to the @usr_id
user, but
select * from trans_usr where amount > 100;
only pulls from the user as set in @usr_id
, but
update trans_usr set usr_id = 555;
actually changes the usr_id
, allowing us to break the ON CHECK option - this took me by surprise!
精彩评论