Data visibility between linked server - sql-server

Please consider the following situation
I have Prod instance of SQL server 2012
Also Archive instance of SQL server express 2012
Prod sees Archive as linked server and is able to write data with similar query from some .net code that creates a transaction and the transaction is committed at the end.
insert into <ArchiveServer>.<database>.<schema>.<table>
Select * from <ProductionServer>.<database>.<schema>.<table> Where <some conditions>
Now after the transaction finishes I am able to execute the following query
select count(1) from <ArchiveServer>.<database>.<schema>.<table>
and it returns correct number of records in the context of production server.
Same query
select count(1) from <database>.<schema>.<table>
in the context of Archive server returned 0 records.
What might be the problem? I am out of clues.
Thanks

Related

Copy Oracle table to SQL Server

I need to copy data from a linked server Oracle table and append to SQL Server table in a regular time interval.
Without using SSIS or any external etl, can this be done by using open query? I have read in forums that the table has to be exported to csv and then imported back again into SQL Server . Is there an alternative as this will be continuous process ?
Certainly. As long as you've configured the Linked Server properly within SQL Server, and your user account has the right permissions in Oracle, getting and sending data between the two is fairly trivial.
Selecting data from Oracle into SQL Server:
IF OBJECT_ID('tempdb..#myTemp') IS NOT NULL DROP TABLE #myTemp
SELECT *
INTO #myTemp
FROM OPENQUERY(MyLinkedServer, 'SELECT col1, col2
FROM OracleTableName
WHERE SomeColumn = ''Human Resources''')
Inserting data from SQL Server into Oracle:
-- note: please make sure you have your columns in the exact same order!
INSERT INTO OPENQUERY(MyLinkedServer, 'SELECT col1
FROM OracleTableName ')
SELECT myPK
FROM AdventureWorks2014.dbo.SomeTable
And you can even drop or recreate a table in Oracle from SQL Server, and run stored procedures.
To create a linked server, I use the below script and these settings:
-- make sure you have Oracle drivers on your SQL Server, and an up-to-date TNSNames.ora file
EXEC master.dbo.sp_addlinkedserver #server = N'MyLinkedServer', #srvproduct=N'Oracle', #provider=N'OraOLEDB.Oracle', #datasrc=N'TNS_NAME_HERE'
-- disable any logins not explicitly mapped
EXEC master.dbo.sp_droplinkedsrvlogin #rmtsrvname = N'MyLinkedServer', #locallogin = NULL
-- login mapping between SQL Server user and their appropriate Oracle account
EXEC master.dbo.sp_addlinkedsrvlogin #rmtsrvname=N'MyLinkedServer',#useself=N'False',#locallogin=N'domain\username',#rmtuser=N'ORACLE_USERNAME',#rmtpassword='ORACLE_PASSWORD'
-- add a mapping for your SQL Agent account if these will run under jobs,
-- and your SQL Engine account if you still get errors on things that are automated,
-- unless those automated jobs are explicitly running under user accounts
Honestly, the hardest part is making sure you get Oracle drivers installed right on the SQL Server. After that, it's very straightforward.

SQL Server select from linked server throw error if user has "deny view definition"

I have users with datareader permission on my sql, the problem is that i noticed that the users are doing "fishing" query a lot (they don't know what exactly should be querying to get what thy need).
This is a production SQL and we started to have deadlock because of those users.
Trying to don't break their process i applied "deny view definition to [user]" so they cannot see the list of tables, view, stored procedure, so no more "fishing"
This was a great solution, it works perfect if you connect directly to the SQL Server, you can run queries (select) but you cannot see the list of tables.
However many users are using those account to connect to our SQL using linked server, and when the run a query using the linked server they are getting error:
SELECT TOP 100 *
FROM [SERVERSQL].[DATABASE].dbo.[TABLE]
Msg 7314, Level 16, State 1, Line 1
The OLE DB provider "SQLNCLI11" for linked server "SERVERSQL" does not contain the table ""[DATABASE]"."dbo"."[TABLE]"". The table either does not exist or the current user does not have permissions on that table.
Using SQL Server version 2014 (v12.0.2000.8) and 2016 (v13.0.5492.2).
What is the correct way to hide tables, view, stored procedures, etc but allow select and make it work on linked server?
Thanks
Linked server from one SQL Server to Another:
Linked server name is: PLWNSAVSQL02D which is SQL Server
This sql to linked server works:
select count(*) from [PLWNSAVSQL02D].[SFI_WMS].dbo.[TCOMPANYPALLETMESSAGES];

SQL server -- Table not allowing insert or update of records

I have a Microsoft SQL database that we have been using for several years. Starting this morning a single table in the database is throwing a time-out error whenever we attempt to insert or update any records.
I have tried to insert and update through:
Microsoft Access ODBC
a .Net Program via Entity Framework
a stored procedure run as an automatic job -- that runs each morning
a custom query written this morning to test the database and executed through SQL Server Management Studio
Opening the table directly via 'Edit Top 200 Rows' and typing in the appropriate values
We have restarted the service, then restarted the entire server and continue to get the same problems. The remainder of the database appears to be working fine. All data can be read even from the affected table, and other tables allow updates and inserts to be run just fine.
Looking through the data in the table, I have not found anything that appears out of the ordinary.
I am at a loss as to the next steps on finding the cause or solution.
Its not a space issue is it ? try ...
SELECT volume_mount_point Drive,
cast(sum(available_bytes)*100 / sum(total_bytes) as int) as [Free%],
avg(available_bytes/1024/1024/1024) FreeGB
from sys.master_files f
cross apply sys.dm_os_volume_stats(f.database_id, f.[file_id])
group by volume_mount_point
order by volume_mount_point;

Retrieving single row within transaction hangs

I run several SQLs ( select, update and insert ) within single database connection and transaction, one of the SQLs is to retrieve the balance of an account
select top 1 balance from sample_table where account_id={accountId} order by id desc
This SQL hangs only and only if there are no records actually in the table "sample_table" for the account {accountId} , otherwise it works normally.
Hangs means it waits until a timeout exception error occurs, and during this 'wait' , executing the SQL from Sql Server management studio also hangs.
I'm using Sql Server 2008
thanks
ِEDIT:
After multiple attempts to perform same action, it does work and all the records are inserted properly and then the app works like the charm after that.
Restore the database, then the issue is back again.
The issue is fixed after rebuilding the table, basically executing this SQL:
alter table sample_table rebuild

SQL Server Management Studio Express not showing recently added data

I recently installed and have been using Microsoft SQL Server Management Studio Express 9.00.4035.00 to manage a remote database on my hosts' SQL Server 2005.
Until now I had been using EMS SQL Manager 2011 Lite and that was working fine.
2 new rows where inserted into one of my tables this morning; one by a customer signing up for a service and the other as a test signup by me.
When I run a typical select query:
[Select top 20 * From tblNotary Order By ID Desc]
I don't see the the 2 most recent rows. But when I run the same query from EMS SQL Manager Lite I see the records.
I also verified when connecting using MS Access 2010 I see the 2 new rows in the table. I have checked and double-checked the connection settings and they match EMS.
Is there a setting or something obvious I am missing? Why can't I see the most recent record insertions? I am on a Windows 7 desktop machine.
The most likely reason is you are connecting to a different database than you expected. You can select ##servername to verify both queries are running against the same server.
If the records are stil being inserted as part of an open transaction and have not been committed, they are called "phantom" rows. You will not see phantom rows if your query runs at transaction isolation level read committed or higher. It may be that EMS SQL Manager Lite is running at read uncommitted, in which case it will include phantom rows in the query.
Sometimes connection strings are a tricky thing. Run this to double check:
select ##servername servername, name, crdate
from sys.sysdatabases
where name = db_name()
if you have an issue running that, just do a simpler version:
select ##servername, db_name(), ##version
Select top 20 * From tblNotary Order By ID Desc

Resources