ORA-01730: Invalid Number of Column Names Specified
ORA-01730 is thrown by Oracle when you create or replace a view and the number of column aliases listed in the view header does not match the number of columns returned by the SELECT statement. This is a purely structural mismatch that Oracle catches at parse time, before any data is accessed. The fix is always straightforward: align the two counts.
Top 3 Causes
1. Column Alias List Doesn't Match SELECT Output
The most common cause. When you explicitly name columns in the view header, the count must exactly equal the number of expressions in the SELECT clause.
-- WRONG: 3 aliases, but SELECT returns 4 columns
CREATE VIEW emp_view (emp_id, emp_name, dept_name)
AS
SELECT e.employee_id,
e.first_name || ' ' || e.last_name,
d.department_name,
e.salary -- 4th column, no alias in header
FROM employees e
JOIN departments d ON e.department_id = d.department_id;
-- ORA-01730: invalid number of column names specified
-- CORRECT: 4 aliases match 4 SELECT columns
CREATE VIEW emp_view (emp_id, emp_name, dept_name, salary)
AS
SELECT e.employee_id,
e.first_name || ' ' || e.last_name,
d.department_name,
e.salary
FROM employees e
JOIN departments d ON e.department_id = d.department_id;
2. Forgetting to Update the Header After Adding a Column
When you modify a view with CREATE OR REPLACE VIEW and add a new column to the SELECT clause, but forget to add the corresponding alias in the header list.
-- Original view with 3 columns
CREATE OR REPLACE VIEW sales_summary (region, total_sales, sale_count)
AS
SELECT region, SUM(amount), COUNT(*)
FROM sales
GROUP BY region;
-- WRONG: Added AVG column to SELECT but not to header
CREATE OR REPLACE VIEW sales_summary (region, total_sales, sale_count)
AS
SELECT region, SUM(amount), COUNT(*), AVG(amount) -- now 4 columns
FROM sales
GROUP BY region;
-- ORA-01730
-- CORRECT: Header updated to include avg_amount
CREATE OR REPLACE VIEW sales_summary (region, total_sales, sale_count, avg_amount)
AS
SELECT region, SUM(amount), COUNT(*), AVG(amount)
FROM sales
GROUP BY region;
3. Dynamically Generated DDL Scripts
Batch scripts or application code that build CREATE VIEW statements programmatically can produce mismatched counts when the column-list-building logic and the SELECT-list-building logic fall out of sync.
-- Always verify the column count before executing dynamic DDL
DECLARE
v_header VARCHAR2(200) := 'col_a, col_b, col_c';
v_select VARCHAR2(200) := 'a, b, c, d'; -- 4 items, mismatch!
v_h_count NUMBER;
v_s_count NUMBER;
BEGIN
v_h_count := REGEXP_COUNT(v_header, ',') + 1;
v_s_count := REGEXP_COUNT(v_select, ',') + 1;
IF v_h_count != v_s_count THEN
RAISE_APPLICATION_ERROR(-20001,
'Column count mismatch: header=' || v_h_count
|| ' vs select=' || v_s_count);
END IF;
-- proceed with EXECUTE IMMEDIATE only if counts match
END;
/
Quick Fix Solutions
Option A – Fix the header count
Count every expression in your SELECT clause and match the header list exactly.
-- Use this query to inspect an existing view's columns
SELECT column_name, column_id
FROM user_tab_columns
WHERE table_name = 'YOUR_VIEW_NAME'
ORDER BY column_id;
Option B – Drop the header entirely and use inline aliases
This is the safest long-term approach because it eliminates the possibility of a count mismatch.
CREATE OR REPLACE VIEW dept_summary AS
SELECT d.department_id AS dept_id,
d.department_name AS dept_name,
COUNT(e.employee_id) AS emp_count,
ROUND(AVG(e.salary), 2) AS avg_salary
FROM departments d
LEFT JOIN employees e ON d.department_id = e.department_id
GROUP BY d.department_id, d.department_name;
Prevention Tips
Prefer inline column aliases over view-header lists. Defining aliases with
ASdirectly in the SELECT clause removes the need to keep two separate lists in sync and completely prevents ORA-01730.Add a validation step to your deployment pipeline. Before running any view DDL, execute the bare SELECT statement in a test environment and confirm the column count. Include a checklist item in code reviews to verify that any changes to the SELECT clause are reflected in the view header when a header is used.
Related Errors
| Error Code | Description |
|---|---|
| ORA-00957 | Duplicate column name in view definition |
| ORA-00998 | Expression must be named with a column alias |
| ORA-04063 | View has errors (invalid state after base table change) |
📖 Want a more detailed guide?
Check out the full in-depth version (Korean) on oraerror.com — includes detailed analysis, additional SQL examples, and prevention tips.
Top comments (0)