(Syntax error with emulating "create user if not exists"에서 관련,하지만 별도.)일반적으로/동적으로 생성하는 방법/드롭 사용자
일반적으로/동적 (즉,이 sp_adduser
시스템 프로 시저가 다른 포함 모방 사용자를 추가하는 기능을 수행하는 것이 가능 DBMS)
MySQL은 다음 if [not] exists
구문을 지원하지 않습니다 http://bugs.mysql.com/bug.php?id=15287 참조 :
create user if not exists 'foo'@'%' identified by password 'bar';
또한이 기능을 지원하지 않습니다
drop procedure if exists create_user_if_not_exists;
delimiter ||
create procedure create_user_if_not_exists
(sUser varchar(60),
sHost varchar(16),
sPassword varchar(255))
begin
-- ensure user does not yet exist
if (select ifnull((select 1
from mysql.user
where User = sUser
and Host = sHost), 0) = 0) then
set @createUserText = concat('create user ''', sUser, '''@''', sHost, ''' identified by ''', sPassword, ''';');
prepare createUserStatement FROM @createUserText;
execute createUserStatement;
deallocate prepare createUserStatement;
end if;
end ||
delimiter ;
당신이 말한 프로 시저 호출하려고하면 이유는
call create_user_if_not_exists ('foo', '%', 'bar');
멋진 메시지가 표시됩니다.
This command is not supported in the prepared statement protocol yet
다음 작품,하지만 분명히 특히 재사용되지 않습니다 :
drop procedure if exists create_user_if_not_exists;
delimiter ||
create procedure create_user_if_not_exists
()
begin
if (select ifnull((select 1
from mysql.user
where User = 'foo'
and Host = '%'), 0) = 0) then
create user 'foo'@'%' identified by password 'bar';
end if;
end ||
delimiter ;
그래, 내가 그 일을 끝냈다. 특히 깨끗하게 느껴지지 않습니다 ... –