[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Perform an insert on a linked server

Posted on 2009-02-14
8
Medium Priority
?
803 Views
Last Modified: 2013-12-18
Hi all,

I have created a linked server between SQL Server 2005 and Oracle 10g using an odbc connection:

--Create linked server
EXEC sp_addlinkedserver
@server = 'PerformOEE_OLink',
@srvproduct = '',
@provider = 'MSDASQL',
@datasrc = 'Oracle_10g_Test'
GO

I have created a login so I can acces the linked server:

--Create linked server login
EXEC master.dbo.sp_addlinkedsrvlogin
@rmtsrvname=N'PerformOEE_OLink',
@useself=N'False',
@locallogin=NULL,
@rmtuser=N'sysman',
@rmtpassword='mypass1'
GO

I can query the linked server without any problems:

--Query on linked server
SELECT * FROM OPENQUERY(PerformOEE_OLink,'SELECT * FROM ud_downtime')

But when I try to insert a new record, I get a message saying 1 rows affected, but when I requery the linked server table, I only see what was already there and no new record:

--Insert on linked server
INSERT OPENQUERY (PerformOEE_OLink, 'SELECT downstamp,upstamp,tester,downcode,downcount FROM ud_downtime')
VALUES ('14-FEB-2009 18:00:00','14-FEB-2009 18:02:00',5,6,8)

I have also tried the following:

--Insert on linked server
INSERT OPENQUERY (PerformOEE_OLink, 'SELECT downstamp,upstamp,tester,downcode,downcount FROM ud_downtime WHERE 1 = 0')
VALUES ('14-FEB-2009 18:00:00','14-FEB-2009 18:02:00',5,6,8)

Is the problem that I need to execute a commit after the insert statement, If so, how do I do this?

Any help is greatly appreciated.

Thanks.
0
Comment
Question by:resourcesys
8 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 23641662
In oracle, you have to issue the Commit statement to commit any kind of transactions.
So for the best practice, have your insert statement within a transaction and commit it once it is successful.
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 23641969
Since it is a linked server, why not try the four part identifier ?

begin transaction
INSERT PerformOEE_OLink...ud_downtime ( downstamp,upstamp,tester,downcode,downcount) VALUES ('14-FEB-2009 18:00:00','14-FEB-2009 18:02:00',5,6,8)
commit transaction
0
 

Author Comment

by:resourcesys
ID: 23644603
I have tried the following two statements and got errors for them both:

begin transaction
INSERT OPENQUERY (PerformOEE_OLink, 'SELECT downstamp,upstamp,tester,downcode,downcount FROM ud_downtime')
VALUES ('14-FEB-2009 18:00:00','14-FEB-2009 18:02:00',5,6,8)
commit transaction

Msg 8501, Level 16, State 3, Line 4
MSDTC on server 'CEDAR\SQLEXPRESS' is unavailable.
====================================================================================
begin transaction
INSERT PerformOEE_OLink...ud_downtime ( downstamp,upstamp,tester,downcode,downcount) VALUES ('14-FEB-2009 18:00:00','14-FEB-2009 18:02:00',5,6,8)
commit transaction

The OLE DB provider "MSDASQL" for linked server "PerformOEE_OLink" does not contain the table "ud_downtime". The table either does not exist or the current user does not have permissions on that table.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:resourcesys
ID: 23644683
I have done some chaecking and the following statement is inserting records:

INSERT OPENQUERY (PerformOEE_OLink, 'SELECT downstamp,upstamp,tester,downcode,downcount FROM ud_downtime')
VALUES ('14-FEB-2009 18:00:00','14-FEB-2009 18:02:00',5,6,8)

The problem is, the following statement is only getting the top record:

SELECT * FROM OPENQUERY(PerformOEE_OLink,'SELECT * FROM ud_downtime')

Is there a way to get all records back?

Thanks.
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 23645440
You do need the correct database name etc in that four part identifier... Have a quick look at : http://www.databasejournal.com/features/mssql/article.php/10894_3290801_2/Linked-Severs-on-MS-SQL-Part-4-Oracle.htm   It uses Oracle 9, but same things apply.

Now being a linked server, performance is going to be pretty ordinary, some of which can depend on your driver, and how it is setup e.g. need to check "Allow Inprocess" in the provider options.   The first page of that link above goes through setting up Oracle Client and might be worth quickly checking your version and the steps that the article took in case there were one or two little hidden gems.

Also, have a look at : http://forums.oracle.com/forums/thread.jspa?messageID=2507161 mentions something about an upgrade and a patch, and the use of the four part identifier. Also, have you seen : http://support.microsoft.com/kb/280106 

Seems you are very close, normally the issues are establishing the link more so than a data refresh problem... Will keep looking around and see if I can find anything.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 23646567
did you actually use something like this:

SET ROWCOUNT 1

in your session?

try this:
SET ROWCOUNT 0

and then rerun the queries...
0
 

Author Comment

by:resourcesys
ID: 23648059
Hi,

SET ROWCOUNT 0

This seems to work so thanks for the assistance and thanks to everybody who looked at and left a comment.

Thanks.
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 23648257
Set rowcount !?!?!?  would never have guessed that one - well done AngelIII - will have to remeber that one for the future...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question