Export/Import query

  • Hello all,

    I have two databases, db1 and db2.

    I am pulling data from db1 with view which I have to insert in db2 table:

    - if record in db2 with same field1 and field2 exists - update;

    - if record in db2 doesn't exist, I need to insert it.

    Thanks,

    Brano

  • branovuk (8/6/2013)


    Hello all,

    I have two databases, db1 and db2.

    I am pulling data from db1 with view which I have to insert in db2 table:

    - if record in db2 with same field1 and field2 exists - update;

    - if record in db2 doesn't exist, I need to insert it.

    Thanks,

    Brano

    MERGE

    http://technet.microsoft.com/en-us/library/bb510625.aspx

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • One more issue with this specific query, which gave me headache is:

    - db1 has record like this:

    record_id, column1, column2, column3, column4.

    - db2 has record like this:

    record_id, column_id (based on column value - db2 has table with column_id, column value), and value column.

    So, I am inserting next:

    From db1: record_id, column1 in new db2 record (or update existing) with values record_id, column_id, value.

    Example (for new record):

    INSERT (from db1) (record_id, column1) with values (1, Drawings)

    INTO

    db2 (record_id, column_ID) = 1, 123 (where 123 is column_id for Drawings).

    After first, INSERT is taking second value from db1 and inserting it ...

    Thanks,

    Brano

  • branovuk (8/6/2013)


    One more issue with this specific query, which gave me headache is:

    - db1 has record like this:

    record_id, column1, column2, column3, column4.

    - db2 has record like this:

    record_id, column_id (based on column value - db2 has table with column_id, column value), and value column.

    So, I am inserting next:

    From db1: record_id, column1 in new db2 record (or update existing) with values record_id, column_id, value.

    Example (for new record):

    INSERT (from db1) (record_id, column1) with values (1, Drawings)

    INTO

    db2 (record_id, column_ID) = 1, 123 (where 123 is column_id for Drawings).

    After first, INSERT is taking second value from db1 and inserting it ...

    Thanks,

    Brano

    You are going to have to paint a far clearer picture here if you want detailed assistance. You will need to post a few things:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    Please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Thank you Sean,

    I will make it clearer.

    I don't have actual DLL, I have data/query, will try to be more clear in next post/reply on this issue.

  • First, db1 has:

    SELECT ObjectID, Media, artist, place, gender from db1.view1

    Example:

    1, paint, Arthur, Europe, male;

    2, paint, John, US, male;

    3, wood, Ann, Europe, female;

    Second, db2 has:

    SELECT CustomData.Field_ID, CustomData.Record_ID, FieldDef.Name,

    CustomData.StringValue

    FROM CustomData LEFT OUTER JOIN

    FieldDef ON FieldDef.Field_ID = CustomData.Field_ID

    Example for CustomData table (Field_ID, Record_ID, StringValue):

    10, 18031,1

    10141, 18031,Arthur

    Example for FieldDef table (Field_ID, Name):

    10, ObjectID

    10140, Media

    10141, artist

    10142, place

    10222, gender

    What I need to check: If Object_ID = 1 (or any other Object_ID which is in db1.view) exists in CustomData.

    If yes, then INSERT/UPDATE CustomData table to have:

    10, 18031,1

    10140, 18031, paint

    10141, 18031, Arthur

    10142, 18031, Europe

    10222, 18031, male

    I hope I explained better this time, it looks quite difficult for me, even to explain.

  • This does look difficult to explain with words. Using ddl this probably would be a lot simpler. Ultimately you need some help putting together a query which means that somebody on the other end needs to have something to work with.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • I am having trouble working out exactly what you are asking but see below it may help you?

    UPDATE

    --table to update

    Values1

    SET

    --column you want the value inserted into and from

    Values1._Value = Values2._Value

    -- normal join

    FROM

    Values1

    INNER JOIN

    Values2

    ON

    Values1._ID = Values2._ID

    -- use where as your if statement

    where Values1._Value = Values2._Value_2

  • bawinkley (8/6/2013)


    I am having trouble working out exactly what you are asking but see below it may help you?

    UPDATE

    --table to update

    Values1

    SET

    --column you want the value inserted into and from

    Values1._Value = Values2._Value

    -- normal join

    FROM

    Values1

    INNER JOIN

    Values2

    ON

    Values1._ID = Values2._ID

    -- use where as your if statement

    where Values1._Value = Values2._Value_2

    What I am asking for is ddl and sample data. That means create table statements and insert statements. I can't possibly know if any code I might toss together will work when I have nothing to work with. Please take a few minutes and read the article in my signature about best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • I think I explained very detailed what I need in one of my posts.

    Please check and let me know if anything missing in my second explanation:

    First, db1 has:

    SELECT ObjectID, Media, artist, place, gender from db1.view1

    Example:

    1, paint, Arthur, Europe, male;

    2, paint, John, US, male;

    3, wood, Ann, Europe, female;

    Second, db2 has:

    SELECT CustomData.Field_ID, CustomData.Record_ID, FieldDef.Name, CustomData.StringValue

    FROM CustomData LEFT JOIN

    FieldDef ON FieldDef.Field_ID = CustomData.Field_ID

    Example for CustomData table (Field_ID, Record_ID, StringValue):

    10, 18031, 1

    10141, 18031, Arthur

    Example for FieldDef table (Field_ID, Name):

    10, ObjectID

    10140, Media

    10141, artist

    10142, place

    10222, gender

    What I need to check: If Object_ID = 1 (or any other Object_ID which is in db1.view) exists in CustomData.

    If yes, then INSERT/UPDATE CustomData table to have:

    10, 18031, 1

    10140, 18031, paint

    10141, 18031, Arthur

    10142, 18031, Europe

    10222, 18031, male

  • branovuk (8/7/2013)


    I think I explained very detailed what I need in one of my posts.

    Please check and let me know if anything missing in my second explanation:

    Simply reposting the same thing is really not very helpful. You want help with code right? We need some tables and data to work with. You need to post create table scripts and inserts. Remember that we can't see your screen, we have no idea what your tables or data look like and we are not familiar with your project. It seems that you have some sort of EAV construct you are working with here. That is about all I can figure out.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 11 posts - 1 through 10 (of 10 total)

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