Using stored procedure in openrowset throws login_mapping error - sql-server

Because I need to fill a view with dynamic data, I wrote a stored procedure to collect the data and use select from OPENROWSET('SQLNCLI','server=....;trusted_connection=yes','EXEC Stored_Procedure') as the view definition.
As far as I know you can't use variables in your view definition so that's why I choose this route.
The stored procedure collects data from a linked server source combined with native SQL Server data. The linked_server has the appropriate permissions setup for the remote user and can be successfully queried from outside SQL Server.
That works very well in a local context but when I try to use the view in a PostGreSQL foreign-table I get an ODBC error. The first errors involved the permission to execute the stored procedure by the caller's user account. After that I get stuck. It seems the connection with the server has sufficient access permissions but the OPENROWSET call to execute the stored procedure does not (no login_mapping exists).
However, if I change the SQL in the OPENROWSET command to a simple select statement (select * from db.schema.table) it works fine.
I tried to change the server in de OPENROWSET to a datasource to the local server itself, with the remote user to impersonate, but with no luck either. The same login_mapping error.
My guess is that this has something to do with the fact that the users security context is not the one used by the OPENROWSET command?
I have looked around a lot but was not able to find a solution. Can you help me?

Related

Trying to EXEC a stored procedure on another server

I created a stored procedure in a database called DBA_Test. I went to another server and tried to execute this stored procedure using
EXEC *LinkedServerName*.DBA_Test.dbo.spLoad_Clients_Monthly_Filings
When I try to execute the query, I get the following error:
Server 'LinkedServerName' is not configured for RPC.
When the line is in the query window of SSMS, the DBA_Test is underlined in red and hovering over it shows a message:
Could not locate entry in sysdatabases for database 'DBA_Test'. No entry found with that name. Make sure that the name is entered correctly.
I have everything spelled correctly and the stored procedure is listed in the database as seen here:
I tried executing the stored procedure on the same server, but from another database, and it ran fine. Every other server I switched to gave the red line error saying that the database could not be found in sysdatabases. I have all the linked servers working correctly.
When you create linked server you need to set RPC (and maybe RPC out):
Regarding your second question, sometimes SSMS doesn't correctly parse linked server objects

Excel error: Geting external data from microsoft query

I am fairly familiar with retrieving data from SQL Servers into Excel 2010. Recently I read the following article on allowing users to submit parameters to a stored procedure and was trying it out:
Data from SQL server
I am able to walk though all the steps of connecting to the server using the SQL Server Native Client 11.0 driver. I can call the stored procedure and return data in the Microsoft Query window (through step 8 of the instructions in the above link).
I can move through all 14 steps with ease. However, after linking the parameters to specific cells and defining the location for the data, hitting "OK" to return the data to the spreadsheet I receive the following error:
Some searching suggested that it may be a permission issue with the Stored Proc so I tried several variations of GRANT EXEC ON [dbo].[uspGLDetail_ItemNumber] TO PUBLIC. The proc I am attempting to execute is [uspGLDetail_ItemNumber].
You can see I am returning data in the Microsoft Query window:
I am not sure why the error is referencing that specific object. I attempted to check the permissions on the listed object, but do not find that procedure on our Server. Perhaps, part of the solution could be how to find the listed object in the error.
I am thinking this is perhaps more of an issues with Excel communicating to the server than a permission issue on the stored proc so was trying a few different drivers with no luck. However, I have successfully been retrieving data into excel From SQL Server with no issue for a long time.

Retrieve data from remote server in a stored procedure

Say I have a stored procedure that gets data from another database on another server like this:
SELECT * FROM [DatabaseServer].[DatabaseName].dbo.dbPerson
I can setup a linked server and it works as I would expect. Is there a way to connect to a remote server in a stored procedure without creating a linked server i.e. by specifying the connection string along with the username and password to connect with?
I know that I can do this with SSIS. I wandered if it is possible in a stored procedure.
As Zohar said you can use opendatasrouce like this
Select
*
from
OPENDATASOURCE(
'SQLOLEDB',
'Data Source=_SQL_SERVER_NAME_;User ID=_LOGIN_;Password=_PASSWORD_'
).[DatabaseName].dbo.dbPerson
WHERE ...
or you can use different providers and put different connection strings

