Failing to delete record from linked server - sql-server-2005

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'

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

Active Directory query from SQL Server 2012 trigger failing

I have an OpenQuery call to Active Directory that gets the email address for a given Windows Domain account name. It works fine until I put it in an Insert trigger. The offending piece of T-SQL is:
set #sql = 'select mail from openquery(ADSI, '''
+ 'SELECT mail FROM ''''LDAP://DC=COMPANY,DC=LOCAL''''
WHERE objectClass=''''user'''' AND objectClass<>''''computer''''
AND samAccountName=''''username'''''') '
exec sp_executesql #sql, N'#recipients varchar(500) output', #recipients output
ADSI is a linked server and I have the security context set to always use a specific user account. I get the following error returned in my web app when adding a new record.
"The member with identity 'mail' does not exist in the metadata collection.
Parameter name: identity"
I have tried connecting as the same user that my app uses for connecting to the database and running the query from SQL Management Studio and I cannot replicate the problem outside of the Trigger. Can someone point me in the right direction? I suspect it is a security issue but I am lost and running out of ideas.

Executing sys.xp_readerrorlog with OPENROWSET

The following runs successfully:
EXEC sys.xp_readerrorlog 0
This however does not:
SELECT *
FROM OPENROWSET(
'SQLNCLI',
'Server=.;Trusted_Connection=Yes;',
'EXEC sys.xp_readerrorlog 0')
I am logged in using my Windows account and I have sysadmin permissions on all databases on my local server, including the system databases.
Here is the error message:
Msg 7357, Level 16, State 2, Line 11
Cannot process the object "EXEC sys.xp_readerrorlog 0". The OLE DB provider "SQLNCLI10" for linked
server "(null)" indicates that either the object has no columns or the
current user does not have permissions on that object.
I've checked the folder permissions on C:\Windows\ServiceProfiles\LocalService\AppData just in case that it was an issue with OPENROWSET creating a temp file but local administrators (I'm in the group) have full control.
Why am I getting this error and how can I get around it?
Try this:
SELECT *
FROM OPENROWSET(
'SQLNCLI',
'Server=.;Trusted_Connection=Yes;',
'SET FMTONLY OFF;EXEC sys.xp_readerrorlog 0')
Basically, it's trying to infer the shape of the result before running the procedure.
Using that set option means the only way this will be possible is by executing the procedure.

Error using distributed transaction in SQL Server 2008 R2

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';

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.