mysql setschema_How do I set the default schema for a user in MySQL

I have a script file for MySQL that needs to be run on about 30 databases. Here‘s a snippet:

ALTERTABLE.`tblusers`ADDCOLUMN`availability`...ALTERTABLE.`tblusers`ADDCOLUMN`reliability`INTEGER ...

There are many more lines in this script and I‘d like to automate it in a loop of current databases to be updated. I‘ve got the list and the loop down using cursors, but here‘s where I‘m running into trouble:

When I‘m on a particular database in the cursor, that database name is in a variable. I can‘t just say ALTER TABLE curschema.tblusers because the script complains that there is no database named curschema (the name of the variable containing the database name that I‘d like to run operations on). I‘ve been able to work around this by creating and executing a statement using parameters:

SET@curschema =curschema;SET@query =NULL;SET@email =emailAddress;SET@pass =pass;SET@statement =CONCAT(‘SELECT userid INTO @query FROM ‘,@curschema,‘.tbluser

PREPARE stmt FROM@statement;EXECUTEstmt;

The problem is, setting up executable strings (like above) will become an extremely tedious task with the dozens of lines of code that I have to run. I was hoping that there was a way that I could set the current database for operations and then just reset that database each loop pass so my generic statements might be run:

(start of my loop)

SET DATABASE database0 -- (through to database29)

-- run statements with database0 .... 29 being implied with the above command

ALTERTABLE`tblusers`ADDCOLUMN`availability`TINYINT(1)UNSIGNED ...ALTERTABLE`tblusers`ADDCOLUMN`reliability`INTEGER UNSIGNED NOT...

(end of my loop)

Does such a command exist to set the current database on which operations may be performed? If no such command exists, is there an easier way to accomplish my task? I figure that at this juncture it‘s just easier to do a find/replace on all the database names 30 times than it is to rewrite my entire script file to be executable/parameter-ized strings.

Thanks!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值