Connecting Tech Pros Worldwide Help | Site Map

User defined variables question

  #1  
Old July 11th, 2007, 01:25 AM
bissatch@yahoo.co.uk
Guest
 
Posts: n/a
Hi,

Im trying to write an SQL file which contains SQL queries which are to
be run individually one after the other. Im using user defined
variables so that I can save time on having to manually insert
constant values.

Hope this makes sense:

SET @sequence := SELECT COUNT(*) FROM tracks WHERE cd_id = 12;
INSERT INTO tracks (song, artist, sequence, cd_id) SET ('Fools Gold',
'Stone Roses', @sequence, 12);

Basically here Im trying to SET the value of $sequence based on the
result of the select statement. So when I first run the two queries
@sequence will have a zero value, the next time it will count the
tracks table and will now return a value of 1, then 2, 3,4 etc. Each
time I will replace the values of the INSERT query but the SET SELECT
query will remain. The SELECT statement runs fine on its own but
obviously Im either trying to do something that is not possible with
the user-defined variables or more likely Ive got the query wrong.
Anyway, could someone please point me in the correct direction, much
appreciated. Thanks

Burnsy

  #2  
Old July 11th, 2007, 02:45 AM
bissatch@yahoo.co.uk
Guest
 
Posts: n/a

re: User defined variables question


Its cool, figured it out:

SELECT @sequence := COUNT(track_id) FROM tracks WHERE cd_id = 12;

or for whole values:

SELECT @sequence := COUNT(track_id)+1 FROM tracks WHERE cd_id = 12;

Cheers

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Abstract class variables question tshad answers 20 November 20th, 2007 10:35 AM
User-defined function chreo answers 3 November 21st, 2005 02:16 PM
User defined data type used in stored procedure parameters dawatson833@hotmail.com answers 13 July 23rd, 2005 10:31 AM
User-defined Globals? Jon Grieve answers 7 July 17th, 2005 07:38 AM