Linked-Server stored procedure data missing in SSRS - sql-server

I am building a report in SSRS using Visual Studio 2010 Design view. I am using a stored procedure as my data set. When I run out the stored procedure in SQL Server, it runs fine. When I run it out in preview, I am missing some columns.
In the stored procedure, I join a table that lives on a linked-server through a synonym. These seem to be the missing columns in the preview.
Is there a known issue where linked-server data will not get returned in SSRS even if it compiles in SQL Server?

This is probably due to permissions if you have the linked server set to execute as the context of the current user - it will use the credentials of the Data Source in SSRS.
Is your data source configured to use a specific user? Try running the sproc as that user?
-- Edit based on Comments --
It appears the issue is probably with the linked server authentication. There are a few ways round this;
Set up Kerberos pass-through authentication so you can tick 'Be made using the login's current security context' in the linked server properties
https://blogs.msdn.microsoft.com/farukcelik/2008/01/02/how-to-set-up-a-kerberos-authentication-scenario-with-sql-server-linked-servers/
https://www.databasejournal.com/features/mssql/article.php/3696506/Setting-Up-Delegation-for-Linked-Servers.htm
Use a SQL (not Windows) account on the remote server by ticking 'Be made using this security context' and entering the sql accounts details
Use a scheduled job of some kind to transfer the data the sproc returns over to one server so that linked server connection is not required
Change the report to query both data servers separately (as two different data sources and data sets), then join the results within the report (this is a bit fiddley and not really recommended)

Related

How can I profile the SELECT statements inside stored procedures when using SQL Server Profiler for Azure Data Studio on an Azure SQL Database?

The SQL Server Profiler for Azure Data Studio shows stored procedures, when connected to an Azure SQL Database.
Is it possible to profile the SELECT statements inside the stored procedures?
Is the reason why I seem unable to do it because there is only one (limited) template provided with the profiler when accessing Azure SQL Database?
The SQL Server Profiler GUI within Azure Data Studio is just a front-end for Extended Events. If you want to set up different data collection, such as sp_statement_completed to see individual statements within procedures, you just have to define an Extended Events session with that event and you're good to go. Here are the basics to get that done. Instead of the SSMS GUI, use the "Corresponding T-SQL" section as the guide for creating your own session. You may also want to correlate the individual statements with the start & stop of the procedure itself, rpc_starting and rpc_completed. In that case, use Causality Tracking as I show it here on my blog. Then, you can query the XML output, or, use another tool to consume the information (I do prefer DBATools for that approach).
That should give you everything you need.

SSRS Report Dataset Not Showing Correct Data for Two Fields

Firstly some background on the environment..
RDL's are designed in Report Builder 3.0 (The pre-2016 one).
RDL's are hosted on what appears to be an SSRS 2014 Reporting Services server (Reason why I am saying appears to be is because the alias of the Report Manager and Web Service URL is "SSRS_2014" (And the DBA's told me so)).
Our database server is either running SQL Server 2014 or 2016. I am running SQL Server 2017 on my workstation.
The problem:
I have an SSRS report data set that retrieves information from a very standard stored procedure. Recently I had to change one line of code and add a column related to this change to the result set. Stored procedure works as expected when testing it in SSMS Query Analyser. Here’s an excerpt of the intended results:
But after refreshing the dataset (which adds the new column) the dataset now returns inconsistent values under two columns for all the records returned whilst the stored procedure that retrieves the data is returning values when I run it in SSMS. Even when I run it in Query Designer it still returns inconsistent values.
Here’s a screenshot:
This is not a shared data set and from what I can gather the report does not have any caching applied. When trying to see if there is, or when trying to setup caching the Report Manager returns the following error:
An error occurred within the report server database. This may be due to a connection failure, timeout or low disk condition within the database. (rsReportServerDatabaseError)
Get Online Help For more information about this error navigate to the report server on the local server machine, or enable remote Errors.
As below:
This behaviour is occurring even under the following circumstances:
I deleted and added the data set.
I deleted and added the data source.
I created a new data source.
I created a new RDL.
I created a copy of the stored procedure (Renamed slightly).
I added copies of these columns (Renamed slightly) to the stored procedure (They behave the same).
Fiddled with the “Use single transaction when processing the queries” option (Was off).
The clinchers:
(Bear in mind that the stored procedure returns the data correcting in SSMS).
- When I hard-code the values in the stored procedure, these values appear when executing the stored procedure via SSMS Query Analyser but not when running it via Query Designer (Same inconsistent values).
- I have another (summary) report that obtains data from this stored procedure (Embedded) via another procedure. That report returns the data correctly.
- I tried to create a second stored procedure that executes this stored procedure (Similarly to what the “summary” one does) and it still misbehaves.
- When I take the script that I use to test the stored procedure in SSMS Query Analyser it returns the values! But this is not ideal because the parameters are defined and when I remove those parameters it goes back to normal (mis)behaviour.
I was considering adding a type of snapshot table. Where the stored procedure would first build the data and then do a select from this table to return the data. The problem here is this report is run my multiple users and I do not have the time (3 days before Christmas and I am already on Christmas leave) do go and design a whole snapshot system.
I did a lot of research on the internet yesterday. And went through all suggestions given in the following sites/forums to no avail:
Why is my SSRS report showing old data?
SSRS: field shows correct in query but wrong in report preview
How to clear cache of 1 stored procedure in sql server
https://jazz.net/forum/questions/243993/report-builder-not-showing-updated-data
https://learn.microsoft.com/en-us/sql/t-sql/database-console-commands/dbcc-freeproccache-transact-sql?view=sql-server-2017
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/e6f86f15-420c-4b64-bc70-01dea93a0995/report-results-and-query-results-returning-different-number-of-rows-why?forum=sqlreportingservices
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/9bc13904-a727-4786-9a11-693570f5f8d4/diable-the-cache-for-all-reports-in-ssrs-2008?forum=sqlreportingservices
And various other ones about caching and so on.
I even got the DBA’s to restart that SSRS server to see if that might refresh any forms of caching.
I also added “WITH RECOMPILE” hint to the stored procedure to no avail.
So I am at a loss… I am at this stage thinking of taking the Stored procedure’s code and running it as an embedded query but that is not best practice.
We are also in the process of handing this project over to another company so I don’t want to leave them this (I have my pride).

