Simple Insert

  • Thanks,

    Dam again!

  • Here is what i have so far, but no data has changed, but i also do not receive an error.

    This is all that it says--->

    Running dbo."p_copyproduct" ( @newProductId = 11, @prevProductId = 15 ).

     

     

    ============This is the procedure-----------

    ALTER procedure p_copyproduct

    (@newProductId int, @prevProductId int)

    as

    If Not exists

    (select ProductId from ProductAttribute where productId=@NewProductId)

    begin

    Insert

    ProductAttribute (ProductId , AttributeId)

    select

    @newProductId , AttributeId

    from

    ProductAttribute

    where

    ProductId = @prevProductId

    end

    Dam again!

  • Hey dude you're selecting from the same table you are inserting to, that can't insert rows and not cause duplicates.

  • I am selecting the attributeID's that are already in the table and reinserting them with new ProductID's. Alot of the same products share the same attributes.

    Dam again!

  • Product a cannot be blue, black and blue again. Each attribute must be unique to the product in that table.

  • I have a pair of socks that does that blue and black thing...

    Can i get a little hint if i am getting a little closer..Please

    ======================SProc

    ALTER procedure

    p_copyproduct

    (@newProductID

    int, @prevProductId int)

    as

    begin

    Insert

    ProductAttribute (ProductId , AttributeId)

    select

    @newProductId , AttributeId

    from

    ProductAttribute

    where

    @newproductID not in (select productid from productattribute)

    and

    where

    ProductId = @prevProductId

    Dam again!

  • What do you want to do exactly?

  • I have a table the has two columns in it (http://afcc1.com/sqlHELP/sqlMajor.jpg (The one on the far right)) 

    I would like to copy some of the AttributeID's and reinsert them with new ProductID's (With out deleting the existing information) In other words i need to use the same AttributeID's with a different ProductID.

    This is a many to many table. Where many Many Products can share Many Attributes.../

    Erik.,..

    Dam again!

  • Sorry I mislead you a little bit because I didn't remember what had to be done :

    ALTER procedure p_copyproduct

    (@newProductId int, @prevProductId int)

    as

    SET NOCOUNT ON

    Insert into dbo.ProductAttribute (ProductId , AttributeId)

    select @newProductId , AttributeId

    from dbo.ProductAttribute

    where ProductId = @prevProductId

    and not exists (Select * from dbo.ProductAttribute PA2 where PA2.ProductId = @newProductId and PA2.AttributeId = ProductAttribute.AttributeId)

    SET NOCOUNT OFF

    GO

    This will copy the attributes from the old to the new product only if they don't already exist for the new product.

  • Thank You, This is my first dealing with the insert from one table to another so i am happy to have your help.

    Thanks,

    Erik...

    Dam again!

  • HTH .

Viewing 11 posts - 16 through 25 (of 25 total)

You must be logged in to reply to this topic. Login to reply