How to print select results from within a stored procedure - sql

I have a stored procedure that I'm trying to debug (T-SQL).
It contains creates a temporary table, and has several update statements to update it with various data.
How can I insert statements to view the contents of this table at various points during the running of the stored procedure.
Ideally, I'll be running this directly from MS SQL Server Management Studio, and simple output to the Message frame will suffice.

Wouldn't it be possible to SELECT what you need? Sorry if I misunderstood the question. Selected variables/tables will be returned as results

Related

Manipulating data from a stored procedure by saving data into a table

Problem: I have a stored procedure in SQL Server 2012 and I want to put constraints to the output so I only get relevant information.
I am using Execute. The way I see it I have two options:
save the result of the execution into a table, so I can use it for different purposes
put constraints to the variables in Execute so I only get the results I want
The first method is discussed here:
Insert results of a stored procedure into a temporary table .
My code is (due to company information I can't share the whole thing):
create table #mtable ( .... )
Insert into #mtable
Execute [myProcedure]
The error:
An INSERT EXEC statement cannot be nested.
I assume the error is because of the code in the stored procedure. How can I fix that problem without looking into the code for the stored procedure. Is there another way where I can save the content in a table?
My problem can also be solved by proposal #2. Is it possible for me to manipulate the output from the stored procedure with something like:
Execute [myProcedure] where variable1 > 100

SQL Server : inserting multi-table output from stored procedure into separate tables

I have a stored procedure that returns several tables (currently consumed by a web service). I now need to insert that output into a SQL Server database.
I can insert a single table if I know the existing definition via
INSERT INTO #temp
EXEC ('myProcSQlhere')
But for this I need to know the definition of the output table in advance.
SELECT *
INTO #temp
FROM OPENQUERY(SERVERNAME, 'EXEC myProcSQlhere')
Which gets around knowing the table definition, but only allows entry into one table. So how do I insert data into multiple tables?
A stored procedure can only return one value. What your stored procedure does is performing multiple selects and so giving multiple result sets. This works for systems that can consume multiple result sets but you are stuck in SQL-Server.
Check if the stored proc doesn't already call independent stored procedures for each result set and if so, call those. Or use the definition of the stored procedure and call each query used within sequentially.
If you have no access to the system providing the stored procedure, you will need to implement a CLR stored procedure and fetch each result set to store the results into different tables.

SSIS ole db source using stored procedure cannot retrieve meta-data for columns

I'm using a stored procedure for ole db source in SSIS. It always pops out error when I click on "Columns" panel. However, the same query executes successfully in SSMS and in "Build Query..." window.
In the stored proc., no temp table but table variable is used. I did a lot research online but all similar cases seem to be caused by temp tables.
For SSIS to use a stored proc as a datasource, the stored proc has to result in only one possible resultset structure.
You can let SSIS see the meta data for a complex stored proc by starting with something like this:
IF 1=2
SELECT
1 AS MyIntColumn
, 1.11 AS MyMoneyColumn
, 'test' AS MyVarcharColumn
ELSE
BEGIN
...
The thing is to put in a SELECT statement in the 1=2 block that would return the Columns and the DataTypes that your stored procedure will ultimately return. That way SSIS can look at that first statement and know what columns and datatypes to expect from the stored proc. That's all it needs. It doesn't need "real" data in that first select. It can be all dummy values that don't even come from a table.

Running synchronous commands to between two sql servers

I'm running a stored procedure on server1 from my application. The stored procedure does a bunch of stuff and populate a table on server2 with the result from the procedure.
I'm using linked server to accomplish this.
When the stored procedure is done running the application continues and tries to do some manipulation of the result from the stored procedure.
My problem is that the results from the stored procedure has not been completely inserted into the tables yet, so the manipulation of the tables fails.
So my question is. Is it possible to ensure the insert into on the linked server is done synchronous? I would like to have the stored procedure not return until the tables on the linked server actually is done.
You can use an output parameter of the first procedure. When the table is create on the second server the output parameter value will be return to your application and indicates the operation is ready.
If the things are difficult then this you can try setting a different isolation level of your store procedure:
http://msdn.microsoft.com/en-us/library/ms173763.aspx
I found the reason for this strange behavior. There was a line of code in my stored procedure added during debug that did a select on a temporary mem table before the data in the same table was written to the linked server.
When the select statement was run, the control was given back to my application and at the same time the stored procedure continued running. I guess the stored procedure was running synchronously from the start.

Creating entities from stored procedures which have dynamic sql

I have a stored procedure which uses a couple of tables and creates a cross-tab result set. For creating the cross-tab result set I am using CASE statements which are dynamically generated on basis of records in a table.
Is it possible to generate an entity from this SP using ADO.NET Entity framework? Cuz each time I try Get Column Information for the particular SP, it says that The selected stored procedure returns no columns.
Any help would be appreciated.
A member of my team recently encountered something like this, where a stored procedure was generating all kinds of dynamic SQL and returning calculated columns so the data context didn't know what to make of it. I haven't tried it myself yet, but this was the solution he claimed worked:
The solution is simply to put the line
“SET FMTONLY OFF;” into the proc.
This allows the Data Context to
actually generate the return class.
This works in this case, only because
the proc is doing nothing but querying
data.
Full details here:
http://tonesdotnetblog.wordpress.com/2010/06/02/solution-my-generated-linq-to-sql-stored-procedure-returns-an-int-when-it-should-return-a-table/
You only need the “SET FMTONLY OFF” in
the proc long enough to generate the
class. You can then comment it out.