Given a stored procedure like the one shown below:
CREATE PROCEDURE [dbo].[GetBusinessUnitSysNameAndGroupNames]
#ModelAfter varchar(100),
#BusinessUnitSystemName varchar(100) OUT,
#GroupsName varchar(4000) OUT
AS
BEGIN
if (#ModelAfter = 'Corporate')
BEGIN
SET #GroupsName = 'Admins'
SET #BusinessUnitSystemName = 'AcmeOutdoors'
END
else if (#ModelAfter = 'Retailers')
BEGIN
SET #GroupsName = 'Sellers'
SET #BusinessUnitSystemName = 'AcmeShoppers'
END
END
When I run from the SQL Studio command line:
EXEC [dbo].[GetBusinessUnitSysNameAndGroupNames] '~ModelAfter~', 'acmeoutdoors', 'admins'
I just get a result in the message panel like Command(s) completed successfully. But what I would like to see the actual result, not just a success message. Something like shown below(which doesn't work, just my idea).
DECLARE #Result varchar(max)
SET #Result = EXEC [dbo].[GetBusinessUnitSysNameAndGroupNames] '~ModelAfter~', 'acmeoutdoors', 'admins'
PRINT #Result
Returning Data by Using OUTPUT Parameters
If you specify the OUTPUT keyword for a parameter in the procedure
definition, the stored procedure can return the current value of the
parameter to the calling program when the stored procedure exits. To
save the value of the parameter in a variable that can be used in the
calling program, the calling program must use the OUTPUT keyword when
executing the stored procedure.
DECLARE #Result1 varchar(max), #Result2, varchar(max)
EXEC [dbo].[GetBusinessUnitSysNameAndGroupNames] 'Corporate', #Result1 OUT, #Result2 OUT
PRINT #Result1
PRINT #Result2
Related
I would like to use a variable in my INSERT command. This variable includes result value from a storedprocedure:
declare #File as varbinary(max)
exec #File=[dbo].[MySp]
but If I use #File in an INSERT command, another value of is written in table
insert into [dbo].[Plots] values ('test', #File)
My Stored Procedure:
CREATE PROCEDURE [MySp]
AS
BEGIN
EXEC sp_execute_external_script #language = N'R'
, #script = N'_RCODE_'
, #input_data_1 = N'_INPUT_QUERY_'
,#output_data_1=N'OutputDataset'
--- Edit this line to handle the output data frame.
WITH RESULT SETS (([plot] VARBINARY(max)));
END;
Your using of Stored Procedure is wrong.
There is a recordset on first screenshot, but after execution exec #File=[dbo].[MySp] you don't have the recordset in variable #File.
You got
#return_status
in #File
#return_status Is an optional integer variable that stores the return
status of a module. This variable must be declared in the batch,
stored procedure, or function before it is used in an EXECUTE
statement.
The right query can be like this:
declare #File as varbinary(max)
DECLARE #Table TABLE
(
plot VARBINARY(MAX)
)
INSERT #Table
exec [dbo].[MySp]
SELECT #File = MAX(plot)
FROM #Table
insert into [dbo].[Plots] values ('test', #File)
Your EXEC call is getting the result code of the SP, which is 0 for success, I suppose, in the absence of an explicit return statement.
See this answer for more details on how to capture actual data from your SP: https://stackoverflow.com/a/3963991/16777
I have this stored procedure:
ALTER PROCEDURE spCertificationType
#result nvarchar(15) output,
#mode int
AS
BEGIN
if #mode = 1
begin
exec spGeneratedID 2, #result output
print #result
end
END
but when I tried to execute it,it has this error
The formal parameter “#mode” was not declared as an OUTPUT parameter, but the actual parameter passed in requested output.
I tried to set #mode as output like this:
ALTER PROCEDURE spCertificationType
#result nvarchar(15) output,
#mode int output
AS
BEGIN
if #mode = 1
begin
exec spGeneratedID 2, #result output
print #result
end
END
but the it returns a null value.
Any fix for this? Thanks in advance.
the sequence of parameter in store procedure is that first use input parameter then use output parameter:-
you can see this link for more knowledge of store procedure:-
http://www.codeproject.com/Articles/126898/Sql-Server-How-To-Write-a-Stored-Procedure-in-SQL
ALTER PROCEDURE spCertificationType
#mode int,
#result nvarchar(15) output
AS
BEGIN
if #mode = 1
begin
exec spGeneratedID 2, #result output
print #result
end
END
I fixed this error a different way.
I had removed the OUTPUT statement after a parameter in my SP.
I then got the error.
I then went back to the SQLDataSource Configure Data Source wizard and went through the steps again. I discovered that changing the SP made the wizard delete the settings associated with the parameter that used to have OUTPUT after it.
In my case, I had declared the OUTPUT parameter last but was also getting a similar exception.
I moved the default parameters (like #var FLOAT = null) to the end after the output parameter then it started working.
create procedure [dbo].[basic_und6] (#name varchar(500), #email varchar(500), #result int out)
as
begin
Select #result=COUNT(*) from tbl_Students
where Firstname= #name and Email=#email
print #result
end
Given below is the query which I am running to execute the stored procedure
**declare #result1 int
execute basic_und6 Amit,'amit#abc.com',#result1
print #result1**
I am getting result as 1.
But according to my understanding, it should have displayed two 1's
First 1 is from the stored procedure print and second 1 from the query which had print statement.
Modify your execute statement to this :
declare #result1 int
execute basic_und6 Amit,'amit#abc.com', #result1 OUTPUT
print #result1
This is because, you haven't specified a variable where the output value must be returned to. The calling statement must do this by explicitly using the OUTPUT keyword. More info here
If you specify the OUTPUT keyword for a parameter in the procedure definition, the stored procedure can return the current value of the parameter to the calling program when the stored procedure exits. To save the value of the parameter in a variable that can be used in the calling program, the calling program must use the OUTPUT keyword when executing the stored procedure.
No it will show only one 1
when we select a value into a variable the row count will not be shown
I am trying to execute a procedure(A) inside another procedure(B) and I want to store the value returned from the procedure A in a variable called #LogId.
after executing Procedure A
exec TES_usp_getnextlogid
it returns something like
NewLogId = xxxx
I tried doing the following in procedure B.
declare #LogId int
exec #LogId = TES_usp_getnextlogid
my question is, why doesn't #LogId holds the value which is returned from TES_usp_getnextlogid Procedure?
Thanks.
use output parameter for this, return value should only be used for returning Success/Faliuer status.
CREATE PROCEDURE My_Outter_Proc2
AS
BEGIN
DECLARE #NextID INT;
EXEC TES_usp_getnextlogid #NextID OUTPUT
EXEC My_Inner_Proc #NextID
END
Here is my sample:
ALTER PROCEDURE EmpFirstName
#myParam int,
#empFName varchar(20) output
AS
BEGIN
SET NOCOUNT ON;
SELECT #empFName = empfname
FROM FE_QEMP
WHERE empno = #myParam
END
GO
myParam is the input and empFName will carry the output, so the procedure
should only take 1 parameter since empFName is the output, but in my case
i get this error:
Msg 201, Level 16, State 4, Procedure
EmpFirstName, Line 0 Procedure or
function 'EmpFirstName' expects
parameter '#empFName', which was not
supplied.
This is the way i called the procedure:
DECLARE #returnValue varchar(20)
EXEC #returnValue = EmpFirstName 10
SELECT 'Return Value ' = #returnValue
Return values and output parameters are two different things. If you want to call it with an output parameter, you'd do it like this:
EXEC EmpFirstName 10, #returnValue OUTPUT
SELECT 'Return Value ' + #returnValue
If you want to call it in the manner that you described in your example, then you need to alter the stored procedure to state RETURNS VARCHAR(20) and remove the output parameter. To return a value, you have to explicitly call return. In your example, you'd declare a variable, assign it in the select statement, then call return #varName.
Thanks. My aha moment came with this post. Did not realise that output parameters need to be qualified with the "output" identifier too when executed, not just in the procedure!
Here are my test workings for my fellow sql server noobs. I am using sqlcmd with sql server 2005.
The stored procedure:
/* :r procTest.sql */
if exists (select name from sysobjects where name="procTest" and type="P")
drop procedure procTest;
go
create procedure procTest
/* Test stored procedure elements. */
(#i_pt_varchar varchar(20),
#o_pt_varchar varchar(20) output)
as
begin
print "procTest";
set #o_pt_varchar = "string coming out";
print "#i_pt_varchar " + #i_pt_varchar;
print "#o_pt_varchar " + #o_pt_varchar;
return (0);
end
go
The test call:
/* :r procTest.test.sql */
declare #returnFlag int;
declare #i_varchar varchar(20);
declare #o_varchar varchar(20);
set #i_varchar = "string going in";
set #o_varchar = null;
execute #returnFlag = procTest #i_varchar, #o_varchar output
print "#returnFlag " + cast(#returnFlag as varchar(20));
print "after call";
print "#i_varchar " + #i_varchar;
print "#o_varchar " + #o_varchar;
go