1

I got the following script that does actually the following, it uses an email-address to

  • search for that user
  • search for messages from that user
  • search for Entries of that user
  • puts the findings to a file

currently that is working but now I would like to do the following: Replace the sub select "SELECT ID FROM DBSCHEMA.USERS WHERE EMAIL ='&SEARCH'" with a kind of variable to not put (and execute) this statement multiple times in the script.

But what ever I tried did not work. (Started using DECLARE block - than the spool was not working, end everything else just broke the script)
I'm using a oracle database - but i would like to stick to generic commands if possible.

DEFINE SEARCH='search@example.org';

spool OUTPUTFILE.out
prompt Start Searching by mail '&SEARCH'

SELECT
    'ID;NAME;LAST_LOGIN;EMAIL_ADDRESS;FIRST_NAME;LAST_NAME;BLABLA;LANGUAGE_ID;'
FROM
    DUAL;

SELECT
    ID||';'||NAME||';'||LAST_LOGIN||';'||EMAIL||';'||FIRST_NAME||';'||LAST_NAME||';'||BLABLA||';'||LANGUAGE_ID||';'
FROM
    DBSCHEMA.USERS
WHERE
    EMAIL ='&SEARCH';

prompt Feedback messages
prompt ID;SUBJECT;MESSAGE;TIMESTAMP;

SELECT
    ID||';'||SUBJECT||';'||MESSAGE||';'||TIMESTAMP||';'
FROM
    DBSCHEMA.SITE_FEEDBACK
WHERE
    USER_ID = (SELECT ID FROM DBSCHEMA.USERS WHERE EMAIL ='&SEARCH');

prompt Other Entries
prompt ID;TITLE;BLABLA;TIMESTAMP;

SELECT
    ID||';'||TITLE||';'||BLABLA||';'||TIMESTAMP||';'
FROM
    DBSCHEMA.SITE_ENTRY
WHERE
    USER_ID = (SELECT ID FROM DBSCHEMA.USERS WHERE EMAIL ='&SEARCH');

spool off;
wolφi
  • 8,091
  • 2
  • 35
  • 64
m.weiloa
  • 155
  • 1
  • 11
  • To begin with, start by using a declared variable to hold the e-mail address, something like `DECLARE Searched_Email NVARCHAR2(256) ;` and `...WHERE EMAIL = Searched_Email ;`. If this works, you script is OK and the problem is your prompt management which, anyway, is not part of standard (and real-systems usage) SQL language. – FDavidov Jun 03 '18 at 12:57
  • @FDavidov: WIth the simple define it is working - with declare not - even without the promts. – m.weiloa Jun 03 '18 at 23:01
  • Please post both versions: the one that works and the one that does not. – FDavidov Jun 04 '18 at 05:40

1 Answers1

1

How does search get populated? Also, what version of SQL*Plus do you have?

In SQL*Plus you could declare a variable and populate it in PL/SQL, like this (I've used the HR demo schema):

define search='VJONES';

var user_id number

begin
    select employee_id into :user_id from employees where email = '&search';
exception
    when no_data_found then
        raise_application_error(-20001, 'No user found with email ''&search''.');
    when too_many_rows then
        raise_application_error(-20002, 'Multiple users found with email ''&search''.');
end;
/

select phone_number from employees where employee_id = :user_id;

PHONE_NUMBER
--------------------
650.501.4876

(You don't strictly need the exception handler, but it will improve handling of invalid or duplicated data.)

You could also stick with define variables:

column user_id new_value user_id
select employee_id as user_id from employees where email = '&search';

   USER_ID
----------
       195

1 row selected.

Now you have defined and populated &user_id and you can use it in queries:

select phone_number from employees where employee_id = &user_id;

PHONE_NUMBER
--------------------
650.501.4876

I don't understand the parts of your question about things not working or using generic commands. If you could clarify those I might be able to add to my answer.

William Robertson
  • 15,273
  • 4
  • 38
  • 44
  • The second one is finally working somehow. First one is not working (not in SQLDeveloper, not as a file run with sqlplus, not typed into sqlplus) - SP2-0552: Bind variable "user_id" not declared. Second one runs from a file (not in SQLDeveloper). But maybe you can explain the "column user_id new_value user_id" notaition a bit or give me a hin to more documentaion on that (I found a lot of the other not working for me) – m.weiloa Jun 03 '18 at 23:08
  • Sorry, missed a line in copy & paste. I've added `var user_id number` to my answer. The `new_value` thing is documented under [`COLUMN`](https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqpug/COLUMN.html) but it's not obvious what it's for (they expect you to use it for page headings in reports, hence all the stuff about `TTITLE` and `SKIP PAGE`). After you set `column x new_value y` then substitution variable `&y` will get populated with the last value returned by a query for any column named `x`. – William Robertson Jun 04 '18 at 00:09