Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INSERT ALL INTO and Sequence.nextval for a Surrogate Key

Tags:

sql

oracle

plsql

I'm trying to insert 40 rows using an INSERT ALL INTO and I'm not certain on how to insert the surrogate key. Here's what I have

BEGIN
   INSERT ALL 

   INTO  question(question_id)
   VALUES (question_seq.nextval)
END

Now if I add another INTO VALUES then I get a unique constraint violation.

BEGIN
   INSERT ALL 

   INTO  question(question_id)
   VALUES (question_seq.nextval)

   INTO  question(question_id)
   VALUES (question_seq.nextval)
END

How can I update the sequences nextval value for each INTO VALUES so that I can avoid the unique constraint violation? I assumed that nextval would automatically update itself.

UPDATE: I don't know if this is the best way to handle this but here's the solution I came up with:

first I created a function that returns a value then I called that function in the id field of the VALUES clause

create or replace
FUNCTION GET_QUESTION_ID RETURN NUMBER AS 
num NUMBER;
BEGIN
  SELECT UHCL_QUESTIONS_SEQ.nextval 
  INTO num 
  FROM dual;  
  return num;  
END GET_QUESTION_ID;

INSERT ALL
INTO question(question_id)
VALUES (GET_QUESTION_ID())
INTO question(question_id)
VALUES (GET_QUESTION_ID())
like image 997
Robert Avatar asked Dec 03 '11 19:12

Robert


People also ask

How do you use Nextval in insert statement?

NEXTVAL); In the previous example, the database server inserts an incremented value (or the first value of the sequence, which is 1 ) into the col1 and col2 columns of the table. You can use NEXTVAL (or CURRVAL) in the SET clause of the UPDATE statement, as the following example shows: UPDATE tab1 SET col2 = seq_2.

How do you insert data into a sequence in SQL?

Syntax: CREATE SEQUENCE sequence_name START WITH initial_value INCREMENT BY increment_value MINVALUE minimum value MAXVALUE maximum value CYCLE|NOCYCLE ; sequence_name: Name of the sequence. initial_value: starting value from where the sequence starts.

What is sequence Nextval?

SEQUENCE & NEXTVAL statements are used to insert values automatically PRIMAR and UNIQUE columns while adding new rows on table. SEQUENCE statement is used to generate UNIQUE values on particular column in existing table with starting value and increment by value.

What is a surrogate key on a table?

A surrogate key on a table is a column with a unique identifier for each row. The key is not generated from the table data. Data modelers like to create surrogate keys on their tables when they design data warehouse models.

Can I create surrogate keys in a dedicated SQL pool?

In this article, you'll find recommendations and examples for using the IDENTITY property to create surrogate keys on tables in dedicated SQL pool. A surrogate key on a table is a column with a unique identifier for each row.

How do I Use Surrogate keys in a star schema?

Use the surrogate key transformation to add an incrementing key value to each row of data. This is useful when designing dimension tables in a star schema analytical data model. In a star schema, each member in your dimension tables requires a unique key that is a non-business key. Key column: The name of the generated surrogate key column.

Which row has the surrogate value of 1 in column C1?

The first row has the surrogate value of 1 in column C1, and the second row has the surrogate value of 61. Both of these values were generated by the IDENTITY property.


1 Answers

Being from a SQL Server background, I've always created a trigger on the table to basically emulate IDENTITY functionality. Once the trigger is on, the SK is automatically generated from the sequence just like identity and you don't have to worry about it.

like image 76
Nick.McDermaid Avatar answered Sep 23 '22 09:09

Nick.McDermaid