SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Export/Import query


Export/Import query

Author
Message
branovuk
branovuk
SSC-Enthusiastic
SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)

Group: General Forum Members
Points: 134 Visits: 307
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
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25990 Visits: 17528
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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
branovuk
branovuk
SSC-Enthusiastic
SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)

Group: General Forum Members
Points: 134 Visits: 307
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
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25990 Visits: 17528
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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
branovuk
branovuk
SSC-Enthusiastic
SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)

Group: General Forum Members
Points: 134 Visits: 307
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.
branovuk
branovuk
SSC-Enthusiastic
SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)

Group: General Forum Members
Points: 134 Visits: 307
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.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25990 Visits: 17528
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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
bawinkley
bawinkley
Grasshopper
Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)Grasshopper (10 reputation)

Group: General Forum Members
Points: 10 Visits: 42
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
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25990 Visits: 17528
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.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
branovuk
branovuk
SSC-Enthusiastic
SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)SSC-Enthusiastic (134 reputation)

Group: General Forum Members
Points: 134 Visits: 307
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search