how to update table from linked server in sql server? - sql

I have few tables I need to load from linked firebird server into SQL Server from time to time. I use statement like this:
SELECT * into dekr FROM OPENQUERY ( [PLINK] ,'select * from dekr' )
It takes a while since it's going over network etc, is there a way to update once created
table dekr only with changes since last time?
thanks

Related

Passthrough Query from one SQL Server to another that drops a table on the source

I have a SQL Server in Spain and one in the US and there is a domain trust between the two with linked servers on each for access to the other.
I would like to be able to run the below query on the US SQL Server without having to maintain a stored proc on the US Server in order to run it. Is there a way to create a passthrough query from the SQL Server in Spain? I've already tried using OPENQUERY and OPENROWSET and it's just not working as they only seem to work with select statements that return results:
DROP TABLE IF EXISTS [Global].[dbo].[WW_Customer_Receivables]
SELECT *
INTO [global].[dbo].[ww_customer_receivables]
FROM
[LinkedServerObject-Spain].[global].dbo.ww_customer_receivables
If you want to execute DDL statement on your linked server with openquery you can with the following trick:
SELECT *
FROM OPENQUERY(linkedserver, '
DROP TABLE IF EXISTS [Global].[dbo].[WW_Customer_Receivables]
SELECT ##RowCount')
The SELECT ##RowCount returns a result set. So OPENQUERY works.
This trick works for all DDL operations like create, alter, drop. Or if you want to perform inserts/updates/deletes that don't return a result set.
Same trick is applied here Where they have a dummy select foobar.
If you want to execute the into statement from the openquery you can do it like this:
DROP TABLE IF EXISTS [Global].[dbo].[WW_Customer_Receivables]
SELECT *
INTO [global].[dbo].[ww_customer_receivables]
FROM OPENQUERY([LinkerServerObject-US], '
SELECT *
FROM [global].dbo.ww_customer_receivables')

How to create a table using the INSERT INTO clause using linked servers in SQL Server Management Studio

I have a server called GreatPlains and I would like to create a new table (not already defined) using the INSERT INTO clause onto my local server's reporting database. We have a linked server set up for the GreatPlains server and our main production server.
Simplified version of current query:
SELECT *
INTO [local].[Reporting].[dbo].[NewTable]
FROM [linked].[Main].[dbo].[Orders]
I'm also getting the error:
The object name 'local.Reporting.dbo.NewTable' contains more than the
maximum number of prefixes. The maximum is 2.
There are two mistakes in your query
1.INTO clause support maximum of 2 prefixes. You cannot include SERVER NAME
DATABASE_NAME.SCHEMA_NAME.TABLE_NAME
2.Unwanted INSERT ketword
So your query should be
SELECT *
INTO [Reporting].[dbo].[NewTable]
FROM [linked].[Main].[dbo].[Orders];
I think you have an extra insert:
SELECT *
INTO [local].[Reporting].[dbo].[NewTable]
FROM [linked].[Main].[dbo].[Orders];
If the table is already defined and has the same columns in the same order, then you can do:
INSERT INTO [local].[Reporting].[dbo].[NewTable]
SELECT *
FROM [linked].[Main].[dbo].[Orders];
If you are running the script in your local server and table already exists in the database,Use the below script.
USE [Reporting]
GO
INSERT INTO [dbo].[NewTable]
SELECT *
FROM [linked].[Main].[dbo].[Orders]
If you don't have the table in your database,use the below script.
USE [Reporting]
GO
SELECT *
INTO dbo.[NewTable]
FROM [linked].[Main].[dbo].[Orders]

Export data from SQL Server to Oracle using SSIS

I want to export some data from SQL Server to Oracle but I have this scenario:
My table in SQL I need to save my ID in a parameter in SSIS to use this because I want to run this package every 30 minutes.
So if my last ID in SQL exported to oracle is 10, I will save this on a variable in SSIS to change my query like this "SELECT * FROM TABLE WHERE ID > PARAMETER"
But I have no idea how to do it. help me
You can't store a variable in SSIS and have it last between sessions. The ID is in a table, yes? You can do something along the lines of
select
*
from
table
where
id > (select max(id) from <destination table>)
Or, if you're looking at completely different databases, you can populate a variable with the max(id) from your destination, and then plug that variable into your select from your source.

how to handle dates before 2000 through linked server in SQL Server 2012?

I have linked server established from firebird database to SQL Server 2012 and looks like
when copying data the dates before 2000 year appears as 30-13-1899, why is that?
anyway to go around this weird behavior? I copy data like this:
SELECT * into #temp FROM OPENQUERY ( [test] ,'select * from testtable' )
thanks

To get data from table in script from sql server 2005

I am using sql server 2005
I have a table [say tblHistory] and this table contains 100 rows.
I have created the same table at the server, but the table doesn't have the data, I want data from tblHistory to convert into
INSERT INTO tblHistory ------
so that I could run the script on the server to fill the database.
To generate all the INSERT INTO statements you need based on table data, take a look at this project: http://www.codeproject.com/KB/database/sqlinsertupdategenerator.aspx
you need to create a linked server between the two servers and then you do something like this
INSERT INTO tblHistory
select * from LinkedServerNAme.DatabaseName.SchemaName.tblHistory
To add a linked server read this http://msdn.microsoft.com/en-us/library/ms190479.aspx
BTW you can also use OPENROWSET, OPENDATASOURCE, SSIS or bcp out and bcp in
Not sure I understand the question... you just want to copy the contents of one table into another?
INSERT INTO newTable SELECT * FROM tblHistory
If the new table doesn't already exist, you can use SELECT INTO:
SELECT *
INTO new_table
FROM tblHistory
But that's the caveat - it has to be a new table, no data already in there.
Otherwise, use:
INSERT INTO new_table
SELECT x.* --preferrable to actually define the column list than use *
FROM OLD_TABLE x
You should check out the SSMS Tools Pack - one of its feature is the ability to generate those INSERT scripts you're looking for!
Get the SSMS Tool Pack from this site - it's an add-in for SQL Server Management Studio - highly recommended!