SSRS Report Using Stored Procedure - sql-server

I am working with an SSRS Report that uses a stored procedure.
The stored procedure [after the Use ... Set ANSI NULLS On] starts with ALTER PROCEDURE ...
While I can understand the SQL in a stored procedure, I have never used one in an SSRS Report [I only use 'straight' SQL statements].
When I use SQL as my Dataset, I can copy that SQL into SSMS and run it and see the data it returns.
With this stored procedure, how do I execute it in SSMS to see the data it returns? The stored procedure has a sample 'EXEC ...' statement with all the parameters populated ... but when I run that - no data is returned.
The SSRS report runs fine, but I want to be able to work with the stored procedure in SSMS and see the data it is returning. My goal is to be able to run the stored procedure in SSMS and then tweak it.
How do I work with this stored procedure in SSMS so I can look at the output?

If you just want to execute the procedure in SSMS, locate it in the object browser ([DatabaseName]/Programmability/Stored Procedures). RIght-click the procedure and select 'Execute Stored Procedure'
Fill in the parameters and click OK and a script will be generated to run the procedure.
It's a bit overkill but at least everything is there and you can run it whenever you like.
If you want to edit the proc, right-click and choose modify, a new script will be created (the ALTER PROCEDURE script you mentioned). Make changes as required, run the script and that will modify the procedure, then execute the procedure to see the results.
Of course it would be safer to make a copy and edit that, you can also just run the body of the stored proc by commenting out the ALTER PROCEDURE statement until you are happy with it but you may have to declare and variables that are normally passed in as parameters.

The stored procedure [after the Use ... Set ANSI NULLS On] starts with
ALTER PROCEDURE ...
That's the Alter Procedure script. Use this to edit a stored procedure.
In other words, edit the SQL code you want to optimize, then run the whole script to save the changes.
How do I work with this stored procedure in SSMS so I can look at the
output?
In SSMS use the syntax for stored procedures:
EXEC myspname paramter1, parameter2, param...
Where parameter1, parameter2, etc. are the parameters described in the ALTER Procedure script, directly after the ALTER PROCEDURE myspname. Parameters are preceded by the # symbol.
As you type-in the EXEC procedure command pop-up hints should appear describing the parameter.
Without knowing the code to the stored procedure, it could be doing any number of things based on what is passed to it by parameter. A stored procedure can do DDL and DML queries, and does not necessarily have to select anything at all for output.

Related

`go`'s in stored procedures in Sybase 16

I have the following stored procedure in Sybase 16,
create or replace procedure ... as
...
drop table tempdb..koppelingen
go
declare
vre_cursor cursor for
...
Then I see: declare cursor must be the only statement in a query batch.
If I skip the go, I can create the stored procedure.
If I execute the code of the stored procedure by hand by selecting it and execute, I have to use the go.
So what happens in a stored procedure? Does it insert the go's by itself? But then I do not understand the error message of declare cursor above.
go is not an ASE command.
go is a client-side command that tells the client application (eg, isql) that a batch of SQL can now be sent to ASE. In the case of the create or replace procedure ... the go tells the client application (eg, isql) that you've completed the stored proc definition and it can now be submitted to ASE for parsing & compiling.
declare cursor must be in a batch of SQL by itself (ie, declare ...\ngo) if being run from a client application (eg, isql); when inside a stored proc the declare cursor command can be grouped with other follow-on commands (eg, open, fetch).

Force a stored procedure to fail when a column not exits?

Sometimes it happens that I have some errors in stored procedure body, e.g.
select invalidColumn from validTable
When I try to create/alter stored procedure, it works and I can see the error only when I try to execute the stored procedure.
Is it possible to force schema check when I create or alter?

Get the text of a stored procedure after rename in SQL Server

I encountered a strange problem today.... I did an sp_helptext on a stored procedure but the stored procedure name in the CREATE PROCEDURE statement (in the result) did not correspond to the name I used in the sp_helptext statement (see screenshot below). I tried selecting from the sys.all_sql_modules and also OBJECT_DEFINITION but it gave the same result.
In my example below I used InvIRCode_GetByInvIRID but the result gave me InvIRCode_GetListByInvIRID
I did a bit of research and it seems it is a known SQL Server bug and happens when a stored procedure was renamed.
My question is this: How can I get the correct source code for a stored procedure?
The interesting thing is that when I right-click on the sp in the Object Explorer of SQL Server Management Studio and select to script the procedure to a new window, it does give the correct code.
Note this warning message is returned by sp_rename:
Caution: Changing any part of an object name could break scripts and stored procedures.
The text in the catalog views is not changed by sp_rename. The proc name will be corrected when you script the proc via SSMS or SMO. You can alter (or drop/create) modules using that script to fix the text in the catalog views after a rename.

Debugging SQL Server 2012 stored procedure - can't step into code line by line

Using: SQL Server 2012
In my list of stored procedures, I find one, right-click then click "Modify", then try to debug the stored procedure. I press F11 to step into the script but once I get to the ALTER PROCEDURE keyword, the rest of the script is highlighted and, I presume, "stepped into" simultaneously and I cannot go line by line. How can I execute the stored procedure with parameter values and also step through it so that I can see how the code behaves at runtime?
I figured it out: You can't go through the Object Explorer to debug the procedure; rather, you have to write a script that executes the procedure with parameter values like so:
exec procedure_name param1, param2;
Then the debugging mode works.

SQL Server stored procedure calling from another stored procedure

In SQL Server 2000, from one stored procedure (Master stored procedure) I am calling a series of other procedures.
The nth procedure is throwing a column not found error, but the nth procedure got executed just fine if I run it separately.
If I place this nth procedure at any place in the master stored procedure it behaves in the same way.
Can anyone please help me to find solution to this problem?
If the procedure is doing any type of DDL against a table, the same transaction cannot correctly reference the table. For example, if you have a stored procedure that runs a SELECT... INTO and then another statement that runs an ALTER TABLE... ADD columns, you will run into this issue.

Resources