如果表不存在执行一个长查询 [英] if table does not exist execute a long query

查看:376
本文介绍了如果表不存在执行一个长查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MySQL 5.0+,如果表不存在,我试图执行一个大的命令列表。所以我想有:

I am using MySQL 5.0+ and I am trying to execute a big list of commands if a table does not exist. So I would like to have:

if not exist table
then
10000 line query that creates and populates the table with a lot of entries.
end if

唯一的问题是我一直在搜索,

The only problem is that I have been searching and so far I found out that MySQL does not support such a feature.

目前我有:

IF NOT EXISTS `profiles`
THEN
    A LOT OF QUERIES;
END IF;

由于某种原因,它继续给我错误,说第1行语法错误。

For some reason it keeps on giving me error saying syntax is wrong on line 1.

所以我想知道是否有人会有一个更好的想法,如何去处理这个问题,或如何解决它。

So I was wondering if anyone would happen to have a better idea as to how go about approaching this problem, or how to fix it.

推荐答案

添加到bfavaretto的代码,如果你有information_schema.tables,尝试这样:

Addding on to code from bfavaretto, if you do have information_schema.tables, try something like this:

IF NOT EXISTS (SELECT * FROM information_schema.tables
WHERE table_schema = 'databasename'
AND table_name = 'tablename')
do your big long create table stuff

这篇关于如果表不存在执行一个长查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