0

I have two tables in a database, Products and ProductKeys.

Products columns:

  • ProductID (PK)
  • ProductName

ProductKeys columns:

  • ID (PK)
  • ProductKey
  • ProductID (FK)
  • Price

I am trying to insert a new ProductKey.

Let's say I have a product with ProductID = 1 and ProductName = 'test'

Insert into ProductKeys(ProductKey, ProductID, Price)
    select 'testkey', 1, 4

This of course works. But what if I want to insert a new ProductKey based on a product name and the ProductID to be set automatically based on the ProductName.

I hope you understand my issue. Thank you in advance people! Cheers.

EDIT: To insert in that table i must provide all fields a value , but what if I do not know the value of the ProductID and I know the ProductName, so I can insert a ProductKey based on a ProductName not a ProductID.

Sorry for the mess guys , new to SQL.

3
  • 2
    pls explain with some example Commented Jul 8, 2015 at 17:07
  • I'm assuming this is sql-server? Please tag your question appropriately. Additionally the c# tag is not relevant to the question. Commented Jul 8, 2015 at 18:26
  • Indeed, thank you for the heads up! Commented Jul 8, 2015 at 18:41

1 Answer 1

1

You can select data to insert based on a table's data as so:

Insert into ProductKeys(ProductKey , ProductID , Price)
select p.productName, p.productId, 1 -- whatever your price is
from products p
where p.productName = 'testkey'

Sir, can I ask you , can this be extended? for example! If the ProductName does not exist the command won't work for sure. But if in case when ProductName does not exist ALSO insert a NEW product in Products table with the selected ProductName and also create the ProductKey. Can it be extended with a 'not exist'? Thanks a bunch sir! You already helped me alot!

I don't think you could do this in a single statement - maybe you could use an on before insert trigger, but I think something like that would break down with batch inserts. (but don't quote me on this, never tried it)

You could however break it up into multiple statements like this:

declare @productName varchar(50), @productId int, @price int

select 
        @productName = 'someNameThatDoesntExist', 
        @price = 5 -- whatever your price is.

if not exists (     -- check if the product name exists in the products table, if not create it
    select 1 
    from products 
    where productName = @productName
)
    begin
        insert into products (productName)
        select @productName

        select @productId = @@identity
    end
else -- the product already exists in the products table, so lookup its ID
    select @productId = productId
    from products
    where productName = @productName

Insert into ProductKeys(ProductKey , ProductID , Price)
select @productName, @productId, @price
Sign up to request clarification or add additional context in comments.

4 Comments

This worked sir! Thanks a bunch! But a little modified. " Insert into ProductKeys (ProductKey , ProductId ,Price) select 'insert_random_key_here' , p.ProductID , 1 from products p where p.productName = 'testkey' " Brilliant! Thanks again!
Sir, can I ask you , can this be extended? for example! If the ProductName does not exist the command won't work for sure. But if in case when ProductName does not exist ALSO insert a NEW product in Products table with the selected ProductName and also create the ProductKey. Can it be extended with a 'not exist'? Thanks a bunch sir! You already helped me alot!
@MihailGeorgescu updated answer w/ example to create from your recent comment
I think breaking in two statements will also help! Thanks a bunch sir! AGAIN! <3

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.