Insert into table.. exec at linked server does not work - sql

This works, returning a resultset :
exec ('select ''col'', count(1) from test.dbo.[Table1] with (nolock)') at svrA
When I try to insert the resultset into a table:
insert into rowcount_sub (tablename,rowcnt)
exec ('select ''col'', count(1) from test.dbo.[Table1] with (nolock)') at svrA
Fails giving this error:
OLE DB provider "SQLNCLI10" for linked server "svrA" returned message "No transaction is active.".
Msg 7391, Level 16, State 2, Line 1
The operation could not be performed because OLE DB provider "SQLNCLI10" for linked server "svrA" was unable to begin a distributed transaction.

If you do not use distributed transaction on purpose, You can use advanced properties of the linked server object on main server to disable promotion of distributed transaction.

I was able to solve the same problem by using OPENQUERY instead of EXEC:
insert into rowcount_sub (tablename,rowcnt)
SELECT * FROM OPENQUERY(svrA, 'select ''col'', count(1) from test.dbo.[Table1] with (nolock)')
Hope it helps someone...

The operation could not be performed
because OLE DB provider "SQLNCLI10"
for linked server "svrA" was unable to
begin a distributed transaction.
The message is pretty clear and quite explicit. All you have to do is open your system documentation and follow the steps on configuring distributed transactions: Configuring MS DTC Services.
There are also plenty of blogs and tutorials out there:
How to configure DTC on Windows 2003
How to configure DTC on Windows 2008

Changing "Enable Promotion of Distributed Transaction" from True to false fixed my issue.

Related

Transaction (Process ID) was deadlocked: Sql Server Management Studio

I'm trying to delete a database (from Sql Server Management Studio) and I receive this error:
Transaction (Process ID 52) was deadlocked on lock resources with
another process and has been chosen as the deadlock victim. Rerun the
transaction. (.Net SqlClient Data Provider)
I googled a lot (with a lot of result also here in so) but I cannot find any solution.
I really need to delete (and restore this db), but I'm stucked.
Any suggestion?
I guess you have open transactions. For example, for the database StackOverflow I am opening a transaction like this:
USE StackOverflow
BEGIN TRAN
and
USE StackOverflow
exec sp_whoisactive
So, when I try to deleted it:
I get the following error:
An exception occurred while executing a Transact-SQL statement or
batch. (Microsoft.SqlServer.ConnectionInfo)
Cannot drop database "StackOverflow" because it is currently in use.
(Microsoft SQL Server, Error: 3702)
If I choose close existing connections from the delete interface the database is successfully deleted.

SQL Server 2005 DB2 Linked Server Error When Inserting Result Set into a Temp Table

