Timeout is not working for a linked server - sql-server

I have a job that executing some query on linked server via openquery. Linked server running MySQL. I dont know why but sometimes my job gets lagged and my query still idling for all the time.
When I was created linked server I've used this:
EXEC master.dbo.sp_addlinkedserver #server = #linkedServerName, #srvproduct=#linkedServerName, #provider=N'MSDASQL', #datasrc=#linkedServerName, #catalog=N'DictionariesALL'
EXEC master.dbo.sp_addlinkedsrvlogin #rmtsrvname=#linkedServerName,#useself=N'True',#locallogin=NULL,#rmtuser=NULL,#rmtpassword=NULL
EXEC master.dbo.sp_serveroption #server=#linkedServerName, #optname=N'collation compatible', #optvalue=N'false'
EXEC master.dbo.sp_serveroption #server=#linkedServerName, #optname=N'data access', #optvalue=N'true'
EXEC master.dbo.sp_serveroption #server=#linkedServerName, #optname=N'dist', #optvalue=N'false'
EXEC master.dbo.sp_serveroption #server=#linkedServerName, #optname=N'pub', #optvalue=N'false'
EXEC master.dbo.sp_serveroption #server=#linkedServerName, #optname=N'rpc', #optvalue=N'false'
EXEC master.dbo.sp_serveroption #server=#linkedServerName, #optname=N'rpc out', #optvalue=N'false'
EXEC master.dbo.sp_serveroption #server=#linkedServerName, #optname=N'sub', #optvalue=N'false'
EXEC master.dbo.sp_serveroption #server=#linkedServerName, #optname=N'connect timeout', #optvalue=N'60'
EXEC master.dbo.sp_serveroption #server=#linkedServerName, #optname=N'collation name', #optvalue=null
EXEC master.dbo.sp_serveroption #server=#linkedServerName, #optname=N'lazy schema validation', #optvalue=N'false'
EXEC master.dbo.sp_serveroption #server=#linkedServerName, #optname=N'query timeout', #optvalue=N'60'
EXEC master.dbo.sp_serveroption #server=#linkedServerName, #optname=N'use remote collation', #optvalue=N'true'
EXEC master.dbo.sp_serveroption #server=#linkedServerName, #optname=N'remote proc transaction promotion', #optvalue=N'true'`
It shows how I added timeout states but it isn't working somenimes. How can I fix it?

Related

How to consume Active Directory data from Power BI?

