Run PostgreSQL stored procedure from SQL Server - sql-server

I have a database in SQL Server which contains collected data during one day, and a database in PostgreSQL with OSM data. I need to modify collected data in order to create reports for my users.
Now, I imagined that somehow call PostgreSQL procedure from SQL Server, pass collected data to PostgreSQL, do something with that data, and return another result set to SQL Server for creating reports.
What is the 'most efficient' way for achieving this? OR, better question atm, what is the way for achieving this functionality?
My idea is to connect SQL Server and PostgreSQL with PostgreSQL ODBC driver, then copy data from SQL Server to PostgreSQL table, run that stored procedure on PostgreSQL, and return data to SQL Server result table. But, it is not scheduled task. Data to be transferred to PostgreSQL contains latitude, longitude and bearing for about 2-3 million of rows and function which analyses them requires one per one record, not all at once.

My way is using C# and npgsql to connect to postgres.
I create an app:
check SQL Server every min.
check Postgres for the last id inserted.
create a dataset from MSQL with the news ids
insert the new records into Postgres.
run postgres store procedure to generata new data
create separated webservice to consume the report generated on postgres.

Related

SSIS, query Oracle table using ID's from SQL Server?

Here's the basic idea of what I want to do in SSIS:
I have a large query against a production Oracle database, and I need the following where clause that brings in a long list of ids from SQL Server. From there, the results are sent elsewhere.
select ...
from Oracle_table(s) --multi-join
where id in ([select distinct id from SQL_SERVER_table])
Alternatively, I could write the query this way:
select ...
from Oracle_table(s) --multi-join
...
join SQL_SERVER_table sst on sst.ID = Oracle_table.ID
Here are my limitations:
The Oracle query is large and cannot be run without the where id in (... clause
This means I cannot run the Oracle query, then join it against the ids in another step. I tried this, and the DBA's killed the temp table after it became 3 TB in size.
I have 160k id's
This means it is not practical to iterate through the id's one by one. In the past, I have run against ~1000 IDs, using a comma-separated list. It runs relatively fast - a few minutes.
The main query is in Oracle, but the ids are in SQL Server
I do not have the ability to write to Oracle
I've found many questions like this.
None of the answers I have found have a solution to my limitations.
Similar question:
Query a database based on result of query from another database
To prevent loading all rows from the Oracle table. The only way is to apply the filter in the Oracle database engine. I don't think this can be achieved using SSIS since you have more than 160000 ids in the SQL Server table, which cannot be efficiently loaded and passed to the Oracle SQL command:
Using Lookups and Merge Join will require loading all data from the Oracle database
Retrieving data from SQL Server, building a comma-separated string, and passing it to the Oracle SQL command cannot be done with too many IDs (160K).
The same issue using a Script Task.
Creating a Linked Server in SQL Server and Joining both tables will load all data from the Oracle database.
To solve your problem, you should search for a way to create a link to the SQL Server database from the Oracle engine.
Oracle Heterogenous Services
I don't have much experience in Oracle databases. Still, after a small research, I found something in Oracle equivalent to "Linked Servers" in SQL Server called "heterogeneous connectivity".
The query syntax should look like this:
select *
from Oracle_table
where id in (select distinct id from SQL_SERVER_table#sqlserverdsn)
You can refer to the following step-by-step guides to read more on how to connect to SQL Server tables from Oracle:
What is Oracle equivalent for Linked Server and can you join with SQL Server?
Making a Connection from Oracle to SQL Server - 1
Making a Connection from Oracle to SQL Server - 2
Heterogeneous Database connections - Oracle to SQL Server
Importing Data from SQL Server to a staging table in Oracle
Another approach is to use a Data Flow Task that imports IDs from SQL Server to a staging table in Oracle. Then use the staging table in your Oracle query. It would be better to create an index on the staging table. (If you do not have permission to write to the Oracle database, try to get permission to a separate staging database.)
Example of exporting data from SQL Server to Oracle:
Export SQL Server Data to Oracle using SSIS
Minimizing the data load from the Oracle table
If none of the solutions above solves your issue. You can try minimizing the data loaded from the Oracle database as much as possible.
As an example, you can try to get the Minimum and Maximum IDs from the SQL Server table, store both values within two variables. Then, you can use both variables in the SQL Command that loads the data from the Oracle table, like the following:
SELECT * FROM Oracle_Table WHERE ID > #MinID and ID < #MaxID
This will remove a bunch of useless data in your operation. In case your ID column is a string, you can use other measures to filter data, such as the string length, the first character.

How to Migrate data from multiple T-SQL Queries to Single Table?

I want to enter data from mutiple T-SQL queries into my azure sql database, We want to enter data in such a way so that we have 8 columns in a single table in azure sql database, and for those 8 columns we have multiple T-SQL statements that 1 for each that will enter the data from the select statments into the azure sql database, how can this be achieved, for long term we want this to run as a job going forward.
If your multiple T-SQL queries run in one database, I suggest you can think about the Azure Data Factory.
Azure Data Factory can help migrate data from one table or multiple tables to Azure SQL database by T-SQL queries.
You also can trigger the pipeline runs on a schedule. You can create a scheduler trigger to schedule the pipeline to run periodically (hourly, daily, and so on).
For details about Data factory, please see Azure Data Factory Documentation.
Tutorials:
Incrementally load data from multiple tables in SQL Server to an Azure SQL database
Copy multiple tables in bulk by using Azure Data Factory.
And if your source data is in SQL Server instance, you can create a linked server to Azure SQL Database, this also can help you achieve that.
You can query and insert data to linked Azure SQL server by T-SQL statements.
About SQL Server linked server, please see: Create Linked Servers (SQL Server Database Engine)
Hope this helps.

Unable to migrate data from sql server to aurora Mysql DB using AWS data migration service

I am unable migrating data from SQL server to aurora MySQL DB using AWS DMS service. Currently, I have a different number of columns for a particular table in MySQL DB due to which I am unable to transfer data from SQL server to aurora MySQL DB.
Please check the below image for reference.
As the picture suggests, I want to transfer data from booking table in SQL server to booking table in aurora Mysql DB having less number of columns.
Can anyone suggest a way to do it?
There may be a way to cope with this directly from AWS without having to change your schema. But, one option here would be to simply create a table in MySQL which matches the column/type count of the counterpart in SQL Server, i.e.
CREATE TABLE booking (bookingID int, bookingVersion int, ...) -- 7 columns
Then, migrate the data from SQL Server to MySQL using the AWS tool. Finally, just drop the bookingVersion column from your MySQL table:
ALTER TABLE booking DROP COLUMN bookingVersion;
This should work, because all the extra DML steps I suggested can completely be done within MySQL, and don't involve your SQL Server database at all.

SSIS update DB2 database from SQL Server

I have an SSIS package which imports full table data from DB2 database and loads in SQL Server on a daily basis. At the end of the day I need to post only updates (only some rows of table which are edited) to DB2 from SQL Server.
I have couple of options to do this task
Write a stored procedure using linked servers and update only required rows to DB2
Use script component in SSIS and perform row by row update.
Both of them can work, however I don't want to use either of them. Is there any other better solution apart from these which I can perform in SSIS?
Note : I cannot truncate and load all records from SQL Server to db2 as this is not a feasible solution.
Thank you

Compare millions of records from Oracle to SQL server

I have an Oracle database and a SQL Server database. There is one table say Inventory which contains millions of rows in both database tables and it keeps growing.
I want to compare the Oracle table data with the SQL Server data to find out which records are missing in the SQL Server table on daily basis.
Which is best approach for this?
Create SSIS package.
Create Windows service.
I want to consume less resource to achieve this functionality which takes less time and less resource.
Eg : 18 millions records in oracle and 16/17 millions in SQL Server
This situation of two different database arise because two different application online and offline
EDIT : How about connecting SQL server from oracle through Oracle Gateway to SQL server to
1) Direct query to SQL server from Oracle to update missing record in SQL server for 1st time.
2) Create a trigger on Oracle which gets executed when record is deleted from Oracle and it insert deleted record in new oracle table.
3) Create SSIS package to map newly created oracle table with SQL server to update SQL server record.This way only few records have to process daily through SSIS.
What do you think of this approach ?
I would create an SSIS package and load the data from the Oracle table use a Data Flow / OLE DB Data Source. If you have SQL Enterprise, the Attunity Connectors are a bit faster.
Then I would load key from the SQL Server table into a Lookup transformation, where I would match the 2 sources on the key, and direct unmatched rows into a separate output.
Finally I would direct the unmatched rows output to a OLE DB Command, to update the SQL Server table.
This SSIS package will require a lot of memory, but as the matching is done in memory with minimal IO, it will probably outperform other solutions for speed. It will need enough free memory to cache all the keys from the SQL Server Table.
SSIS also has the advantage that it has lots of other transformation functions available if you need them later.
What you basically want to do is replication from Oracle to SQL Server.
You could do this in SSIS, A windows Service or indeed a multitude of platforms.
The real trick is using the correct design pattern.
There are two general design patterns
Snapshot Replication
You take all records from both systems and compare them somewhere (so far we have suggestions to compare in SSIS or compare on Oracle but not yet a suggestion to compare on SQL Server, although this is valid)
You are comparing 18 million records here so this is a lot of work
Differential replication
You record the changes in the publisher (i.e. Oracle) since the last replication then you apply those changes to the subscriber (i.e. SQL Server)
You can do this manually by implementing triggers and log tables on the Oracle side, then use a regular ETL process (SSIS, command line tools, text files, whatever), probably scheduled in SQL Agent to apply these to the SQL Server.
Or you could do this by using the out of the box replication capability to set up Oracle as a publisher and SQL as a subscriber: https://msdn.microsoft.com/en-us/library/ms151149(v=sql.105).aspx
You're going to have to try a few of these and see what works for you.
Given this objective:
I want to consume less resource to achieve this functionality which takes less time and less resource
transactional replication is far more efficient but complicated. For maintenance purposes, which platforms (.Net, SSIS, Python etc.) are you most comfortable with?
Other alternatives:
If you can use Oracle gateway for SQL Server then you do not need to transfer data and can make the query directly.
If you can't use Oracle gateway, you can use Pentaho data integration or another ETL tool to compare tables and get results. Is easy to use.
I think the best approach is using oracle gateway.Just follow the steps. I have similar type of experience.
Install and Configure Oracle Database Gateway for SQL Server.
https://docs.oracle.com/cd/B28359_01/gateways.111/b31042/installsql.htm
Now you can create a dblink from oracle to sql server.
Create a procedure which compare the missing records in oracle database and insert into sql server database.
For example, you can use this statement inside your procedure.
INSERT INTO "dbo"."sql_server_table"#dblink_name("column1","column2"...."column5")
VALUES
(
select column1,column2....column5 from oracle_table
minus
select "column1","column2"...."column5" from "dbo"."sql_server_table"#dblink_name
)
Create a scheduler which execute the procedure daily.
When both databases are online, missing records will be inserted to sql server. Otherwise the scheduler fail or you can execute the procedure manually.
It takes minimum resource.
I will suggest having a homemade ETL solution.
Schedule an oracle job to export source table data (on a daily
manner based on the application logic ) to plain CSV format.
Schedule a SQL-Server job (with acceptable delay from first oracle job) to read this CSV file and import it
to a medium table inside sql-servter using BULK INSERT.
Last part of the SQL-Server job will be reading medium table data
and do the logic(insert, update target table). I suggest having another table to store reports of this daily job result.

Resources