Insert data in rows after a select

  • Hi!

    scenario: I want to insert data in rows from specific conditions.

    select anadesc,idenvaseana,idegrupoana,matid,tipaid

    from dbo.analisis

    where (anadesc like '%disuelto%') and (matid = 'am') and (tipaid = 'mt')

    How can i insert the data in the output of this consult?

    thanks in advance.

  • Assuming your target table is called TargetTable, your code should look something like this:

    INSERT TargetTable (anadesc,idenvaseana,idegrupoana,matid,tipaid)

    select anadesc,idenvaseana,idegrupoana,matid,tipaid

    from dbo.analisis

    where (anadesc like '%disuelto%') and (matid = 'am') and (tipaid = 'mt')

  • benyos (9/9/2009)


    Assuming your target table is called TargetTable, your code should look something like this:

    INSERT TargetTable (anadesc,idenvaseana,idegrupoana,matid,tipaid)

    select anadesc,idenvaseana,idegrupoana,matid,tipaid

    from dbo.analisis

    where (anadesc like '%disuelto%') and (matid = 'am') and (tipaid = 'mt')

    thanks man!!

    i guess i should add de values command

    INSERT TargetTable (anadesc,idenvaseana,idegrupoana,matid,tipaid)

    VALUES (1,2,3,4,5)

    select anadesc,idenvaseana,idegrupoana,matid,tipaid

    from dbo.analisis

    where (anadesc like '%disuelto%') and (matid = 'am') and (tipaid = 'mt')

    [/quote]

  • Not sure what you're trying to do here...

    You have an "insert...values..." command and then "select...where..."

    What are you trying to achieve? Do you mean to update records that correspond to specific criteria or insert new ones?

  • benyos (9/9/2009)


    Not sure what you're trying to do here...

    You have an "insert...values..." command and then "select...where..."

    What are you trying to achieve? Do you mean to update records that correspond to specific criteria or insert new ones?

    i want to insert new records that correspond to specific criteria.

  • i want to insert new records that correspond to specific criteria.

    That means:

    1. You are inserting new records

    2. You specify values for the new records so they match certain criteria.

    In this case, you code would look like this...

    INSERT TargetTable (anadesc,idenvaseana,idegrupoana,matid,tipaid)

    VALUES ('some string that contains disuelto like this record just did','idenvaseana goes here','idegrupoana goes here','am','mt')

    And your values may come from 'values' list like I showed here or another select on a table, like I first posted

  • benyos (9/9/2009)


    i want to insert new records that correspond to specific criteria.

    That means:

    1. You are inserting new records

    2. You specify values for the new records so they match certain criteria.

    In this case, you code would look like this...

    INSERT TargetTable (anadesc,idenvaseana,idegrupoana,matid,tipaid)

    VALUES ('some string that contains disuelto like this record just did','idenvaseana goes here','idegrupoana goes here','am','mt')

    And your values may come from 'values' list like I showed here or another select on a table, like I first posted

    thanks benyos!

    i will try it and post my results 😀

  • hi!

    to any other noobs around here...the solution.:-D :

    update dbo.analisis

    set idegrupoana = '10'

    where (matid = 'am') and (tipaid = 'mt')and(idegrupoana is NULL)and

    (anadesc like '%disuelto%')

    it worked like a charm.!!

    thanks to sql server central!! 😉

Viewing 8 posts - 1 through 7 (of 7 total)

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