1

insert into ASSET_MAIN_CATEGORIES values(select max(sno) from ASSET_MAIN_CATEGORIES, 'PROD','AC HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1);

I wanted to insert max of SNO as column value. But It is showing "Missing Expression Error"

How can I achieve this.

Any Help would be appreciated.

2
  • show the table defnition extra columns are getting added up in insert thats why your are getting too many rows. Commented Aug 7, 2018 at 14:53
  • its showing missing expression because sql statement is not within brackets and some ; ... may be required. Commented Aug 7, 2018 at 14:55

3 Answers 3

1

There are at least ways of doing it:

INSERT INTO ... VALUES with embedded select in parentheses so that database will evaluate it:

insert into ASSET_MAIN_CATEGORIES values(
  (select max(sno) from ASSET_MAIN_CATEGORIES),
  'PROD','AC HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1
);

INSERT INTO ... SELECT since all other data is static:

insert into ASSET_MAIN_CATEGORIES
  select
    max(sno),
    'PROD','AC HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1
  from ASSET_MAIN_CATEGORIES
;

Note that if you do not specify which columns you are populating in ASSET_MAIN_CATEGORIES database assumes that you're feeding values for all of them in the order that they were created in this table.

Sign up to request clarification or add additional context in comments.

1 Comment

@vishnu the problem is with your INSERT statement, and I already wrote on that in the last section of my answer. You are providing more values than you have columns in your table.
1

You need to surround select max with brackets, because it's a SQL statement:

insert into ASSET_MAIN_CATEGORIES values((select max(sno) from ASSET_MAIN_CATEGORIES), 'PROD','AC HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1);

If you have an error, prefer to add column names in insert statement

1 Comment

@vishnu so you have more values than columns, you need to show columns and their types, or remove extra value(s), normally in insert you state the column names before values to see avoid such issues . see docs.oracle.com/cd/B12037_01/appdev.101/b10807/13_elems025.htm
0

this will work :-

i have checked with my tables its working ,sample code to understand:

insert into b(col1,col2) select 7,(select max(col2) from b)
  from dual;

what code you require:-

 insert into ASSET_MAIN_CATEGORIES(col1,col2,....,col10) 
 select (select max(col2) from ASSET_MAIN_CATEGORIES), 'PROD','AC 
 HMU','AC_HMU','PRODUCT','99CS002','','NR','LKO',1)
 from dual;

1 Comment

check now its working in col1.col2,..,col 10 you have to pass your column_names that is why i was asking for tables desc from you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.