I want to do what SQL MERGE...WHEN MATCHED...WHEN NOT MATCHED does,
but with the source and destination tables on different Azure databases. (Typically on the same server if that helps.)
The source and destination tables are not exactly the same, that's why SQL MERGE would be perfect. (I can decide what column to match on, and which columns to use in the INSERT or UPDATE.)
If this can not be done in SQL, I could load the tables into my C# code and do the merge there (will affect performance though). Does anyone know if .NET has something similar to SQL MERGE in ADO.NET (merging DataTables).
Thanks for any help!
Edit: this image shows the tables before MERGE (all Values are int):
This image shows the MERGE statement and the resulting source and dest:
Azure SQL database doesn't support across database operations directly, even these databases are in the same Azure SQL Server. it will throw below error.
To work around this Azure SQL database only support the across database query with elastic query
Example
CREATE MASTER KEY; -- create master key
GO
-- credential maps to a login or contained user used to connect to remote database
CREATE DATABASE SCOPED CREDENTIAL CrossDbCred1 -- credential name
WITH IDENTITY = 'username', -- login or contained user name
SECRET = '**********'; -- login or contained user password
GO
-- data source to remote Azure SQL Database server and database
CREATE EXTERNAL DATA SOURCE source
WITH
(
TYPE=RDBMS, -- data source type
LOCATION='server.database.windows.net', -- Azure SQL Database server name
DATABASE_NAME='database1', -- database name
CREDENTIAL=CrossDbCred1 -- credential used to connect to server / database
);
GO
-- external table points to table in an external database with the identical structure
CREATE EXTERNAL TABLE [dbo].[source]
(
[Id] [varchar](50),
[value1] [int],
[value2] [int],
[value3] [int]
)
WITH (DATA_SOURCE = [source], -- data source
SCHEMA_NAME = 'dbo', -- external table schema
OBJECT_NAME = 'source' -- name of table in external database
);
GO
Now we can test our Elastic Query to merge tables.
MERGE into destination1 B
USING source E
ON (B.Id = E.Id)
WHEN MATCHED THEN
UPDATE SET value2 = E.value2, value3 = E.value3
WHEN NOT MATCHED THEN
INSERT (Id,value2,value3) VALUES (Id,value2,value3);
Output
Before merging
After merging
Related
Using ADO.NET with the MySql Connector (MySql.Data) driver, I open a new, non-pooled query (no initial catalog/database is specified in connection string) and issue the following query:
ALTER TABLE `public`.`table2`
ADD CONSTRAINT `fk_table2_table1`
FOREIGN KEY (`table1_id`) REFERENCES `table1` (`id`);
The structure of the two tables is as follows:
CREATE TABLE `public`.`table1`
(
id int primary key
);
CREATE TABLE `public`.`table2`
(
id int primary key,
table1_id int
);
The ALTER TABLE query causes the following exception:
MySql.Data.MySqlClient.MySqlException (0x80004005): No database selected
However, with the same setup, if I run the ALTER TABLE query using the mysql command line tool (and several other MySQL clients) I get no such error.
Does anyone have suggestions to things I can look into. I'm currently thinking this is an issue with the MySql Connector (MySql.Data) driver, however, I could easily be wrong here.
Update:
We are not able to reproduce this with a minimally viable repro. It is likely, the issue lies somewhere in our own code and not in the driver.
Another update:
We were actually able to successfully reproduce this using just the mysql command line tool. It requires Mysql 5.6.34 (won't repro in many other versions I tested including 5.6.41). Also, we reproduced in AWS RDS but not sure if that is required. In order to reproduce perform the following steps:
1) Fresh database install
2) login via mysql command line tool
3) create two tables
create schema public;
create table public.table1( id int primary key);
create table public.table2(id int primary key, table1_id int);
alter table public.table2 add foreign key fk_name (table1_id) REFERENCES table1(id);
The final command results in the error:
ERROR 1046 (3D000): No database selected
I Have two copies of the same sql server database (DB1 & DB2). What is the best way to do the following :
Update the data of some tables in DB2 To be the same as DB1 (ie: DB1 is the source And DB2 is the destination)
and update the data of some other tables in DB1 to be the same as DB2 (ie: DB2 is the source And DB1 is the destination)
How to make this using c# program? hope to help me in the best soulution to do that.
keep in mind that there is no connection between the program and the two copies at the same time and the tables has many relations up on Identity columns
Thanks :)
My suggestion would be to setup a linked server and then create stored procedure that make use of MERGE SQL command.
Note this is semi pseudo code example
MERGE DB2 AS target
USING DB1 AS source
ON <Primary Keys>
WHEN MATCHED THEN
UPDATE SET Key = source.Value
WHEN NOT MATCHED THEN
INSERT (<Columns>)
VALUES (source.values)
OUTPUT deleted.*, $action, inserted.* INTO #MyTempTable;
Then you can create a SqlConnection/SqlCommand and then call the stored procedure. This code you can ultimatelt host in a windows service or normal .Net event i.e. button click, OnLoad as example.
My company is using SQL Server Compact to store and manage a local database and I need to get the primary key information from the database tables.
I have used OleDbConnection.GetSchema() in the past to get this information from Access databases using Jet and OLE drivers. But it appears that SqlCeConnection.GetSchema() throws a NotImplementedException (or rather that it returns DBConnection.GetSchema() which throws the same exception).
I don't need all of the information normally returned by GetSchema(), just the names of the PK's. Is there a good way to find out the Primary Key for a table in a SQL Server Compact database?
NOTE: The above method references are not static function calls. Consider the class name to be an object reference of that type.
SELECT INDEX_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.INDEXES
WHERE PRIMARY_KEY = 1
AND TABLE_NAME = 'MyTable';
i'm creating a shopping website for buying computer parts. i want to accept advertisement(images) and also provide a way to upload drivers(exe or msi) of the items locally. i already have the following tables in the SQL Server 2005 database:
--here is the driver table
tblDriver (driver_id bigint primary key identity(1,1),
driver_name nvarchar(max) not null, driver_address nvarchar(500) not null)
--here is the ad table
tblAd (ad_id bigint primary key identity(1,1), ad_address nvarchar(500) not null)
so where should i save the actual files? i'm looking for a clean solution with no truble for CRUD operations.
As usual - you may either store the files into DB, or store them into filesystem.
Storing in the DB has its advantages - you always will have a consistent backup.
Store in filesystem for faster file read.
But you may combine both methods and store files in DB as FILESTREAM column. Although if I remember right - FILESTREAMs are available only starting from Sql Server 2008.
IMO better solution is to save the files in file system and store the path or location in database.
i have an idea to call two values from two different databases and comapre them in one statement? is it possible?
i am working with c# and MS-SQL
Yes.
For MSSQL you can add the database name in front of your table. You normally have 4 namespaces you can use
[Server name].[database name].[owner].[table_name]
So if you want to compare two values in the one statement you should only need to join across the tables by placing the database name in front of the table name.
If the databases are on different servers then you will need to create a linked server to the side which will run your SQL so that its aware of the other sql server. You can add linked servers in Management studio or via SQL using something like sp_addlinkedserver
You may do cross database joins to compare these values:
SELECT
db1.Value as value1,
db2.Value as value2
FROM
[database1].dbo.MyTable1 as db1
INNER JOIN
[database2].dbo.MyTable as db2
ON
/* insert join clasue */
There are a few possibilities here depending on your setup. If your databases are different SQL Server installations then you will want to look at sp_linkedserver first. Once you have the ability to see both databases using the same login you could just execute the following query where db1 and db2 are the databases, dbo is the owner and tab1 and tab2 are the respective tables.
SELECT a.col1
FROM db1.dbo.tab1 a, db2.dbo.tab2 b
WHERE a.col1 = b.col2
If you should happen to lack the SQL Server permissions to create a linked server, you could create connections to each server and your client could attach to the servers using Microsoft JET library, and then you could perform the heterogeneous join client-side.