简体   繁体   中英

ORACLE SQL- Insert new variable to table

I have a stored proc I am trying to update a table with and am having trouble with how I would set a variable to different values dependent on the result of other column queries and joins.

Example:

INSERT INTO CUSTOMERS(
CUST_ID,
CUST_signup_info)

SELECT CUST_ID,
CUST_signup_info

FROM (SELECT DISTINCT(c.CUST_ID, CUST_signup_info) FROM CUSTOMERS c 
inner join CUSTOMER_INFO ci on
ci.CUST_ID = c.CUST_ID)

What I need help with is say there are three possible values for CUST_signup_info (walk-in, web, phone) and i need the return value to update to the CUSTOMERS table as 1, 2, 3 respectively. Would I need to create a variable to do this somehow?

I need something like

if c.CUST_ID = 'walk-in' THEN c.CUST_ID= 1
else if c.CUST_ID = 'web' THEN c.CUST_ID= 2
else if c.CUST_ID = 'phone' THEN c.CUST_ID= 3

And how/ where would this logic be implemented into the INSERT statement

Not sure what exactly you are trying to acheive here. Is the CUST_ID meant to be the primary key for the CUSTOMERS table? If so then why do you need to update this? Have it auto populate from a sequence. Also, your last piece of code (the if then logic) is incorrect since you are using CUST_ID on both sides.

To me, it looks like you need a lookup table for signup type, and then have a column in your CUSTOMER_INFO table that links backs to that table using the ID. So something like this:

CUSTOMER_SIGNUPS table
  ID             TYPE
1             Walk In
2             Web
3             Phone

CUSTOMER_INFO table
  TYPE_ID -> this links back to the CUSTOMERS_SIGNUP table.

Perhaps I am missing exactly what you are trying to achieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM