Entity Framework and Application Intent parameter - sql

I have a stored proc that uses a synonym to pull data from another SQL Server 2016 database.
This is the connection string that EF is using:
<add name="ConnectionString_CSAdminEF_RO" connectionString="Data Source=myserver;initial catalog=csadmin;user=tcrpAdminTool;password=XXXX;applicationintent=readonly" providerName="System.Data.SqlClient" />
It throws this error:
The OLE DB provider "SQLNCLI11" for linked server "MYSERVER.CORP.COMPANY.COM" does not contain the table "otherserver"."dbo"."mysynonym"". The table either does not exist or the current user does not have permissions on that table.
When I try it in ADO.NET it throws this error:
Procedure usp_get_Data has no parameters and arguments were supplied.
Here is the synonym:
CREATE SYNONYM [dbo].[mysynonym] FOR [OTHERSERVER.CORP.COMPANY.COM].[mydb].[dbo].[eDocsEntity]
The problem only started when I added applicationintent=readonly. This additional parameter works fine when no parameter is involved.
Any ideas as to what could be causing this? I've seen nothing in the forums about it
Thanks
Carl

From our DBA:
I was working on a linked server on primary not realizing that when you read only route to secondary you have to check the secondary servers linked server and in secondary which is a DMS primary they are using a server which did not have read rights to the database referenced by the synonym
I gave it read permissions and that's it

Related

access a synonym using linked server

Using SQL server 2016
Is it possible to access a synonym from a linked server, when the actual target table of the synonym cannot be accessed by the source server?
There are two databases in one server
say on
server1
database1
database2
server2
database3
And there is a table "table1" on database1.
And a synonym for that has been created in database2
USE [database2]
GO
CREATE SYNONYM [dbo].[synonym1_for_table1] FOR [server1].[database1].[dbo].[table1]
GO
There is a linked server access for database2 from server2
When the following query is executed from the server2
SELECT *
FROM [server1].[database2].[dbo].[synonym1_for_table1]
Below error is encountered
Cannot process the object [database2].[dbo].[synonym1_for_table1]. The
OLE DB provider "SQLNCLI" for linked server "server1" indicates that
either the object has no columns or the current user does not have
permissions on that object.
In server2 under "linked servers", under the tree
I see tables and views of database2 but not synonyms.
Is there a way to see synonyms. That could be a solution to this issue.
Update:
If the synonym is made a view, the following error is observed
The server principal "xyz" is not able to access the database "database1" under the current security context.
"xyz" cannot be created/cannot be granted access to "database1".
You should create a view to read data from table present on the other server/database.
Also, create a separate user (service account or user account) that will have access only to read data from the source table and can execute the view(present on the different server).

Calling a Function Using DB Link in Oracle

I have a function (takes 2 parameters) that exists in my source DB and I am trying to call it from another DB using a DB Link. I am using the following code but I keep getting an error:
SELECT SCHEMA.FUNC_NAME#DBLINK(PARAM1, PARAM2) FROM SCHEMA.TABLE_NAME#DBLINK;
And here is the error I am getting:
ORA-00904: "SCHEMA"."FUNC_NAME": invalid identifier
I am going to rename your database references in my attempt to answer your question.
I will use localDB and remoteDB as database names.
You have a function created on remoteDB.
You should have the DBlink created on localDB. In the DBLink definition, you have owner, db_link, username, host, and created date.
On the remoteDB, you need to give select and maybe execute privileges to the username specified in your dblink.
Then, you should be able to execute the function from the localDB using some syntax such as EXEC SQL CALL schema.func_name#dblink(parm1, parms);

VSTS-Build for SQL server project is not working

I have an Azure SQL Server database that I am trying to put into TFS. I have created project and imported database, but when I try to do a build in Visual Studio I get the following error:
"SQL71589: Master Key must be created before a database scoped
credential".
You need to create scoped credentials for your database. See this MSDN article:
https://learn.microsoft.com/en-us/sql/t-sql/statements/create-database-scoped-credential-transact-sql?view=sql-server-2017
Depending on your scenario, the code will look something like this:
-- Create a db master key if one does not already exist, using your own password.
CREATE MASTER KEY ENCRYPTION BY PASSWORD='<EnterStrongPasswordHere>';
-- Create a database scoped credential.
CREATE DATABASE SCOPED CREDENTIAL AppCred WITH IDENTITY = 'dbUserNameFooBar',
SECRET = '<EnterStrongPasswordHere>';
GO
Hope that helps.

Need to learn how to set appropriate permissions on SQL Server database to allow Synchronisation through Sync Framework 2.1

SQL Server is not my strong point and I start to get lost when going past the basic Create Table, Select from Table etc.
I am attempting to set up a database synchronisation scenario with an Microsoft SQLCompact 3.5 database being Synced through a Proxy to and SQL 2008 R2 database. It is being synced through the Microsoft Sync Framework 2.1.
I got everything set up and working fine through the proxy when the user account is the db_owner of the database.
Of course this now needs to be locked down - so I have been trying to restrict the permissions to the bare minimum required for Synchronisation.
According to the Microsoft articles, I need to do the following...
Use the principle of least permission. Do not grant more permissions than are required to perform a specific task. For example, do not grant INSERT permissions for server database tables that are involved in download-only synchronization. Synchronization operations require the following permissions:
EXECUTE permissions on all the stored procedures that Sync Framework uses to read and write to metadata tables and base tables.
SELECT, INSERT, UPDATE, and DELETE permissions for metadata tables and any base tables that will be updated during a synchronization session.
When you provision SQL Server databases that use SqlSyncProvider, be aware of the following permissions requirements for provisioning:
CREATE TABLE permissions to enable creation of the metadata tables: scope_info and scope_config, and the tracking tables that are created for each base table.
ALTER TABLE to add triggers to the base tables.
CREATE PROCEDURE permissions to create the procedures that Sync Framework requires.
SELECT and INSERT permissions for scope_info and scope_config tables.
SELECT permissions for base tables.
I allowed the wizards in Visual Studio 2010 to create the Sync database and proxy for me.
As such - I am unable to find the scope_info and scope_config tables in SQL Server databases, and I am also unable to find the metadata tables so cannot set permissions on these tables. Also - where would I find the stored procedures that the Synchronisation framework is trying to use - I have looked but cannto find them.
Where would I find these and how would I go about setting the appropriate permissions?
I have granted datareader and datawriter, Insert, Update, Delete and Select as well as Execute permissions on the SQL Server database but the sync fails. I have also granted Create Table, Create Procedure and ALTER permissions on the database for the user as well- but still it fails.
If i enable the db_owner role for the user - it works.
The error I receive is:
Unable to initialize the client database, because the schema for table 'tblApplications, tblApplicationConfiguration, tblApplicationInstallProperties, tblApplicationPreRequisites, tblApplicationTypes, tblComputerApps, tblComputers, tblComputerTypes, tblDriveHWSerials, tblDrives, tblDriveTypes, tblFunctions, tblLocationApps, tblLocationComputers, tblLocationIPAddress, tblLocations, tblLocationUsers, tblPermissions, tblRegionLocations, tblRegions, tblRegisteredModules, tblRequestFormats, tblRequestStatus, tblRequestTypes, tblRoles, tblRoleUsers, tblSecurity, tblUsers, tblVehicle, tblVehicleLocationMap, tblVehicleMake, tblRequestProcessingStatus, tblDriveStatus, tblVideoViewTypes' could not be retrieved by the GetSchema() method of DbServerSyncProvider.
Make sure that you can establish a connection to the client database and that either the SelectIncrementalInsertsCommand property or the SelectIncrementalUpdatesCommand property of the SyncAdapter is specified correctly.
I am not able to use the db_owner role when its released.
there are two types of database providers in Sync Framework, the offline provider (SqlCeClientSyncProvider/DbServerSyncProvider) which is used by the Local Database Cache project item and the collaboration/peer-to-peer provider (SqlSyncProvider/SqlCeSyncProvider).
the offline providers dont use the scope_xxx tables as such you wont find it.
assuming you used the default SQL Server Change Tracking when you configured the sync via the wizard, try granting VIEW CHANGE TRACKING permission to the account you're using to connect to the database.
I'm not sure if it will help, but I found this:
Try to set UploadOnly on the SyncTable object instead. When you set it on the SyncAdapterBuidler, the select incremental commands are not generated by the builder. These commands are used to get the table schema from the database. Since these are missing you are getting the schema error.
Also, maybe this blog entry will help.
As JuneT Mentioned, you should turn on Change Tracking
ALTER DATABASE YourDataBaseName
SET CHANGE_TRACKING = ON
(CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON)

Oracle: Using a database link in a stored procedure : table or view does not exist

I currently have an issue whereby I cannot reference a table in a linked database within a stored procedure. I get the error message:
ORA-00942: table or view does not exist
Here are the steps I took on the host machine (running oracle 10g) to set up the database link to the remote database (running oracle 11g). The steps are accurate, but some some names have been changed, though they have been kept consistent.
Update tnsnames.ora, adding a new entry:
REMOTE_DB =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)
(HOST = 10.10.10.10)
(QUEUESIZE = 20)
(PORT = 1521)
)
(CONNECT_DATA =
(SERVICE_NAME = remote_service)
)
)
Create database link, as the user who will later be creating and executing the stored procedure:
create database link remote_link
connect to "remote_user"
identified by "remote_pass"
using 'REMOTE_DB';
Prove database link is working by selecting from it:
select id from remote_table#remote_link;
id
--------------------------------------------------------------------------------
8ac6eb9b-fcc1-4574-8604-c9fd4412b917
c9e7ee51-2314-4002-a684-7817b181267b
cc395a81-56dd-4d68-9bba-fa926dad4fc7
d6b450e0-3f36-411a-ba14-2acc18b9c008
Create stored procedure that depends on working database link:
create or replace
PROCEDURE test_remote_db_link
AS
v_id varchar(50);
BEGIN
select id into v_id from remote_table#remote_link where id = 'c9e7ee51-2314-4002-a684-7817b181267b';
dbms_output.put_line('v_id : ' || v_id);
END test_remote_db_link;
Explode own head after staring at the following error message for over an entire working day:
Error(10,27): PL/SQL: ORA-00942: table or view does not exist
I have tried many things to try to sort this issue out, including:
When creating the database link, not using quotes around the username and password. Link creates fine, but selecting from it gives me this error:
ERROR at line 1:
ORA-01017: invalid username/password; logon denied
ORA-02063: preceding line from TWS_LINK
Tried various combinations of username and password in upper/lowercase. Received same error as 1.
Tried single quotes instead of double quotes around username and password. Recieved this error:
ERROR at line 1:
ORA-00987: missing or invalid username(s)
Proved I have full access to the remote db by connecting into it with sqlplus:
[oracle]$ sqlplus remote_user/remote_pass#REMOTE_DB
SQL*Plus: Release 10.2.0.1.0 - Production on Thu Oct 20 22:23:12 2011
Copyright (c) 1982, 2005, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>
I'm not sure what to do next. The possible next step is to start looking at issues on the remote database, and perhaps see if other databases can connect to it. Another would be to look at incompatibilities going from host 10g to remote 11g.
OK so I was able to get this working, of sorts.
It turns out that when creating the database link, the double quotes around the username and password fields were causing the issue. To summarise:
If they were present, and the link created as so:
create database link remote_link
connect to "remote_user"
identified by "remote_pass"
using 'REMOTE_DB';
The remote database could be queried via sql
The stored procedure could not be compiled, recieving the ORA-942 error
As the procedure could not be compiled, it could not be executed
When the double quotes are not present:
create database link remote_link
connect to remote_user
identified by remote_pass
using 'REMOTE_DB';
The remote database could not be queried via sql, recieving an invalid password error (detailed in the question)
The stored procedure could be compiled with no errors.
The stored procedure executes as expected, retrieving data from across the database link, and displaying it.
So, even though the remote database cannot be querued via sql, recieving an invalid password error, the procedure that uses this same connection information compiles and executes normally.
I'm sure you'll agree, this is a curious state of events, and I genuinely stumbled across making it work in my scenario. I'm not quite sure I would call it a solution, as there are plenty of unanswered questions.
Hopefully if someone comes here via google, they'll find this answer useful, and at least get their code running.
GC.
I faced the same issue on 11gR2, and I'm thankful to this forum for helping me find the problem. The way to make the db link work in both SQL and procedure is to follow the below syntax (enclose only the password within double quotes).
create database link remote_link
connect to remote_user
identified by "remote_pass"
using 'REMOTE_DB';
I think I see a problem here. Is the user who is executing the stored procedure the same user who created the stored procedure?
You said, "Create database link, as the user who will later be executing the stored procedure".
If the user creating the database link is different from the user creating the stored procedure, that may be your problem.
Try creating the stored procedure and database link as the same user, or creating a public database link.
Then, since Oracle default is definer rights, you can have anyone execute the stored procedure (assuming they have been granted execute privilege on the procedure).