Error using distributed transaction in SQL Server 2008 R2 - sql

I am using SQL Server 2008 R2.
I am getting the following error when I try to execute a SP from java side. The same SP is running well when I use query browser to run.
I have tried using hot fix also but it is not working.
ERROR :: The operation could not be performed because OLE DB provider "SQLNCLI10" for linked server "server name" was unable to begin a distributed transaction.
when executed in transaction

Inside SQL Server Management Studio, expand Server Objects, then Linked Server, then right click on the linked server in question and choose 'Properties.' Select the 'Server Options' page, and make sure 'Enable Promotion of Distributed Transactions' is set to 'False'
Or you can do it with T-SQL:
USE master;
EXEC sp_serveroption '<<your linked server name>>', 'remote proc transaction promotion', 'false';

Related

How to insert data from prod server to dev server in SQL Server

I would like to insert data from prod server to dev server for a particular table.
I am using insert into SQL query and fully qualified name. That is I am specifying server name, databsename, schema name and table name.
insert into ServerADev.[ING_DB].dbo.[Table1]
select *
from ServerAProd.[ING_DB].dbo.[Table1]
where ID = '08914'
ID is the column in Table1.
For above query I am getting an error:
Cannot find ServerAProd in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.
When I EXEC sp_addlinkedserver #server='ServerAProd', I am getting:
User does not have permission to perform this action.
Do I need to make a request to DBA (database admin) to grant permission to perform this query?
You need to set up a linked server to query a foreign server.
So, either:
ServerAProd is not what you named the linked server
or
You didn't create a linked server yet. You can use the sp_addlinkedserver from the error message, or browse to "server objects" in the object explorer then right-click -> new on "Linked Servers". See the link above for more details.
For your edit... yes this requires permissions:
When using Transact-SQL statements, requires ALTER ANY LINKED SERVER permission on the server or membership in the setupadmin fixed server role. When using Management Studio requires CONTROL SERVER permission or membership in the sysadmin fixed server role.
1. create linked server for the dev server on production server,
2. use this openquery() to insert data, so that you can insert large data very quickly.
INSERT OPENQUERY (devservername, 'SELECT * FROM devservar_database..dev_server_table_name')
select * from production_table_database_name..production_table_table_name

I need to add a linked server to a MS Azure SQL Server

I have tried and tried, and can not get linked. I can connect to the server using SSMS, but can not link to it from a local server. Here is my script (replacing things in brackets with pertainent information):
EXEC master.dbo.sp_addlinkedserver
#server = N'[servername].database.windows.net',
#srvproduct = N'Any',
#provider = N'MSDASQL',
#datasrc = N'Azure_ODBC1'
GO
EXEC master.dbo.sp_addlinkedsrvlogin
#rmtsrvname = N'[servername]',
#useself = N'False',
#locallogin = NULL,
#rmtuser = N'[username]',
#rmtpassword = '[password]'
GO
As specified in ckarst second link, there is a solution that works. I am posting it here to save you the trouble to search for it. As suggested by JuanPableJofre in this page Azure feedback :
Using SQL 2014, I was able to do a distributed query between a local SQL server and a SQL Azure.
First, I created a Linked-Server:
Linked Server (name): LinkedServerName
Provider: Microsoft OLE DB Provider for SQL Server
Product name: (blank)
Data Source: azure-db.database.windows.net
Provider string: (blank)
Location: (blank)
Catalog: db-name
In security options: (*)
Be made using this security context
Remote login: azure-user-name
With password: yourPassword
In SSMS entered the following test query:
use [Local_DB]
go
Select *
from [LinkedServerName].[RemoteDB].[dbo].[Remote_Table]
It worked beautifully !!
To summarize, the linked server is created on your local database. The catalog (database name) is important as Azure might not let you specify it in a query (ie: use azureDBName will not work on Azure), so the database name has to be in the catalog.
Unfortunately, Linked Server is not supported by SQL Azure DB.
https://msdn.microsoft.com/en-us/library/azure/ee336281.aspx
However, as you can see from the forum link below, Microsoft is aware of the scenario and the customer feedback has been heard. http://feedback.azure.com/forums/217321-sql-database/suggestions/402636-cross-database-reference
While Azure DB doesn't support defining Linked Servers, you can use the recently previewed Elastic Query feature to define an "External Data Source" that is just another Azure DB database, and define "External Tables" that are references to tables in that external database. Then you can query these as though they are local objects. This is very similar to the Linked Server concept and is described in detail here.
-- Make a link to the cloud
EXEC sp_addlinkedserver
#server='[servername].database.windows.net', -- specify the name of the linked server
#srvproduct=N'Azure SQL Db',
#provider=N'SQLNCLI',
#datasrc='yourservername', -- add here your server name
#catalog='FCS';
GO
--Set up login mapping
EXEC sp_addlinkedsrvlogin
#rmtsrvname = '[servername].database.windows.net',
#useself = 'FALSE',
#locallogin=NULL,
#rmtuser = 'username',
#rmtpassword = 'password'
GO
This does create a linked server in my envirnoment, however it doesn't connect to the catalog that I have specified (FCS). It connects to a default for some reason. Is there something i am doing wrong

