ORA-00913 Too Many Values - Resolution Guide
ORA-00913: Too Many Values
Section titled “ORA-00913: Too Many Values”Error Overview
Section titled “Error Overview”Error Text: ORA-00913: too many values
This error occurs when an SQL statement provides more values than expected. It’s most commonly seen in INSERT statements where the number of values in the VALUES clause exceeds the number of columns, or in subqueries where the SELECT list has more columns than the outer query expects.
Common Causes
Section titled “Common Causes”1. INSERT with Mismatched Column Count
Section titled “1. INSERT with Mismatched Column Count”- More values in VALUES clause than columns specified
- Column list doesn’t match the values provided
- Copy-paste errors adding extra values
2. Subquery Returns Too Many Columns
Section titled “2. Subquery Returns Too Many Columns”- WHERE clause subquery returns multiple columns
- SET clause subquery returns more than one column
- Comparison operators used with multi-column subquery
3. UNION/UNION ALL Column Mismatch
Section titled “3. UNION/UNION ALL Column Mismatch”- Different number of columns in UNION branches
- Added a column to one SELECT but not all others
4. INSERT…SELECT Mismatch
Section titled “4. INSERT…SELECT Mismatch”- SELECT returns more columns than the target INSERT column list
Diagnostic Examples
Section titled “Diagnostic Examples”Example 1: INSERT Column Mismatch
Section titled “Example 1: INSERT Column Mismatch”-- This table has 3 columnsCREATE TABLE employees (id NUMBER, name VARCHAR2(100), dept VARCHAR2(50));
-- ERROR: 4 values for 3 columnsINSERT INTO employees VALUES (1, 'John Smith', 'IT', 'Active');-- ORA-00913: too many values
-- FIX: Match values to columnsINSERT INTO employees VALUES (1, 'John Smith', 'IT');
-- BETTER: Always specify column list explicitlyINSERT INTO employees (id, name, dept) VALUES (1, 'John Smith', 'IT');Example 2: Subquery with Too Many Columns
Section titled “Example 2: Subquery with Too Many Columns”-- ERROR: Subquery returns 2 columns, comparison expects 1SELECT * FROM ordersWHERE customer_id = (SELECT customer_id, customer_name FROM customers WHERE email = '[email protected]');-- ORA-00913: too many values
-- FIX: Return only the needed columnSELECT * FROM ordersExample 3: UNION Column Count Mismatch
Section titled “Example 3: UNION Column Count Mismatch”-- ERROR: First SELECT has 3 columns, second has 4SELECT id, name, salary FROM employeesUNION ALLSELECT id, name, salary, department FROM contractors;-- ORA-00913: too many values
-- FIX: Match column countsSELECT id, name, salary FROM employeesUNION ALLSELECT id, name, salary FROM contractors;Example 4: INSERT…SELECT Mismatch
Section titled “Example 4: INSERT…SELECT Mismatch”-- ERROR: Selecting 4 columns into 3-column insertINSERT INTO emp_archive (id, name, dept)SELECT id, name, dept, hire_date FROM employees WHERE status = 'INACTIVE';-- ORA-00913: too many values
-- FIX: Match SELECT columns to INSERT columnsINSERT INTO emp_archive (id, name, dept)SELECT id, name, dept FROM employees WHERE status = 'INACTIVE';Diagnostic Queries
Section titled “Diagnostic Queries”Check Table Column Count
Section titled “Check Table Column Count”-- Count columns in the target tableSELECT table_name, COUNT(*) as column_countFROM dba_tab_columnsWHERE owner = 'YOUR_SCHEMA' AND table_name = 'YOUR_TABLE'GROUP BY table_name;
-- List all columns with orderSELECT column_name, column_id, data_type, nullableFROM dba_tab_columnsWHERE owner = 'YOUR_SCHEMA' AND table_name = 'YOUR_TABLE'ORDER BY column_id;Check for Virtual or Hidden Columns
Section titled “Check for Virtual or Hidden Columns”-- Virtual columns count toward column total but shouldn't be in INSERTSELECT column_name, data_type, virtual_column, hidden_columnFROM dba_tab_colsWHERE owner = 'YOUR_SCHEMA' AND table_name = 'YOUR_TABLE'ORDER BY column_id;
-- Only show insertable columnsSELECT column_name, column_idFROM dba_tab_columnsWHERE owner = 'YOUR_SCHEMA' AND table_name = 'YOUR_TABLE' AND virtual_column = 'NO'ORDER BY column_id;Resolution Steps
Section titled “Resolution Steps”Step 1: Identify the Mismatch
Section titled “Step 1: Identify the Mismatch”-- For INSERT statements, count both sides:-- Left side: columns in INSERT INTO table (col1, col2, ...)-- Right side: values in VALUES (val1, val2, ...)-- These counts MUST match
-- If no column list specified, count ALL table columnsSELECT COUNT(*) as total_columnsFROM dba_tab_columnsWHERE owner = 'YOUR_SCHEMA' AND table_name = 'YOUR_TABLE';Step 2: Always Use Explicit Column Lists
Section titled “Step 2: Always Use Explicit Column Lists”-- BAD: Fragile, breaks when table structure changesINSERT INTO employees VALUES (1, 'John', 'IT', SYSDATE);
-- GOOD: Explicit, self-documenting, resilientINSERT INTO employees (id, name, department, hire_date)VALUES (1, 'John', 'IT', SYSDATE);Step 3: Validate Subquery Column Counts
Section titled “Step 3: Validate Subquery Column Counts”-- Run the subquery independently to check outputSELECT customer_id, customer_name -- Returns 2 columnsFROM customers
-- If using = operator, subquery must return exactly 1 column-- If using IN operator, subquery must return exactly 1 column-- For multi-column comparisons, use row syntax:SELECT * FROM ordersWHERE (customer_id, region_id) IN (SELECT customer_id, region_id FROM preferred_customers);Prevention Strategies
Section titled “Prevention Strategies”1. Always Specify Column Lists in INSERT
Section titled “1. Always Specify Column Lists in INSERT”-- This practice prevents ORA-00913 and ORA-00947 (not enough values)-- It also survives table alterations that add new columns
INSERT INTO orders (order_id, customer_id, order_date, total)VALUES (seq_orders.NEXTVAL, :cust_id, SYSDATE, :total);2. Use IDE Column Count Validation
Section titled “2. Use IDE Column Count Validation”-- Most IDEs highlight column count mismatches-- SQL Developer, DataGrip, and Toad all provide this-- Enable "check SQL syntax" before execution3. Template for Bulk Inserts
Section titled “3. Template for Bulk Inserts”-- When building bulk INSERT statements, generate from metadataSELECT 'INSERT INTO ' || table_name || ' (' || LISTAGG(column_name, ', ') WITHIN GROUP (ORDER BY column_id) || ') VALUES (' || LISTAGG(':' || column_name, ', ') WITHIN GROUP (ORDER BY column_id) || ');'FROM dba_tab_columnsWHERE owner = 'YOUR_SCHEMA' AND table_name = 'YOUR_TABLE'GROUP BY table_name;Related Errors
Section titled “Related Errors”- ORA-00947: Not Enough Values - Opposite problem: fewer values than columns
- ORA-00936: Missing Expression - Missing value in expression list
- ORA-01400: Cannot Insert NULL - Required column missing from INSERT
- ORA-00904: Invalid Identifier - Column name typo in INSERT column list