Debugging stored procedure in sql server [duplicate] - sql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Debugging Stored Procedure in SQL Server 2008
I wanted to debug the stored procedure in sql server. I tried with break points, but that didn't work for me. I searched in stack over flow even but din't get proper answer. My procedure is as follows,
USE [Practice]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[selectsample]
#input varchar(100),
#output int output
as
begin
select #output=id
from people where name=#input order by id desc
end
What is the best way to debug this stored procedure?

Try adding top 1 to your query. You have one output patameter but your query can return multiple values (what if there are more people that meet name=#input criteria)

Related

Passing query string through variable value fails [duplicate]

This question already has answers here:
Use dynamic table name in sql server query
(2 answers)
Closed 7 months ago.
Using Microsoft SQL Server 2015
This works
create procedure Checking1(#SQLQueryString nvarchar(max))
as
begin
select * from MyTable
end
This is not working
create procedure Checking1(#SQLQueryString nvarchar(max))
as
begin
#SQLQueryString
end;
What am I doing wrong?
You need to tell SQL Server to execute your dynamic-SQL
https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-executesql-transact-sql?view=sql-server-ver16
or
https://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/

return 60 column table from one stored procedure into another [duplicate]

This question already has answers here:
How do I use the results of a stored procedure from within another?
(4 answers)
Closed 3 years ago.
ALTER PROCEDURE [dbo].[outer_sp]
#PrmIdUsAD nchar(128)
,#PrmAnnee smallint
,#PrmMois tinyint
,#PrmIdAct tinyint
AS
BEGIN
DECLARE #DateMin datetime, #DateMax datetime, #idAgence nchar(2), #nomAgence nchar(50);
EXEC inner_sp( #DateMin, #DateMax, #idAgence, #nomAgence)
...
END
inner_sp ends up by (table has 60 columns)
SELECT
ISNULL(nomAgence, '') AS nomAgence,
ISNULL(chemin, '') AS chemin,
...
FROM #temp_arbo_of_4
ORDER BY chemin
END
How do I retrieve this output table in the calling stored procedure ?
I know some variables are not set, but this is outside the scope of question.
I assume you want to do further processing on the results? If not, the output will just be returned anyway.
If you do want to process further, create a temp table with the right columns
CREATE TABLE #temp
(
nomAgence {type},
chemin {type},
...
)
then use select into
SELECT INTO #temp
EXEC SelectOFArbo( #DateMin, #DateMax, #idAgence, #nomAgence);
Note that SQL Server wont let you nest these calls, so SelectOfArbo could not SELECT-INSERT from another proc

Using results of one stored procedure in another stored procedure - SQL Server

Is there any way to use the results of one stored procedure in another stored procedure without using a table variable or temp table? The logic is as follows:
IF (#example = 1)
BEGIN
DECLARE #temp TABLE (Id INT);
INSERT INTO #temp
EXEC [Procedure1] #ItemId = #StockId
set #Cost = (select top 1 id from #temp)
Ideally i would like to know if there is a way to do this without having to use a temp table. Looked around online but can't find anything that works. Any advice would be great.
In general, if you want to use user-defined code in a SELECT, then it is better to phrase the code as a user-defined function rather than a user-defined procedure.
That is, procedures should be used for their side effects and functions should be used for their return values.
That said, you can use openquery (documented here) to run an exec on a linked server. The linked server can be the server you are running on.

Return Table from Store Procedure - SQL Server [duplicate]

This question already has answers here:
SQL server stored procedure return a table
(9 answers)
Closed 4 years ago.
I am trying to get table data which is being returned from Stored procedure.
Create procedure Proc1
as
begin
Select * from Employee
End
Go
I want to use this as :
Select * from Departments D
inner join (Exec proc1) p
on D.Emp_id = p.Emp_id
Please suggest.
Thanks
Short version: you can't. Stored procedures cannot be used as a source of data in a query.
The best you can do is put the results of the stored procedure into a (temporary) table and then query that:
create table #sprocResult (
-- define columns that match the results of the sproc.
-- You should also define a PK, and possibly other indexes
)
insert into #sprocResult exec proc1
(You can use a table valued variable as well.)

Save result of stored procedure in a Table variable [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to SELECT * INTO [temp table] FROM [stored procedure]
I have a nested stored procedure call
In one of the stored procedures I want to save the result into a table variable likt this :
INSERT INTO #myTable
EXEC sp_myStoredProcedure
however, because the proc. is nested the following error occurs :
An INSERT EXEC statement cannot be nested
The procedure must be called from another procedure, changing this is not an option.
I wanted to try to use an output parameter but it still has to be set with a Insert into statement.
What are other options to save the data that is retrieved from the call of a Stored Procedure into a variable ?
Table variables are not visible to the calling procedure in the case of nested procs. The following is legal with #temp tables.
http://databases.aspfaq.com/database/should-i-use-a-temp-table-or-a-table-variable.html
http://support.microsoft.com/kb/305977/en-us
Not tested but maybe do this:
DECLARE #insertedProc as varchar(300)
SET #insertedProc = 'INSERT INTO ' + #myTable + ' sp_myStoredProcedure'
EXEC #insertedProc
Make sure you have defined what #myTable is before hand.