how can i insert from SQL Server to a SQLBase Database using linked Server?
Here are various examples of the correct syntax for SQLServer to SQLBase assuming your LinkedServer is called 'ISLANDLINK' . Note the two dots.
or Go here for the full script and explanation : SQLServer to SQLBase via LinkedServer
or Go here for another example: SQLServer to SQLBase via LinkedServer (more)
--Select:
SELECT * FROM OPENQUERY( ISLANDLINK, 'Select * from SYSADM.BUDGET where DEPT_ID = ''MIS'' ')
--Update:
UPDATE [ISLANDLINK]..[SYSADM].[BUDGET]
SET [BGT_YEAR] = 2016
WHERE DEPT_ID = 'MIS'
GO
--Insert:
INSERT INTO [ISLANDLINK]..[SYSADM].[EMPLOYEE]
([EMPLOYEE_ID]
,[LAST_NAME]) VALUES (99 ,'PLUMAS' )
GO
--Delete:
DELETE FROM [ISLANDLINK]..[SYSADM].[EMPLOYEE]
WHERE [LAST_NAME] = 'PLUMAS'
Related
I'm trying to create historic data for my company; previously i asked how to copy a table with a timestamp into the same mssql database. realistically, it dawned on me that what i really need to do is the following
1) log onto my local mssql box
2) run a query of a view (select * from view_whatever)
3) copy the results of the query and add a timestamp into another database, hosted using azure SQL
if i were doing this from the same database to another table, i'd something like this:
DECLARE #copyDate DATETIME2 = CURRENT_TIMESTAMP
INSERT INTO Hst_Opportunities
SELECT OppName, Oppvalue, #copyDate AS copyDate
FROM dbo.opportunities
the from part is what im struggling with - how do i copy cross server and database when the source database is MSSQL and the target is Azure SQL?
Based on my test, it appears you could create a linked server on your local box and insert the data using four part name:
EXEC master.dbo.sp_addlinkedserver #server = N'SQLAZURE', #srvproduct=N'sqlazure', #provider=N'SQLNCLI', #datasrc=N'NAME.database.windows.net', #catalog=N'TestDb'
EXEC master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'SQLAZURE',#useself=N'False',#locallogin=NULL,#rmtuser=N'USERNAME',#rmtpassword='Password'
GO
DECLARE #copyDate DATETIME = CURRENT_TIMESTAMP;
INSERT INTO [SQLAZURE].[TestDb].[Schema].[Table]
SELECT TOP (10)
[Column1]
,[Column2]
,#copyDate AS copyDate
FROM [Localdb].[Schema].[View];
I have a linked server Remoteserver containing a table that holds file and folder names from a dir
When I am on the remote server I can run a built in procedure (xp_dirtree) and populate the 'files' table but what i need to do is to run a query from the local SQL server that does this:
Delete all records from the [Files] table on Remoteserver
Insert data that comes from the stored procedure:
INSERT [Remoteserver].[dbo].[files] (subdirectory,depth,isfile)
EXEC master.sys.xp_dirtree '\\Fileserver\DBBackup',1,1;
Select the 'subdirectory' column
I tried some things using openquery and i am able to select existing records but unable to do the insert.
Any help is appreciated.
Try this
INSERT INTO OPENQUERY([Remoteserver]
,'SELECT subdirectory, depth, [file] FROM [RemoteDB].[dbo].[files]')
EXEC master.sys.xp_dirtree '\\fileserver\DBBackup', 1, 1;
OR
INSERT INTO OPENQUERY([Remoteserver]
,'SELECT subdirectory,depth, [file] FROM [RemoteDB].[dbo].[files]')
select * from OPENQUERY([another_server_name], 'master.sys.xp_dirtree ''\\fileserver\DBBackup\temp'', 1, 1');
But in general you do not need to use OPENQUERY at all if Fileserver and Remoteserver are accessible from the local machine.
INSERT INTO [Remoteserver].[RemoteDB].[dbo].[files] (subdirectory, depth, isfile)
EXEC master.sys.xp_dirtree '\\Fileserver\DBBackup',1,1;
I found samples where the code:
SELECT * FROM [legacyserver].[database].[schema].[table]
was expressed as:
SELECT * FROM [legacyserver]...[table]
but isn't working for me.
It gives me the error:
An invalid schema or catalog was specified for the provider "MSDASQL" for linked server "legacyserver".
I'm using for legacy server SQL SERVER 2000 and for the new server SQL SERVER 2012.
I tried creating the linked server using:
EXEC sp_addlinkedserver
#server = 'legacyserver',
#srvproduct = '',
#provider = 'MSDASQL',
#provstr = 'DRIVER={SQL Server};SERVER=legacyserver;DATABASE=database;Trusted_Connection=Yes;',
and:
EXEC sp_addlinkedserver
#server = 'legacyserver',
#srvproduct = '',
#provider = 'MSDASQL',
#provstr = 'DRIVER={SQL Server};SERVER=legacyserver;Trusted_Connection=Yes;',
#catalog = 'database';
Is it possible to create the script without hard coding the database name?
I need to create a really big migration script and it need to use a development, acceptance and production databases, and it will be to much work and error prone to change it using replace in the text editor.
UPDATE:
The legacy development, acceptance and production databases are exactly the same (except for the data) and each one have it's own server/instance and database name.
Due to segregation of duties, I can't develop something and deploy it, so I have no way to touch this script after acceptance. I will need to instruct someone else to do so, and if he/she will need to replace every occurrence of [legacyserver].[database], the chances of mistakes are very high.
You can create a synonym
CREATE SYNONYM [table] FOR [legacyserver].[database].[schema].[table]
When you query
SELECT * FROM table
it's actually fetching data from linked server instead of local database.
If want to change database, just drop synonym and create a new one with new database name.
DROP SYNONYM table;
CREATE SYNONYM [table] FOR [legacyserver].[anotherdatabase].[schema].[table]
Your query statement is unchanged.
EDIT: DROP and CREATE SYNONYM statement is a little misleading. You don't need to do it yourself. It's one time job in deployment. Just create a Post-Deployment script that creates all synonyms and parametrize linked server name and database names. Code like:
IF EXISTS (SELECT * FROM sys.synonyms WHERE name = 'table1')
BEGIN
DROP SYNONYM table1
END
EXEC('CREATE SYNONYM table1 FOR ' + '$(LinkedServerName).$(DBName).[dbo].[Table1]')
Note, it use SQLCMD syntax.
Ask operations to change parameter in different environments.
Is it possible to create a table on a linked server?
exec
('CREATE TABLE DatabaseName.dbo.TableName
(
column1 datatype,
column2 datatype,
column3 datatype
)') at [LinkedServer]
The solution from Arpit works fine.
exec
('CREATE TABLE DatabaseName.dbo.TableName
(
column1 datatype,
column2 datatype,
column3 datatype
)') at [LinkedServer];
go
How ever, when you get the error "Msg 7411, Level 16, State 1, Line 1 Server 'MyLinkedServer' is not configured for RPC." you need to change the RPC-parameters on the linked server connection. As default RPC is set to false. It needs to be set to true.
This allows you to run procedures on the linked server.
You must allow this because the solution does not send the "Create table" statement as a SQL command to the linked server. It sends the statement as a string which in turn is executed as a procedure on the remote server.
Hope this helps.
If you're using SQL Server Management Studio and the SQLCMD mode:
-- To enable "SQLCMD mode" in SQL Server Management Studio (SSMS):
-- In the menu toolbar: Query > SQLCMD mode
:setvar LinkedServer "YourLinkedServerNameHere"
:setvar DestinationDb "YourDestinationDatabaseNameHere"
GO
EXEC ('
CREATE TABLE $(DestinationDb).dbo.YourTableNameHere (
[Field1] INT NOT NULL,
[Field2] INT NOT NULL
)
') AT $(LinkedServer)
GO
EXEC ('DROP TABLE $(DestinationDb).dbo.YourTableNameHere') AT $(LinkedServer)
GO
What is the best way to go about doing this? I have found some INSERT statements, but they all seem to work just for inner-database copying.
My rendition:
INSERT INTO [PROGRAM003].[dbo].[faq]
([id], [category], [question], [answer], [tags])
SELECT ([id], [category], [question], [answer], [tags])
FROM [SQL2005_552664_gsow].[dbo].[faq]
I am not very fluent with MSSQL yet, so any and all help is appreciated.
How "remote" are we talking? If it's another database on the same server you can just prepend the database name, as your initial example showed:
INSERT INTO [mytable] SELECT * FROM [database].dbo.[table]
If it's on a completely different server, you need to set them up as linked servers. Then you can do this:
INSERT INTO [mytable] SELECT * FROM [server].[database].dbo.[table]
You must, of course, fully specificy the database and schema within that server as well, and expect performance to suffer because it will need to move data across the network.
You can use OPENDATASOURCE or OPENROWSET to access tables on other servers. You'll need to provide SQL credentials in the query or have both databases setup to allow access to your windows id for this to work. You can also use the import/export wizard in SQL Server Management Studio -- right click on the database and choose Tasks-->Import or Tasks-->Export.
USE master
GO
EXEC sp_addlinkedserver
#server = 'RemoteSer',
#srvproduct = '',
#provider = 'MSDASQL',
#provstr = 'DRIVER={SQL Server};SERVER=000.000.000.000\SQLEXPRESS;'
GO
SELECT * INTO bnkWeb.dbo.test1 FROM [RemoteSer].[bnk].[dbo].[test1]
You might want to look into DTS or (2005+) Integration Services. Both of these will take data from one DB (Table) and copy the data into another DB (Table).
Link to Integration Services: http://msdn.microsoft.com/en-us/library/ms141091.aspx