Unable to call stored procedure from linked server

I am facing a strange issue. I have a linked server on ServerB for ServerA
Now, when I am calling a stored procedure from ServerB like
EXEC [ServerA].[Db].[dbo].[SpName] #Param1 #param2
I am getting error that
The EXECUTE permission was denied on the object 'SpName', database 'Db', schema 'dbo'.
But now when I am executing below query it is returning me result:
SELECT *
FROM [ServerA].[Db].[dbo].[tblName]
I don't know that why I am not able to execute stored procedure from ServerB. I am Db_Owner on both the server.
Screenshot of Linked server security
Linked server catalog
If a linked server query fails, the things to check are (in rough order of probability):
Try logging in locally on the linked server to test access directly. If you have no local access, obviously you won't have it through the link either.
Verify the correct credentials as used when accessing the linked server, and not another user you're not expecting. You can check this with EXEC ('SELECT USER_NAME(), SUSER_NAME()') AT [Server]; if the user name is not what you're expecting, check your linked server definition for the correct login mappings. If you can't access the server at all (any query fails), you have other problems (like Kerberos authentication issues if you're using integrated authentication).
Perform a sanity check that you're accessing the correct server with EXEC ('SELECT ##SERVERNAME') AT [Server]. The network name of a linked server can be changed using sp_setnetname, so the name you use to a access the server isn't necessarily the machine name.
If all else fails, dropping and recreating the linked server definition is always an option, but obviously this could disrupt production work.

SQL Server cross database alias

I'm trying to understand how I can use an alias to reference another database in the same instance, without having to use a hardcoded name.
The scenario is as below:
I have a data db with stores data, an audit db which keeps all changes made. for various reason, i want to keep the audit data in a separate database, not least because it can get quite large and for reporting purposes.
In the data db, I don't want to reference this by a hardcoded name but an alias so that in different environments, I don't have to change the name and various sp's to reference the new name.
for example:
mydevdata
mydevaudit
If a sp exists in mydevdata such as which calls the mydevaudit, I don't want to change the sp when I go to test where the db's may be called mytestdata and mytestaudit. Again, for various reasons, the database names can change, more to do with spaces an instances etc.
So if I had procedure in mydevdata:
proc A
begin
insert into mydevaudit.table.abc(somecol)
select 1
end
when I go to test, I don't want to be change the procedure to reference another name, (assume for sake of argument that happened)
Instead I am looking to do something like:
proc A
begin
insert into AUDITEBALIAS.table.abc(somecol)
select 1
end
I am interested in finding out how I could do something like that, and the pro's and cons.
Also, dymnamic SQL is not an option.
thanks in advance for you help.
You may be able to use synonyms
CREATE SYNONYM WholeTableAliasWithDBetc FOR TheDB.dbo.TheTable
This means all object references in the local DB are local to that DB, except for synonyms that hide the other database from you.
You can also use stored procedures in the audit DB. There is a 3rd form of EXEC that is little used where you can parametrise the stored proc name
DECLARE #module_name_var varchar(100)
SET #module_name_var = 'mydevaudit.dbo.AuditProc'
-- SET #module_name_var = 'whatever.dbo.AuditProc'
EXEC #module_name_var #p1, #p2, ...
Obviously you can change module_name_var to use whatever DB you like
I've just posted this to How to create Sql Synonym or "Alias" for Database Name? which is a workaround for the same situation:
There is a way to simulate this using a linked server. This assumes you have two SQL servers with the same set of databases one for development/test and one live.
Open SQL Server Management Studio on your development/test server
Right click Server Objects > Linked Servers
Select New Linked Server...
Select the General page
Specify alias name in Linked server field - this would normally be the name of your live server
Select SQL Native Client as the provider
Enter sql_server for Product Name
In Data Source specify the name of the development server
Add Security and Server Options to taste
Click OK
The above is for SQL Server 2005 but should be similar for 2008
Once you've done that you can write SQL like this:
SELECT * FROM liveservername.databasename.dbo.tablename
Now when your scripts are run on the development server with the linked server back to itself they will work correctly pulling data from the development server and when the exact same scripts are run on the live server they will work normally.

Resources