Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > MySQL > using variables in mysql

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-07-07, 12:35
neuroticbiscuit neuroticbiscuit is offline
Registered User
 
Join Date: Oct 2007
Posts: 1
using variables in mysql

How do I maintain the value of a variable beyond the first line of a procedure?

I want to use:

CREATE PROCEDURE IntSetUp (a_var VARCHAR(100))
insert into db1.table1
(valueA, valueB)
SELECT a.value, b.value
FROM db2.table1 a inner join db2.table2 b on a.id=b.id
where a.value=a_var;
INSERT into db1.table2
(valueA, valueB)
SELECT a.valueA, a.valueB
FROM db2.table1 a where a.value=a_var;

but the variable loses is value after the first INSERT line, so the 2nd insert never gets done.

I've tried replacing the top line with:

CREATE PROCEDURE IntSetUp (a_var VARCHAR(100))
SET @x := a_var;
and then
SET @x := a_var;

and I got the same problem, the values just disapper after the first line.

I then tried enclosing the whole thing in BEGIN and END tags and putting:
DECLARE @x = a_var
as the first line (also tried DECLARE x = a_var)

but this just throws up an error.

What am I doing wrong?
Reply With Quote
  #2 (permalink)  
Old 10-08-07, 10:36
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 734
You haven't created the stored procedure correctly. The first time you have done it the first SQL line stops at ; because you have failed to specify a delimiter.

In your second attempt you are indeed correct in specifying a BEGIN/END clause as this is what you should be doing.

However what you are mostly likely to want is as follows :
Code:
delimiter // CREATE PROCEDURE IntSetUp (IN a_var VARCHAR(100)) BEGIN SELECT a.value, b.value FROM db2.table1 a inner join db2.table2 b on a.id=b.id where a.value=a_var; INSERT into db1.table2 (valueA, valueB) SELECT a.valueA, a.valueB FROM db2.table1 a where a.value=a_var; END; // delimiter ;

All of the above wants to be specified at cmd line.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On