Marcus Marcus - 1 year ago 70
SQL Question

Insert into select not working no idea why

Is it possible to do this?

insert into cart(cartid,rowid,productname) select max(cartid)+1 from cart, 2, "hello"

i want to insert my own values of rowid and productname

Answer Source

This is how you phrase the query:

insert into cart (cartid, rowid, productname)
    select max(cartid) + 1, 2, 'hello'
    from cart;

However, this is the wrong way to have an auto-incremented cartid. Instead, define the column to be auto-incrementing and simply do:

insert into cart (rowid, productname)
    select 2, 'hello'
    from cart;

The database takes care of the assignment automatically.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download