We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Perform an insert on a linked server

Medium Priority
822 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.
Comment
Watch Question

Raja Jegan RSQL Server DBA & Architect, EE Solution Guide
CERTIFIED EXPERT
Awarded 2009
Distinguished Expert 2019

Commented:
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.
Mark WillsTopic Advisor, Page Editor
CERTIFIED EXPERT
Distinguished Expert 2018

Commented:
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

Author

Commented:
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.

Author

Commented:
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.
Mark WillsTopic Advisor, Page Editor
CERTIFIED EXPERT
Distinguished Expert 2018

Commented:
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.
Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
did you actually use something like this:

SET ROWCOUNT 1

in your session?

try this:
SET ROWCOUNT 0

and then rerun the queries...

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
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.
Mark WillsTopic Advisor, Page Editor
CERTIFIED EXPERT
Distinguished Expert 2018

Commented:
Set rowcount !?!?!?  would never have guessed that one - well done AngelIII - will have to remeber that one for the future...
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.