如何创建名称中包含用户变量的 mysql 视图
我正在使用 mysql 5.5,尝试编写一个脚本来创建用户、视图并授予对这些视图的选择权限。这是我到目前为止所拥有的。
set @type_id := 1;
set @username := 'somecompany';
set @password := 'company1234';
set @prefix := 'somecompany';
CREATE OR REPLACE VIEW CONCAT(@prefix, '_report') AS
SELECT * FROM my_table
WHERE type_id = @type_id;
这是行不通的,因为它不寻找视图名称的字符串。我通过以下语句解决了创建用户的问题:
INSERT INTO mysql.user (Host, User, Password) VALUES ('%', @username, PASSWORD(@password));
是否有类似的技巧可以用来创建视图并将这些视图的选择权授予我创建的用户?
I'm using mysql 5.5, trying to write a script to create users, views, and grant select privileges on those views. Here's what I have so far.
set @type_id := 1;
set @username := 'somecompany';
set @password := 'company1234';
set @prefix := 'somecompany';
CREATE OR REPLACE VIEW CONCAT(@prefix, '_report') AS
SELECT * FROM my_table
WHERE type_id = @type_id;
Which won't work because it isn't looking for a string for the view name. I got around this for creating users with the statement:
INSERT INTO mysql.user (Host, User, Password) VALUES ('%', @username, PASSWORD(@password));
Is there a similar trick I can use to create views and grant select on those views to the user I created?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用准备语句来执行这个查询。只需将查询构造为字符串并使用准备好的语句运行即可。
编辑
MySQL 5.5.12-log
You can use Prepared Statements to execute this queries. Just construct the query as a string and run it with prepared statements.
Edit
MySQL 5.5.12-log