Run a SQL Server procedure from Excel - sql-server

I'm using SQL Server 2008 Enterprise. I created a procedure in one database. The procedure is composed of several queries to different databases and the final combined result set is being displayed.
I try to execute it via Excel, so the results will appear automatically in Excel sheet, but I'm getting the error:
The query did not run, or the database table could not be opened. Check the database server or contact your DBA. Make sure the external database is available and hasn't been moved or recognized, then try the operation again
I created a simpler procedure that queries only one database, and the results displayed at the Excel sheet with no issues.
Hence I suspect that, the original procedure failed due to the fact that I'm querying several databases in the procedure, when in the connection details of the "External Data Properties", only one database is mentioned.
My question is - can it be solved? Can I use multiple databases in the procedure and see it in the Excel?
Thanks,
Roni

I transformed the procedure to have Table Variables instead of Temporary tables and I've added "set nocount on" to the beginning of the procedure.
The second action solved the issue.
The first action improved the response time of the procedure.

(Copying key parts of #Roni's answer)
create procedure dbo.xxx
as
set nocount on
...

Related

Stored procedure works in SQL Server/SSRS query designer. Report body is empty

I have a stored procedure that runs successfully in SQL Server that is not loading anything into the body of the report. There are only 4 parameters to this procedure, 2 varchars and 2 ints.
I have verified the following:
The stored procedure works in SQL Server
The stored procedure works in SSRS when using Query Designer
The stored procedure shows as being executed and completed in SQL Server Profiler
Running what is shown in the SQL Server Profiler does return the expected results
There is no filtering on the table containing the results
There is no visibility restrictions on the table containing the results
The connection to the database is successful using "Test connection" as mentioned here
I have added a second different dataset into the report and it is showing in the body with no problems.
I have recompiled the procedure as mentioned here and it is still not showing in the report.
There are no multi valued parameters, so this answer is mostly irrelevant.
We are not using Visual Studio, so there is no .DATA files like mentioned here
However for the life of me, I can not get anything from this stored procedure to show up in the body of the report.
Due to legal reasons, the stored procedure cannot be posted in this question as it was part of a software our company is using. We have permission from them to use the procedure to make the SSRS Report for our needs.
Further diagnostics and logging are always useful.
You could have the procedure log additional data to a dedicated table for review later.
Useful data would be that which is related to the user's session based on ##spid and the system views sys.dm_exec_sessions, sys.dm_exec_requests, sys.dm_exec_input_buffer() such as statement start time, any blocking spids, wait_types. Also check the connection level properties are consistent and as expected eg quoted_identifier, ansi-settings etc.
You could also log information about the executing queries such as Rows Affected ##rowcount and the values of any parameters or intermediate values.

Access last table in a stored procedure from report builder

I have a stored procedure witch runs several EXEC commands. As a result it returns more than one table. In SQL Server Report Builder or SQL Server Data Tools (SSDT) I can only access the first table it retrieves from this stored procedure. But I need to access last table, in which contains the merged columns from different tables produced by different stored procedures.
I have tried the hide tables other than the last table, but failed. Is there any suggestions you can offer to solve this problem. I appreciate and thank with all my hearth to whom tries to contribute the solution of my problem.
I found a solution for this problem. It is not quite what I have asked but solves this issiue. Here is the solution:
I have edited all sub-stored procedures that I used to "RETURN 0" to prevent them to give an output. So when I call them from Main stored procedure they have no visible output at the "Results" window. Only the Main stored procedure has a single output. Thus I can use it in Report Builder or SSDT like a normal stored procedure without any more modification.

How to store sql result without creating a table to store the result of a stored procedure

We are running exec xp_fixeddrives to get the free space for each physical drive associated with the SQL Server.
I am running this code as part of an SSIS package which fetches all the SQL servers free space.
Currently I am creating a table in tempdb and inserting the results of exec xp_fixeddrives into this table. But the issue I am facing is, when ever the server is restarted I am facing access issue as the table is on Tempdb.
I don't really like the idea of creating a table in Master DB or Model DB. A challenge I am facing is we execute on difference instance of SQL Server versions ranging from 2000 - 2014. So obviously there are issues I have to keep in mind.
Any suggestion on this are much appreciated.
That's obvious, SQL Server reset TempDb whenever SQL Services is restarted. In that case, you will face access issues because that table won't exists. I would probably create my own table to store the details if I want to store historical check information also.
If you are running your code from SSIS and you want to send mail just after validating it then you don't even have to create any table. Just fill a object variable in SSIS from execute SQL task which will be running below query
DECLARE #t TABLE
(Drive VARCHAR(1),Size INT)
INSERT INTO #t
EXEC MASTER..xp_fixeddrives
SELECT * FROM #t
Read this object variable in script task to send mail.

SQL Server export temporary tables created in stored procedure

I work with SQL Server 2012 and create a multi-user application. I work with a stored procedure which changes data matching the input. The stored procedure consists of different SQL statements and is quite complex.
I donĀ“t want the user to change data directly in my database, so I use temporary tables. I want to export the customized temporary tables to Excel.
Is that possible? I already figured out how to create XML data, but creating Excel sheets would be easier for me.

SSIS - Log to table other than SYSSSISLOG

SSIS seems to insist on logging to the system table SYSSSISLOG. Is there a way to make it use a different table?
I want each package to log to a different table.
Quick answer is the same as John Sansom's answer: When logging is used, it creates a table and a stored proc (name varies with version between 2005 and 2008) The stored proc can be modified to do whatever you want. If the stored proc is removed Sql server re-creates it, but if the stored proc is there, Sql server assumes it is OK and leaves it alone. This allows you to modify the stored proc to write to whatever table/tables you want.
Well, you can query that huge-ass log table with something like this:
--first, we identify the packages
;with DetectedPackages as (
select source, s.executionid
from dbo.sysssislog as s
where event = 'PackageStart'
group by source, s.executionid
)
--then we use those executionids to display results
select * from dbo.sysssislog as s
join DetectedPackages dp on s.executionid = dp.executionid
where dp.source = 'PackageName'
And if you want to encapsulate every package in a view, now you know how to do that.
Take a look at the following article over on SQL Server Central, you may need to register but it's free to do so and you will find the site to be excellent SQL Server resource.
The article details how to implement a custom Log Provider that redirects the SSIS log output to another table. Using this implementation as your framework you could extend it to meet your requirements.
SSIS Custom Logging the Easy Way
The above is quite correct however not written well. When you specify your logging in SSIS you can log to a specific data provider IE SSIS Log provider for SQL Server. When you point this to a specific database it will create a [dbo].[sysssislog] table under the System Tables folder in your database. If you navigate in SSMS to your database and programmability -> Stored Procedures there will be a procedure called [dbo].[sp_ssis_addlogentry] this will insert log entries from SSIS. You can repoint this stored procedure to point to the table you want to log to instead of the one generated by SSIS within your database.

Resources