Creating a snapshot database to another SQL Server

I'm trying to save the values of several columns of one table to another table on a different server. I am using SQL Server. I would like to do this without running external programs that query from this database and insert the results into the new database. Is there any way to do this from within the SQL Server Management Studio?
This is a recurring event that occurs every hour. I have tried scheduling maintenance tasks that execute custom T-SQL scripts but I'm having trouble getting the connection to the remote server.
Any help would be appreciated.
If you can set up the remote server as a linked server you should be able to configure the SQL Server Agent to execute jobs that contain queries that access tables on both the local and linked server. Remember that you might have to configure the access rights for the account used to run SQL Server Agent so that it has permissions to read/write tables on both servers.
This practice might not be without issues though as this article discusses.
You can use a 4 part name like;
INSERT [InstanceName].[DatabaseName].[SchemaName].[TableName]
SELECT * FROM [SourceInstanceName].[SourceDatabaseName].[SourceSchemaName].[SourceTableName]
But first you will have to set the remote server as a linked server as so;
https://msdn.microsoft.com/en-us/library/aa560998.aspx

How to audit SQL Server 2008 queries through WCF Services?

I want to save any kind of log/tables with every query executed by my application.
I know I could do this by coding it (before I make any query, I insert a new row in a log table with the query and the user who is executing it.
I have read it can be done automatically but I'm not sure how can it work with WCF Services. I mean every query is going to be executed by the same SQL user and this wouldn't be very useful for audit operations (I need to know WHO made every query, and users will be validated against my own users tables).
Have you ever had a similar scenario? Thanks in advance!
As a starting point it may be worth looking into doing this via SQL Server Profiler. You can normally find this in the Tools Menu in Management Studio.
You can set up a trace to capture all SQL run on a server. More importantly you have a myriad of filter options which can be applied so that you only capture the data you are interested in (e.g. DatabaseName, UserName).
This information can be stored directly in a SQL Table, which should give you the abillity to join onto. Of course running anything like this will result in some overhead on the SQL box.
You can try the SQL Server Audit feature. It audits singe or groups of events both on server and database level. However, be advised that the database level auditing is available in SQL Server Enterprise and Developer editions only

Execute 'View' residing in a remote server from a stored procedure

How to get the results (pass parameter also) of a view of a remote server from a stored procedure?
The view is in a separate server from the current server where the stored procedures exist.
Thanks.
You could use the Linked Server feature of SQL Server inside your stored procedure:
A linked server configuration allows
Microsoft SQL Server to execute
commands against OLE DB data sources
on different servers. Linked servers
offer these advantages:
Remote server access
The ability to issue distributed
queries, updates, commands, and
transactions on heterogeneous data
sources across the enterprise.
The ability to address diverse data sources similarly.
You would have to write your query inside your stored procedure this way for example:
SELECT *
FROM MyRemoteServer.MyDB.dbo.MyView
WHERE MyViewColumnX = #ParameterY
Create a linked server on your main SQL Server, creating the link to your remote server.
The fact you have a view on the remote server is good, it provides a layer of abstraction to your data. You can specify a login for the linked server that has permission to read that view only. You could also modify the view in the future (add a "where" clause for example to improve performance) with no front end changes required.
I would also add another view on your main server that selects from the view on your remote server: -
create view vwMain as select col1, col2 from RemoteServer.DB.dbo.RemoteView.
That way you can reference vwMain throughout your code (within many stored procedures) without continually referencing the remote server. This way, should your remote server change or you move the data to your main server, it is one simple change to vwMain and all your code continues to work.

Resources