I am using the Active Directory connector:
I am consuming 2 columns:
But after hours it keeps loading and never finishes:
Any ideas on how to consume the data properly?
you can try query maybe something like...
First thing we'll do is create our linked server, Active Directory Service Interface also known as ASDI, to Active Directory using the code below:
Make sure you change the #rmtuser and #rmtpassword variables to a login and password that has access to your Active Directory.
USE [master]
GO
EXEC master.dbo.sp_addlinkedserver #server = N'ADSI', #srvproduct=N'Active Directory Service Interfaces', #provider=N'ADSDSOObject', #datasrc=N'adsdatasource'
EXEC master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'ADSI',#useself=N'False',#locallogin=NULL,#rmtuser=N'DOMAIN\USER',#rmtpassword='*********'
GO
EXEC master.dbo.sp_serveroption #server=N'ADSI', #optname=N'collation compatible', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'ADSI', #optname=N'data access', #optvalue=N'true'
GO
EXEC master.dbo.sp_serveroption #server=N'ADSI', #optname=N'dist', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'ADSI', #optname=N'pub', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'ADSI', #optname=N'rpc', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'ADSI', #optname=N'rpc out', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'ADSI', #optname=N'sub', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'ADSI', #optname=N'connect timeout', #optvalue=N'0'
GO
EXEC master.dbo.sp_serveroption #server=N'ADSI', #optname=N'collation name', #optvalue=null
GO
EXEC master.dbo.sp_serveroption #server=N'ADSI', #optname=N'lazy schema validation', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'ADSI', #optname=N'query timeout', #optvalue=N'0'
GO
EXEC master.dbo.sp_serveroption #server=N'ADSI', #optname=N'use remote collation', #optvalue=N'true'
GO
EXEC master.dbo.sp_serveroption #server=N'ADSI', #optname=N'remote proc transaction promotion', #optvalue=N'true'
GO
Once the linked server is created we can now setup our query to return the information we need.
SELECT * FROM OpenQuery
(
ADSI,
'SELECT streetaddress, pager, company, title, displayName, telephoneNumber, sAMAccountName,
mail, mobile, facsimileTelephoneNumber, department, physicalDeliveryOfficeName, givenname
FROM ''LDAP://DOMAIN.com/OU=Players,DC=DOMAIN,DC=com''
WHERE objectClass = ''User''
') AS tblADSI
ORDER BY displayname

Trying to create a new linked server from the script of an available linked sever in another server. The connection test fails on some servers

This is the linked server script generated by the sql server itself. I used this script to create the same linked server on other servers. On some of them when I execute the connection test, it gives me "microsoft sql server, error: 18456" as if I have entered the remote login and password part wrong but I'm sure their correct for the 192.168.0.72 server and also the connection test succeeds on some other servers with the same credentials when I test it. Below is my generated script from the original linked server:
USE [master]
GO
/****** Object: LinkedServer [TEST] Script Date: 8/22/2020 3:42:31 PM ******/
EXEC master.dbo.sp_addlinkedserver #server = N'TEST', #srvproduct=N'192.168.0.72', #provider=N'SQLNCLI', #datasrc=N'.'
/* For security reasons the linked server remote logins password is changed with ######## */
EXEC master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'TEST',#useself=N'False',#locallogin=NULL,#rmtuser=N'sa',#rmtpassword='########'
GO
EXEC master.dbo.sp_serveroption #server=N'TEST', #optname=N'collation compatible', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'TEST', #optname=N'data access', #optvalue=N'true'
GO
EXEC master.dbo.sp_serveroption #server=N'TEST', #optname=N'dist', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'TEST', #optname=N'pub', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'TEST', #optname=N'rpc', #optvalue=N'true'
GO
EXEC master.dbo.sp_serveroption #server=N'TEST', #optname=N'rpc out', #optvalue=N'true'
GO
EXEC master.dbo.sp_serveroption #server=N'TEST', #optname=N'sub', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'TEST', #optname=N'connect timeout', #optvalue=N'0'
GO
EXEC master.dbo.sp_serveroption #server=N'TEST', #optname=N'collation name', #optvalue=null
GO
EXEC master.dbo.sp_serveroption #server=N'TEST', #optname=N'lazy schema validation', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'TEST', #optname=N'query timeout', #optvalue=N'0'
GO
EXEC master.dbo.sp_serveroption #server=N'TEST', #optname=N'use remote collation', #optvalue=N'true'
GO
EXEC master.dbo.sp_serveroption #server=N'TEST', #optname=N'remote proc transaction promotion', #optvalue=N'true'
GO

Linked Server - Can't access user databases

I have 2 SQL Server 2016 instances, both patched at level SP1 CU8. I have created a linked server between them, with the following definition:
USE [master]
GO
/****** Object: LinkedServer [MyServer] Script Date: 4/27/2018 12:58:24 PM ******/
EXEC master.dbo.sp_addlinkedserver #server = N'MyServer', #srvproduct=N'', #provider=N'SQLNCLI'
/* For security reasons the linked server remote logins password is changed with ######## */
EXEC master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'MyServer',#useself=N'False',#locallogin=NULL,#rmtuser=N'SA',#rmtpassword='########'
GO
EXEC master.dbo.sp_serveroption #server=N'MyServer', #optname=N'collation compatible', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'MyServer', #optname=N'data access', #optvalue=N'true'
GO
EXEC master.dbo.sp_serveroption #server=N'MyServer', #optname=N'dist', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'MyServer', #optname=N'pub', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'MyServer', #optname=N'rpc', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'MyServer', #optname=N'rpc out', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'MyServer', #optname=N'sub', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'MyServer', #optname=N'connect timeout', #optvalue=N'0'
GO
EXEC master.dbo.sp_serveroption #server=N'MyServer', #optname=N'collation name', #optvalue=null
GO
EXEC master.dbo.sp_serveroption #server=N'MyServer', #optname=N'lazy schema validation', #optvalue=N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'MyServer', #optname=N'query timeout', #optvalue=N'0'
GO
EXEC master.dbo.sp_serveroption #server=N'MyServer', #optname=N'use remote collation', #optvalue=N'true'
GO
EXEC master.dbo.sp_serveroption #server=N'MyServer', #optname=N'remote proc transaction promotion', #optvalue=N'true'
GO
After creating the linked server, it works fine if I query tables in the system databases. User databases, however, cannot be seen. The sa account, is of course a member of sysadmin on the target server. I also tried creating an explicit user in the target database.
I also tried making a user database the default database.

Can I exclude a stored procedure from a transaction in MS SQL?

To my problem: I call a stored procedure from my business code. This call is in a explicit transaction. The stored procedure sometimes calls another one to write something into the database. This data should stay in the database even when the transaction is rolled back. A similar scenario is when you want to write something in a log table and the log message should be kept (this is not my case, it is just a similar requirement).
How could I exclude the second stored procedure from the outer transaction? I think that I am looking for something like "autonomous transactions" in Oracle. I looked for a possible emulation but all the solutions didn't look very "nice" (create a loopback server, add some .NET methods, ...)
Any ideas? Thank you!
There is no elegant solution to this type of problem although it seems to be common. Everything between begin transaction and either commit or rollback is done as a whole. You cannot just insert a line into a log table for instance and keep that one after an eventual rollback.
But you can do some tricks.
1) Call your procedure with xp_cmdshell to call OSQL.exe. Performance would be bad, but external commands do not participate in the transaction and nothing keeps you from executing SQL statements externally.
2) in the stored procedure you could add the records into a table-variable instead of a real table. Table-variables do not participate in the transaction, as they do not alter the database. Afterwards append the content of the variable to your table when you closed the transaction in either way.
3) If you cannot change the inner procedure, you can fetch the record(s) that it possibly did create into a table variable after the call but from within the still open transaction. Rollback the transaction and append the fetched records to the table again.
Yes you can! Use a linked server to yourself and set the 'remote proc transaction promotion' to false. Here is an example:
EXEC master.dbo.sp_addlinkedserver #server = N'LOOPBACK', #srvproduct=N'Microsoft', #provider=N'SQLNCLI', #datasrc=N'MYMACHINE\INSTANCE', #catalog=N'DB_NAME_HERE'
EXEC master.dbo.sp_serveroption #server=N'LOOPBACK', #optname=N'remote proc transaction promotion', #optvalue=N'false'
EXEC master.dbo.sp_serveroption #server=N'LOOPBACK', #optname=N'rpc', #optvalue=N'true'
EXEC master.dbo.sp_serveroption #server=N'LOOPBACK', #optname=N'rpc out', #optvalue=N'true'
--I think most below are defaults
EXEC master.dbo.sp_serveroption #server=N'LOOPBACK', #optname=N'collation compatible', #optvalue=N'false'
EXEC master.dbo.sp_serveroption #server=N'LOOPBACK', #optname=N'data access', #optvalue=N'true'
EXEC master.dbo.sp_serveroption #server=N'LOOPBACK', #optname=N'dist', #optvalue=N'false'
EXEC master.dbo.sp_serveroption #server=N'LOOPBACK', #optname=N'pub', #optvalue=N'false'
EXEC master.dbo.sp_serveroption #server=N'LOOPBACK', #optname=N'sub', #optvalue=N'false'
EXEC master.dbo.sp_serveroption #server=N'LOOPBACK', #optname=N'connect timeout', #optvalue=N'0'
EXEC master.dbo.sp_serveroption #server=N'LOOPBACK', #optname=N'collation name', #optvalue=null
EXEC master.dbo.sp_serveroption #server=N'LOOPBACK', #optname=N'lazy schema validation', #optvalue=N'false'
EXEC master.dbo.sp_serveroption #server=N'LOOPBACK', #optname=N'query timeout', #optvalue=N'0'
EXEC master.dbo.sp_serveroption #server=N'LOOPBACK', #optname=N'use remote collation', #optvalue=N'true'
CREATE PROCEDURE dbo.ap_deleteme_outsideTransaction AS
--codes is just some random table I have
exec ('insert into codes values (1, ''TEST'', ''TEST_ED'', ''DELTE_ME'', 0, ''1234'', ''myId'', getDate())') at LOOPBACK
GO
CREATE PROCEDURE dbo.ap_deleteme_test_transaction AS
begin transaction
insert into codes values (10, 'TEST', 'TEST_ED', 'DELTE_ME_1', 0, '1234', 'myId', getDate())
--exec ('generic query you may want to execute') at LOOPBACK
exec dbo.ap_deleteme_outsideTransaction
insert into codes values (20, 'TEST', 'TEST_ED', 'DELTE_ME_2', 0, '1234', 'myId', getDate())
--rolling back like this makes no sense, but here you should be able to see 3 records
--inserted and then two rolled back. The record inserted in the second proc call
--will still remain.
rollback transaction
GO
I would not call this optimal, but once you setup this link, you should be able to direct any call you do not want to participate in the transaction to this link and it will not participate. My initial tests do not show a significant performance hit, but be aware that making calls in a loop may prove to be costly.
-Update: Performance tests show it is 1-2 millisecond hit, but this is about 60x slower than a "direct" call. 500 or so hits is not noticeable, but when you get thousands, you can start to see seconds add up - not that this would be a common practice. We see that it far outweighs other side effects such as blocking, which is what we put this in place for.
To the best of my knowledge, you can't exclude specific operations from the transactions that contain them. My approach would be to try to explicitly do the things you want to do instead of rolling back the entire transaction. Or, if it's an option, rollback the transaction, and then re-run the stored procedure that you didn't want to rollback.

