jump to navigation

Using views with variables in MySQL June 18, 2007

Posted by Olexandr Melnyk in : Databases , trackback

One of MySQL’s restriction when using views is that they cannot refer to local variables. So, for example, this statement will produce an error:

create view thematical_books as
select title
     , author
  from books
 where subject = @book_subject


A workaround for this limitation is quite simple: to use a function which returns variable value. For example:

create function book_subject
returns varchar(64) as
return @book_subject;

create view thematical_books as
select title
     , author
  from books
 where subject = book_subject()

Technorati Tags: , , , ,

Comments»

no comments yet - be the first?