MySQL 5.0 Stored Procedures : Part 2
In theb last blog entry I looked at how to create a very simple stored procedure. While it demonstrated how quick and easy it was to create a stored procedure it wasn't particuarly usefull.
So today I'll be showing you two additional things (spread over two entries), you can do to expand on yesterdays example. First up it's creating a multiline procedure. On first consideration this might not seem to be something you need to deal with, why can't you just keep adding lines to the procedure. The problem with that is that MySQL needs to firstly know where the procedure code starts and ends (in our first example it wasn't a problem because it was a single line of code) and also it needs to know when a particular command ends.
Take for example the MySQL command line tool, when you enter an SQL statement you need to add a semicolon ; to the end of the SQL command to tell MySQL that there is no more to enter and to begin processing the command. This is the same for stored procedures, you need to tell MySQL where the individual commands in the procedure end. However MySQL uses the same line delimiter for stored procedures as it does for normal command line operations, this means that when entering multiline procedures we need to change the default delimiter character. Doing this is trivial but it does mean you have to get yourself into "different delimiter mode" in that if your anything like me you will be constantly wondering why MySQL hasn't processed that select.
mysql> delimiter //
Personally I use the // as the new delimiter, your free to choose one you prefer but // seems to work well and not clash with MySQL. So how does this effect MySQL, where we would have used ; we now need to use the new delimiter.
mysql> select 'Test of Delimiter'//
+-------------------+
| Test of Delimiter |
+-------------------+
| Test of Delimiter |
+-------------------+
1 row in set (0.00 sec)
So now the question is how does this relate to stored procedures. Let's create a similar procedure to our last, this time rather than using a literal in the select we can first set a user variable and then select that. The second thing we need to do when creating multiline stored procedures is enclose the body of the procedure (the actual code but not the head) within a begin and an end block. So it looks like this...
mysql> create procedure testProc()
-> begin
-> set @a = 'HelloWorld';
-> select @a;
-> end;
-> //
Query OK, 0 rows affected (0.28 sec)
You can see that the two lines are enclosed within the begin and end and that each one is terminated with the ; delimiter. Because we have changed the MySQL command line default delimiter to get MySQL to process the create procedure statement we need to enter the new delimiter after the procedure statement.
So to run the procedure we just call it as we did with our first example.
mysql> call testProc()//
+------------+
| @a |
+------------+
| HelloWorld |
+------------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.01 sec)
Great we have created a multiline procedure, but it still doesn't do a great deal.


12 Comments:
CALIFORNIA WEB HOSTING
Can I Help You.. Yes If you want to get on the web. If you want to start learning about creating a web site of your own.. go to http://webhost11.com. to find out more about dedicated server webhosting ... We can help find a web host for you.
You are good but not too good bcoz this is not enough information for beginer of Store Procedure
Neo
hi,
Hey your blog is really nice. It provides a lot of imformation. Anyway for free directory submission service feel free to contact topseofirms
Thanx,
topseofirms
Nice Blog. I will continue reading........................
Tip For All Bloggers:
If I can sum up in three words how anyone or any website can totally "DOMINATE" the search engines and get to the very "TOP" of the search engines is: blogs, links, and content, period. That's all it takes. But you need "LOTS" of blogs, links and content.......................MajorEnterprise, Carael Knight
For more information, visit:
The Internet Marketing Genius, Carael KnightThe Most Powerful Internet Marketing Resources On The Web!
I do not know about others but I am grate-ful for sites such as this. I will make it a point to return in the near future. Protect-your-computer-from-spyware
Orange County SEO Companies fall short of, Atomic SEO, with their world class staff, the firm seems to keep a client priveledge like no other seo firm.
Hand Crafted Dog Art Hi, Love dogs? Spread the word to your friends, this is a great site.
Domin8 MySpace with myspace friend adder. Check out www.domin8myspace.com
Domin8 MySpace with unique myspace music.
really it is too good blog for get more information........
can there be two begin in a procedure.Please let me know
black mold exposureblack mold symptoms of exposurewrought iron garden gatesiron garden gates find them herefine thin hair hairstylessearch hair styles for fine thin hairnight vision binocularsbuy night vision binocularslipitor reactionslipitor allergic reactionsluxury beach resort in the philippines
afordable beach resorts in the philippineshomeopathy for eczema.baby eczema.save big with great mineral makeup bargainsmineral makeup wholesalersprodam iphone Apple prodam iphone prahacect iphone manualmanual for P 168 iphonefero 52 binocularsnight vision Fero 52 binocularsThe best night vision binoculars here
night vision binoculars bargainsfree photo albums computer programsfree software to make photo albumsfree tax formsprintable tax forms for free craftmatic air bedcraftmatic air bed adjustable info hereboyd air bedboyd night air bed lowest pricefind air beds in wisconsinbest air beds in wisconsincloud air beds
best cloud inflatable air bedssealy air beds portableportables air bedsrv luggage racksaluminum made rv luggage racksair bed raisedbest form raised air bedsaircraft support equipmentsbest support equipments for aircraftsbed air informercialsbest informercials bed airmattress sized air beds
bestair bed mattress antique doorknobsantique doorknob identification tipsdvd player troubleshootingtroubleshooting with the dvd playerflat panel television lcd vs plasmaflat panel lcd television versus plasma pic the bestThe causes of economic recessionwhat are the causes of economic recessionadjustable bed air foam The best bed air foam
hoof prints antique equestrian printsantique hoof prints equestrian printsBuy air bedadjustablebuy the best adjustable air bedsair beds canadian storesCanadian stores for air beds
migraine causemigraine treatments floridaflorida headache clinicdrying dessicantair drying dessicantdessicant air dryerpediatric asthmaasthma specialistasthma children specialistcarpet cleaning dallas txcarpet cleaners dallascarpet cleaning dallas
Post a Comment
<< Home