AS400 DB2 national characters into SQL Server linked server

I have a SQL Server 2012 Enterprise edition linked server connection to an IBM AS400 DB2 database.
I am using the IBMDASQL provider for the connection.
I have problems with some Hungarian characters: the letter Ő is converting to O during the select.
In this example the name_converted column will be OK, the Ő letter remains Ő.
The name column will be converted to O.
SELECT * FROM openquery (g,
SELECT
cast(name as char(35) ccsid 870) as name_converted,
name,
FROM libr.mytable')
My question is: can I create the linked server so that all the character is using the CCSID 870?
EXEC master.dbo.sp_addlinkedserver #server = N'G', #srvproduct = N'HUN00101', #provider = N'IBMDASQL', #datasrc = N'HUN00101'
EXEC master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'G', #useself=N'False',
#locallogin=NULL, #rmtuser=N'query', #rmtpassword='########'
GO
EXEC master.dbo.sp_serveroption #server = N'G', #optname = N'collation compatible',
#optvalue = N'false'
GO
EXEC master.dbo.sp_serveroption #server=N'G', #optname=N'data access', #optvalue=N'true' GO
EXEC master.dbo.sp_serveroption #server=N'G', #optname=N'dist', #optvalue=N'false' GO
EXEC master.dbo.sp_serveroption #server=N'G', #optname=N'pub', #optvalue=N'false' GO
EXEC master.dbo.sp_serveroption #server=N'G', #optname=N'rpc', #optvalue=N'false' GO
EXEC master.dbo.sp_serveroption #server=N'G', #optname=N'rpc out', #optvalue=N'false' GO
EXEC master.dbo.sp_serveroption #server=N'G', #optname=N'sub', #optvalue=N'false' GO
EXEC master.dbo.sp_serveroption #server=N'G', #optname=N'connect timeout', #optvalue=N'0' GO
EXEC master.dbo.sp_serveroption #server=N'G', #optname=N'collation name', #optvalue=null GO
EXEC master.dbo.sp_serveroption #server=N'G', #optname=N'lazy schema validation', #optvalue=N'false' GO
EXEC master.dbo.sp_serveroption #server=N'G', #optname=N'query timeout', #optvalue=N'0' GO
EXEC master.dbo.sp_serveroption #server=N'G', #optname=N'use remote collation', #optvalue=N'true' GO
EXEC master.dbo.sp_serveroption #server=N'G', #optname=N'remote proc transaction promotion', #optvalue=N'true' GO
Thanks
It looks like the data may not have been properly tagged with CCSID 870 on the IBM i system. Can you try using the "Force Translate" custom property and setting it to 870?
Note: This will cause all data not properly tagged with a CCSID to be handled as CCSID 870.

Resources