I am working on a rather large SQL script to be used with Oracle, but I'm running into an issue. First, let me outline how the script operates.
- Declare variables
- `CUSTOMERID NUMBER;`
- `SERVERID NUMBER;`
- Create a customer if it doesn't exist
- `SELECT ID INTO CUSTOMERID FROM CUSTOMER WHERE NAME = 'The Customer I just Inserted';`
- Create the server if it doesn't exist, using the `CUSTOMERID` value to relate the server to the customer.
- `SELECT ID INTO SERVERID WHERE HOSTNAME = 'the.server.i.just.created';`
- For each service belonging to this server, insert the service using the `SERVERID` value to relate the service to the server.
- Go to 2
Now this process seems to work well for just one customer with 15 servers, each having 6 services. But as soon as the next customer is introduced, I receive prompts for variable substitution. The way I'm using the variables on my insert is pretty straightforward:
INSERT INTO SERVERS(CUSTOMER_ID, HOSTNAME)
SELECT CUSTOMERID, 'the.server.i.just.created' FROM DUAL
WHERE NOT EXISTS (
SELECT *
FROM SERVERS
WHERE HOSTNAME = 'the.server.i.just.created'
);
I have also attempted using the DECLARE ... BEGIN ... END;
method, but I receive the same general results. Some examples I've seen suggest to use the :CUSTOMERID
style variables, but those don't seem to work at all, where they are ending up with null values, which shouldn't be happening given the previous queries.
What I am needing help with is in understanding how to achieve this. I have very limited access to the production environment, so anything I do needs to be kept basic (e.g., no new functions, types, or procedures).