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 > Data Access, Manipulation & Batch Languages > ANSI SQL > store a query from a select statement into a variable

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-03-03, 11:33
let99 let99 is offline
Registered User
 
Join Date: Jun 2003
Posts: 5
store a query from a select statement into a variable

hi all

PROCEDURES:

i want to store a select statement into a variable which i want to call later.. how can this be done ???


Varoable1 = select email from person where ....;

insert into movie (email, imdbnr, moviename, duration, genre)
values (Variable1, nimdbnr, nmoviename, nduration, ngenre);

this doesnt work ... anyone has an idea ???

thx
Reply With Quote
  #2 (permalink)  
Old 06-03-03, 12:06
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Re: store a query from a select statement into a variable

Quote:
Originally posted by let99
hi all

PROCEDURES:

i want to store a select statement into a variable which i want to call later.. how can this be done ???


Varoable1 = select email from person where ....;

insert into movie (email, imdbnr, moviename, duration, genre)
values (Variable1, nimdbnr, nmoviename, nduration, ngenre);

this doesnt work ... anyone has an idea ???

thx

What DBMS? If it was Oracle the answer would be:

DECLARE
v_email person.email%TYPE;
BEGIN
select email INTO v_email FROM person where ...;
...
insert into movie (email, imdbnr, moviename, duration, genre)
values (v_email, nimdbnr, nmoviename, nduration, ngenre);
END;

Mind you, what are nimdbnr etc.? They look like variables too!
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #3 (permalink)  
Old 06-03-03, 12:13
dbmadcap dbmadcap is offline
Registered User
 
Join Date: May 2003
Posts: 87
Re: store a query from a select statement into a variable

declare
lvar1 varchar2(50);
begin
select email into lvar1 from person where <condition>;
insert into movie (email, imdbnr, moviename, duration, genre)
values (lvar1, nimdbnr, nmoviename, nduration, ngenre);
end;
/

Do not forget to handle exceptions like no_data_found or too_many_rows.

Quote:
Originally posted by let99
hi all

PROCEDURES:

i want to store a select statement into a variable which i want to call later.. how can this be done ???


Varoable1 = select email from person where ....;

insert into movie (email, imdbnr, moviename, duration, genre)
values (Variable1, nimdbnr, nmoviename, nduration, ngenre);

this doesnt work ... anyone has an idea ???

thx
Reply With Quote
  #4 (permalink)  
Old 06-03-03, 12:50
let99 let99 is offline
Registered User
 
Join Date: Jun 2003
Posts: 5
thank you thank you !!
it was like this:

PROCEDURE add (attr1, attr2, .....);
v_email person.email%TYPE;
BEGIN
select person.email INTO v_email from person,member ....
insert into movie (email, imdbnr, moviename, duration, genre)
values (v_email, nimdbnr, nmoviename, nduration, ngenre);

P.S. it was oracle & plsql
Reply With Quote
  #5 (permalink)  
Old 06-03-03, 12:53
dbmadcap dbmadcap is offline
Registered User
 
Join Date: May 2003
Posts: 87
Again, do not forget to handle the exception just in case your select statement fails i.e. returns no rows or more than 1 row.


Quote:
Originally posted by let99
thank you thank you !!
it was like this:

PROCEDURE add (attr1, attr2, .....);
v_email person.email%TYPE;
BEGIN
select person.email INTO v_email from person,member ....
insert into movie (email, imdbnr, moviename, duration, genre)
values (v_email, nimdbnr, nmoviename, nduration, ngenre);

P.S. it was oracle & plsql
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