tags:

views:

35

answers:

2

I have some experience with SQL Server 2005 and have written many scripts that just have a BEGIN statement and an END. I could use logic such as loops, cursors, etc. However I can not seem to be able to do this in MySQL without creating a stored procedure. I know that stored procs in MySQL are new and might not be as robust as that of SQL Server but is there anyway to just create a generic script that I know I only want to run one time so I wont have to go back into my Database and remove all these stored procs later?

to clarify a little bit what I am trying to do is create a script that will create multiple talbes, for example

BEGIN 
   CREATE TABLE foo(id INT, data VARCHAR(100)); 
   CREATE TABLE test(t_id INT, data VARCHAR(50)); 
END;

Is this possible in MySQL?

+1  A: 

You don't need the begin and end keywords in MySQL, the following will work:

CREATE TABLE foo(id INT, data VARCHAR(100));
CREATE TABLE test(t_id INT, data VARCHAR(50));

You can also use an if not exists clause to ensure that you don't get errors if the table(s) already exist:

CREATE TABLE IF NOT EXISTS foo(id INT, data VARCHAR(100));
CREATE TABLE IF NOT EXISTS test(t_id INT, data VARCHAR(50));
Ian Kemp
I tried that already and for some reason it will only create the first table. I am using the Query Browser in MySQL Admin to execute the queries. Is this my problem?
A: 

Sorry, I figured it out. I was trying to run the script in the generic sql creator at the top instead of going to File->New Script tab and trying to run the script in there. Thanks for your help

Heh, I had the same problem when I started using Query Browser, it's somewhat unintuitive - and the fact that it won't display resultsets from script execution sucks. :( MySQL are working on a replacement called Workbench (http://wb.mysql.com/) which is like MSSQL's Management Studio. It's very beta but I've been using it for a while now and find it much more useful than Query Browser, so consider giving it a try.
Ian Kemp