Created
April 8, 2014 01:08
-
-
Save dha-lo-jd/10079648 to your computer and use it in GitHub Desktop.
マイグレーションツール使っといてカラム指定しながらビューを作るというのがあまりにもだるいので書いた
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SET @schema = '<schema>'; | |
| SET @table_name = '<table>'; | |
| SET @exclude_columns = CONCAT_WS('\',\'','id','hoge'); | |
| SET @exclude_columns = CONCAT('\'',@exclude_columns,'\''); | |
| SET @sql = CONCAT('SELECT GROUP_CONCAT(COLUMN_NAME) into @out FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ? AND COLUMN_NAME not in (',@exclude_columns,')'); | |
| PREPARE stmt1 FROM @sql; | |
| EXECUTE stmt1 using @table_name, @schema; | |
| SET @columns = @out; | |
| SET @view_name = '<view_name>'; | |
| SET @sql = CONCAT('SELECT ',@columns,' FROM ',@table_name); | |
| SET @view_sql = CONCAT('CREATE ALGORITHM=MERGE DEFINER=<difiner> SQL SECURITY DEFINER VIEW ',@view_name,' AS ',@sql); | |
| PREPARE stmt2 FROM @view_sql; | |
| EXECUTE stmt2; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment