| |

VerySource

 Forgot password?
 Register
Search
View: 1923|Reply: 10

Stored procedure output parameter problem Why is the output always NULL?

[Copy link]

3

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-2-6 01:30:01
| Show all posts |Read mode
The stored procedure code is as follows:
CREATE PROCEDURE UP_Production_Authenticate
@sOrdNo nvarchar (100),
@sResult nvarchar (30) output
AS
if not exists (select * from tblContract where sOrdNo = @ sOrdNo)
begin
set @ sResult = 'yes'
end
else
begin
set @ sResult = 'no'
end
return

After execution, the @sResult value is NULL anyway. The code tracked by the tracker is:
declare @ P1 bit
set @ P1 = NULL
exec sp_executesql N'UP_Production_Authenticate ', N' @ sOrdNo nvarchar (100), @ sResult bit output ', @sOrdNo = N'9042', @sResult = @ P1 output
select @ P1

Why does the system automatically assign a NULL value to @ P1?
Reply

Use magic Report

0

Threads

49

Posts

35.00

Credits

Newbie

Rank: 1

Credits
35.00

 China

Post time: 2020-3-24 18:45:01
| Show all posts
CREATE PROCEDURE UP_Production_Authenticate
@sOrdNo nvarchar (100),
@sResult nvarchar (30) output
AS
set @sResult = ''
if not exists (select * from tblContract where sOrdNo = @ sOrdNo)
begin
set @ sResult = 'yes'
end
else
begin
set @ sResult = 'no'
end
return


--Try this?
Reply

Use magic Report

0

Threads

88

Posts

55.00

Credits

Newbie

Rank: 1

Credits
55.00

 China

Post time: 2020-3-24 19:00:01
| Show all posts
CREATE PROCEDURE UP_Production_Authenticate
@sOrdNo nvarchar (100),
@sResult nvarchar (30) output
AS
set @sResult = '' --- give an initial value first
if not exists (select * from tblContract where sOrdNo = @ sOrdNo)
begin
set @ sResult = 'yes'
end
else
begin
set @ sResult = 'no'
end
return
Reply

Use magic Report

3

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-3-26 11:45:01
| Show all posts
Useless, given the initial value, the execution tracking is still the same
declare @ P1 nvarchar (30)
set @ P1 = NULL
exec sp_executesql N'UP_Production_Authenticate ', N' @ sOrdNo nvarchar (100), @sSubOrdNo nvarchar (100), @sStaff nvarchar (100), @sResult nvarchar (30) output ', @sOrdNo = N'9042', @sSubOrdNo = N'14-002 ', @sStaff = N'lily', @sResult = @ P1 output
select @ P1


I have another stored procedure
CREATE PROCEDURE UP_frmMessage_Read
@sStaff nvarchar (10),
@sResult nvarchar (10) output
AS
  select iXh, sNo, sReceipt, sMessage, sSender, dNow from tblMessage where sReceipt = @ sStaff and sStatus <> 3
if @@ rowcount = 0
 set @ sResult = 'No'
else
 set @ sResult = 'Yes'

Tracked out is

declare @ P1 nvarchar (10)
set @ P1 = N'No '
exec UP_frmMessage_Read @sStaff = N'lily ', @sResult = @ P1 output
select @ P1

This does not give an initial value, why is the set @ P1 = N'No ', not a NULL value?
Reply

Use magic Report

0

Threads

28

Posts

25.00

Credits

Newbie

Rank: 1

Credits
25.00

 China

Post time: 2020-3-28 15:30:01
| Show all posts
Write it like this during execution
declare @ P1 nvarchar (30)
set @ P1 = NULL
exec sp_executesql N'UP_Production_Authenticate ', N' @ sOrdNo nvarchar (100), @ sResult nvarchar (30) output ', @sOrdNo = N'9042', @sResult = @ P1 output
select @ P1
Reply

Use magic Report

3

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-3-28 22:45:01
| Show all posts
What's upstairs? I didn't understand
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-3-30 13:30:01
| Show all posts
declare @ p1 varchar (1024)
SELECT @ p1 = ''
exec UP_Production_Authenticate N'9042 ', @ p1 output
select @ p1

There may be a problem with the calling method.
Reply

Use magic Report

0

Threads

40

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-3-30 18:00:01
| Show all posts
CREATE PROCEDURE UP_Production_Authenticate
@sOrdNo nvarchar (100),
@sResult nvarchar (30) output
AS
if not exists (select * from tblContract where sOrdNo = @ sOrdNo)
set @ sResult = 'yes'

else
set @ sResult = 'no'

return
Reply

Use magic Report

3

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-4-1 02:00:01
| Show all posts
wgfxman: You may understand it wrong, that section is not the code that is called, the code that is tracked out, the call is the stored procedure that is directly called.

hiqwolfzzh: That Begin..end can also be added, the same effect without adding it. I tried.
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-4-1 12:15:02
| Show all posts
The code is correct. If it returns NULL, it is only possible that you called wrongly or that if was not executed at all.
Actual situation. Look carefully for your actual stored procedure.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list