I have SQL Server 2014 and was trying to find out if there's a way to bulk insert data into a table on a remote server like below.
SELECT * FROM OPENQUERY([REMOTESERVER],
'BULK INSERT [DBNAME].[dbo].[demo] FROM ''\\Share\data\demo.dat''
WITH (DATAFILETYPE = ''widenative'')');
Related
I am trying to operate a simple INSERT command from my mssql host database into my oracle's linked server destination database.
Our environment is a mssql 2016 server on hostside, oracle 19c linked server database and linked server connection established.
I am trying to INSERT data into my orcle table:
X_PARTNO_BARC, structure as shown:
My Insert statement:
SELECT
CAST(A.ARANUMMER as varchar(20)) as PARTNO,
CAST(BC.BCCODE as varchar(40)) as EANNUM,
CAST('SQL' as varchar(20)) as CRTUSR,
CAST(format(getdate(),'yyyyMMddhhmmss') as varchar(14)) as CRTDTM
INTO #tmptest
FROM BARCODES BC WITH (NOLOCK)
INNER JOIN AEL A WITH (NOLOCK) ON A.ARIDNR = BC.BCIDNR
INSERT INTO OPENQUERY (DC21WMS,'SELECT * FROM X_PARTNO_BARC')
--(partno,eannum,crtusr,crtdtm)
SELECT * FROM #tmptest
My desired output is:
But then it adds these strange characters to my destination columns
What am i doing wrong?
i played a little bit with the column length on host side. eg. using varchar(max) instead. It changes the output but to other strings
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'
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;
Scenario: get trigger-generated primary key when calling INSERT INTO from SQL Server linked server to Oracle
Given
Oracle 11g table with columns PRIMARY_KEY_ID, FIELD1, FIELD2, CREATE_DATE. Table has "BEFORE INSERT" trigger that selects NEXTVAL from a sequence into PRIMARY_KEY_ID field.
SQL Server 2008 R2 with Linked Server to the Oracle database containing the table above.
When I insert a record into the Oracle table, then I want to retrieve the trigger-generated primary key.
How do I do this?
Make sure these properties are set on the SQL Server linked server:
RPC=True
RPC Out=True
Execute this code in SQL Server:
DECLARE #Field1 NVARCHAR(42);
DECLARE #Field2 NVARCHAR(42);
DECLARE #PrimaryKeyValue INT;
EXECUTE (
'begin INSERT INTO MYSCHEMA.MYTABLE (
FIELD1
,FIELD2
,CREATE_DATE
)
VALUES (
?
,?
,sysdate
) RETURNING PRIMARY_KEY_ID INTO :PrimaryKeyValue; end;'
,#Field1
,#Field2
,#PrimaryKeyValue OUTPUT
) at oracle_linked_server;
Notes
begin and end; are required in the statement.
The #PrimaryKeyValue variable declared in SQL Server is the same as the :PrimaryKeyValue output parameter; Oracle uses a colon prefix for parameters.
See Calling Oracle stored procedure with output parameter from SQL Server, which provided the inspiration for this answer.