Failing to delete record from linked server

I've spent the past two days searching everywhere for a solution to my problem but without any luck.
I have this query that deletes record from a remote server:
delete from OPENROWSET('SQLNCLI', 'Server=AB01S\SQLEXPRESS;Database=ShopData;Trusted_Connection=yes', 'Select receipt_n,action_in, action_ty, action_field_name,action_field_type,action_field_data, terminal from tblData where receipt_n= 1 and terminal = 1');
and I am getting this error:
OLE DB provider "SQLNCLI" for linked server "(null)" returned message "Multiple-step OLE DB operation generated errors. Check each OLE DB status value, if available. No work was done.".
Msg 7202, Level 11, State 1
It also suggests to use sp_addlinkedserver to add a linked server.
Some notes:
This happens only on this specific computer + remote server. On 2 different stations (Computer+Server) it worked just fine.
Insert to OpenRowSet is working OK.
Select * from OpenRowSet is working OK.
=> Changing the delete to select * works OK.
Ad hok is enabled on the computer that queries
Remote connection is enabled on the computer being queried via openrowset
I can ping the server (AB01S)
So far I have tried adding linked setup via GUI and sp_addlinkserver.
I found the problem!!
The server name (for some reason) was incorrect.
##servername returned AB01S_88
The solution was:
running the following code and then restarting the service:
exec sp_dropserver ##servername
exec sp_addserver 'AB01S', local
exec sp_serveroption 'AB01S', 'data access' , 'true'

How to join a database user to the db_owner role using a T-SQL script?

I use SQL SERVER 2008 R2 Express.
I'm on a NEW project with a database named myDatabase with the db owner username myUsernamae.
I would like to use ALTER ROLE instead of sp_addrolemember due to this message from Microsoft regarding sp_addrolemember (Transact-SQL):
Important This feature will be removed in a future version of
Microsoft SQL Server. Avoid using this feature in new development
work, and plan to modify applications that currently use this feature.
Use ALTER ROLE instead.
I tried
USE [myDatabase]
GO
ALTER ROLE [db_owner] ADD MEMBER [myUsername]
GO
but I keep getting this error message:
Incorrect syntax near the keyword 'ADD'.
What is the correct syntax?
ALTER ROLE is new to SQL Server 2012, while you are using SQL Server 2008 R2.
I wouldn't worry to much about sp_addrolemember. Scripts generated by 2008R2 Management Studio use this procedure, so it should be a while before they really phase it out.

Using T-Sql, how can I insert from one table on a remote server into another table on my local server?

Given the remote server 'Production' (currently accessible via an IP) and the local database 'Development', how can I run an INSERT into 'Development' from 'Production' using T-SQL?
I'm using MS SQL 2005 and the table structures are a lot different between the two databases hence the need for me to manually write some migration scripts.
UPDATE:
T-SQL really isn't my bag. I've tried the following (not knowing what I'm doing):
EXEC sp_addlinkedserver
#server = N'20.0.0.1\SQLEXPRESS',
#srvproduct=N'SQL Server' ;
GO
EXEC sp_addlinkedsrvlogin '20.0.0.1\SQLEXPRESS', 'false',
'Domain\Administrator', 'sa', 'saPassword'
SELECT * FROM [20.0.0.1\SQLEXPRESS].[DatabaseName].[dbo].[Table]
And I get the error:
Login failed for user ''. The user is
not associated with a trusted SQL
Server connection.
create a linked server and then use 4 part notation
insert table
select <column names>
from LinkedserverName.DatabaseName.SchemaName.TableName
you can also use OPENROWSET
example
insert table
SELECT a.*
FROM OPENROWSET('SQLNCLI', 'Server=Seattle1;Trusted_Connection=yes;',
'SELECT GroupName, Name, DepartmentID
FROM AdventureWorks2008R2.HumanResources.Department
ORDER BY GroupName, Name') AS a;
try this to create the login
EXEC master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'20.0.0.1\SQLEXPRESS',
#useself=N'False',
#locallogin=NULL,
#rmtuser=N'sa',
#rmtpassword='saPassword'
You can define the PROD Server as Linked Server to the DEV box and then access it.
However I think it would be easier to get a backup from PROD Box and Restore it to DEV or use SSIS for Schema Import.
Look into the RedGate tools, esp. SQL Data Compare. If that's not an option you should look at OPENDATASOURCE or OPENROWSET to access the remote database.
Well you can use a linked server and then use the 4 part names for objects (See BOL for how to set up a linked server)
Or you could use SSIS to set up the data migrations and connect to the remote server
Or you could use OPENROWSET
I'd probably use SSIS, but I'm already familiar with it.
Use SSMS. Right click on the target DB and select "Tasks", "Import Data". You will be able to preview the data and make conversions visually. Save the package in SSIS or run it now.