SQL - Limit the values in an Insert from another table

im trying to insert the following:

insert into TABLEA select b.ID,..... from TABLEB b where code = 'NL'

the problem is that select * from TABLEB where code = 'NL'; returns more than 1 value.

is there anyway to limit that to just 1 value?

i tried select min(b.id) but it didnt work

2 Answers

from what I can gather you want just one instance of where code = NL to be inserted even though there are multiple records that fit this.

If this is what you want, change your "select *" to "select top 1 *" and it will only pull one record from your sub query.

1

use where rownum = 1

insert into TABLEA select b.ID,..... from TABLEB b where code = 'NL' and rownum = 1 

But are you sure you want to do this? What if its actually the 2nd row you want. You might want to figure out why your query is returning more than one.

0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like