We have a linked Server against DB2 on SQL Server 2005.
When we tried putting a simple query result set from the linked sever into a temp table return this error.
OLE DB provider "MSDASQL" for linked server "SOMEDSN" returned message "[IBM][CLI Driver] SQL0998N Error occurred during transaction or heuristic processing. Reason Code = "16". Subcode = "2-8004D026". SQLSTATE=58005
".
Msg 7391, Level 16, State 2, Line 21
The operation could not be performed because OLE DB provider "MSDASQL" for linked server "SOMEDSN" was unable to begin a distributed transaction.
DECLARE #FilterCode varchar(20);
Set #FilterCode = '11122';
Create Table #TmpTable (
Id Int
)
Insert Into #TmpTable
EXEC ('Select A.Id From SCM1.DB2TBL1 A Where A.FilterCode = ?', #FilterCode) At SOMEDSN;
The Exec works just fine with correct result set displayed, but as soon as we tried the result set into the temp table, then we get the error.
I searched here and many other places and tried the followings.
sp_configure 'remote proc trans', 0
On Linked Server Properties > Server Options set "Enable Promotion of Distributed Transactions" to false. I did not see this option.
On Linked Server Properties > RPC, RPC out and distributor are set to value TRUE
Used Openquery, but it cannot use dynamic SQL or pass parameters.
The table contains very many rows. We need to execute the query on the DB2 side to reduce the result set based on the parameter.
Any help would be greatly appropriated!
Have you started the MSDTC service (DTC)?
Under Windows 2008:
From Administrative tools->Component services->Computers->My
computer: Local DTC. Right click Properties, go to the MSDTC tab and
select "Security Configuration".
Enable checkbox for "Network dtc access" and "Allow outbound".
Enable checkbox for "No Authentication Required"
If that does not work, try disabling connection pooling.

The operation could not be performed because OLE DB provider "SQLNCLI10" for linked server "DB_PROD_04" was unable to begin a distributed transaction

I have two servers:
SQL_UAT_01
DB_PROD_04
Both of these servers have the same database name and same tables:
SQL_UAT_01.Database_01.TestTable
DB_PROD_04.Database_01.TestTable
There is a trigger on each of these two tables.
When the trigger fires, it does a simple UPDATE on the table of the OTHER server.
Example
Trigger is fired here:
SQL_UAT_01.Database_01.Test
It does an update here:
DB_PROD_04.Database_01.Test
And vice-versa.
I'm running into an error which I have no clue how to fix.
Again, the error is this:
CallableStatementCallback; bad SQL grammar [{call
spGetAndIncrementIndex(?)}]; nested exception is
com.microsoft.sqlserver.jdbc.SQLServerException: The operation could
not be performed because OLE DB provider "SQLNCLI10" for linked server
"DB_PROD_04" was unable to begin a distributed transaction.
I have already linked the servers on both sides.
Does anyone have ANY idea how I should go about fixing this?
The answer was to install the DT on the server. It's a windows feature. I installed it and wahllah!
If a server runs out of local ports, you also get MSDTC issues
This is also fixed by correcting ephemeral TCP port saturation:
http://msdn.microsoft.com/en-us/library/aa560610%28v=bts.20%29.aspx

SQL-Server trigger update issue from over linked server

I would like to update rows over linked server. I created linked server on my local SQL-Server with this instruction.
I can update rows over Query Analyzer with below code. No error returned and rows update on Linked Server successfully.
UPDATE [LinkedServer].[Database].[dbo].[Table]
SET A = 'A', B = 'B'
WHERE id = 1
But when I created a update trigger to my table on my local sql server like below,
ALTER TRIGGER [TriggerLocalServerTable]
ON dbo.[LocalServerTable]
FOR UPDATE
AS
DECLARE #A varchar(4)
DECLARE #B varchar(4)
DECLARE #id int
BEGIN
SELECT
#A = A,
#B = B,
#id = id
FROM inserted
UPDATE [LinkedServer].[Database].[dbo].[Table]
SET A = #A, B = #B
WHERE id = #id
END
When trigger performed, returns this error like below,
OLE DB provider "SQLNCLI" for linked
server "LinkedServer" returned message
"The partner transaction manager has
disabled its support for
remote/network transactions.". Msg
7391, Level 16, State 2, Procedure
TriggerLocalServerTable, Line 45
The operation could not be performed
because OLE DB provider "SQLNCLI" for
linked server "LinkedServer" was unable
to begin a distributed transaction.
I applied this instruction to local server for solving issue, anything changed.
More details;
if defined my linked server with its
remote ip address.
My local server and Linked Server
not in same domain.
Linked Server Security setting is "Be
made using this security context"
Local server version is Windows
Server 2003 SP2, SQL-Server version is 2005
standart.
Linked server OS version is 2008 and
SQL-Server version 2008 (64 bit).
I couldn't access remote server
with RDP. i can access just from sql
port (1433).
i set linked server "RPC" and "RPC
Out" to "True" from "Security Option".
Do you have any idea to solve this issue.
Thank you very much already now.
Edit:
I solved this issue. Firstly i created a trigger for update on [TriggerLocalServerTable]. This trigger's job is inserting new update fields to a local table. This new table using for updated temp datas. Then i created a job that runs every hour for update fileds on LinkedServer. This job gets data from temp table then update to LinkedServer table's fields.
Regards, Kerberos.
Do you have a Distributed Transaction Coordinator installed on Windows Server? If your update is inside a transaction, it won't work without it.
See
link text
The problem dont lies with SQL Server it is with ODBC Driver.
Go to Start > Settings > Control Panel > Administrative Tools > Data Sources
Open System DSN Tab
Select the driver you have used for Linked Server and click configure
Select Advanced > Flags3 and Uncheck Disable transaction check box
Problem Solved :)

SQL Server: Snapshot transaction problem with synonyms in Express Edition

We have 2 databases, say DB1 and DB2.
DB1 contains all the stored procedures which access also data in DB2.
DB1 uses synonyms to access the tables in DB2.
(Using synonyms is a requirement in our situation)
This works perfectly fine in all situations with SQL Server 2005 Developer Edition.
However in the Express Edition, we get an exception when we do the following:
1 Restart SQL Server
2 Execute the following code within DB1:
set transaction isolation level snapshot
begin transaction
declare #sQuery varchar(max)
set #sQuery = 'Select * from synToSomeTableInDB2'
exec (#sQuery)
commit transaction
This will result in the following error:
Snapshot isolation transaction failed in database '...' because the database was not recovered when the current transaction was started. Retry the transaction after the database has recovered.
The same select query passes fine when used without the EXEC or when run on the Developer Edition.
Restarting the server in step 1 is important as once a connection was made to DB2, the code runs also fine on SQL Server Express Edition.
Does anyone have an idea what this is? We need to be able to use EXEC for some dynamic queries.
We've already checked MSDN, searched Google, ...
Any help is greatly appreciated.
--- Edit: March 10 09
As discussed with Ed Harper below, I've filed a bug report for this.
See https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=422150
As found out via Microsoft Connect, the problem is that by default on SQL Server Express Edition the AUTO_CLOSE option is set on true.
Changing this option to false fixes the problem.
The error message suggests that the query fails because SQL server is still recovering the database following the service restart when you execute your query.
Does the error always occur on the first attempt to run this code, regardless of the time elapsed since the service was restarted?
Can you confirm from the SQL Server log that the database is recovering correctly after the restart?