Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 STored Proc OUTPUT

Author  Topic 

JohnDW
Starting Member

45 Posts

Posted - 2014-10-05 : 10:28:36
Hello,

I Want to have an output from a stored proc.
The output must be displayed in a msgbox in vb.net.
If I work with this code for the stored proc, it works:
ALTER PROCEDURE [dbo].[spTelling]
@scannummer Nvarchar(13)
AS
BEGIN
SET NOCOUNT ON;

declare @productid int, @premium int,@prem int

select @premium = productid From Product Where scannummer = @scannummer

if count(@premium) = 1
begin
select @prem = productid From TELLING Where ProductId = @premium
if count(@prem) = 1

Update TELLING set Tal = Tal + 1 where ProductId = @prem
else
INSERT INTO Telling (ProductID, Tal, Date) VALUES (@premium,1,getdate())
end
END

If I put an output to the sp then it doesn't work:

ALTER PROCEDURE [dbo].[spTelling]

@scannummer Nvarchar(13)

AS
BEGIN
SET NOCOUNT ON;
declare @productid int, @premium int,@prem int

select @premium = productid From Product Where scannummer = @scannummer

if count(@premium) = 1
begin
select @prem = productid From TELLING Where ProductId = @premium
if count(@prem) = 1

Update TELLING set Tal = Tal + 1 where ProductId = @prem
else
INSERT INTO Telling (ProductID, Tal, Date) VALUES (@premium,1,getdate())
end

else
Declare @outscan nvarchar(13)
EXEC
@outscan= @scannummer OUTPUT
END

That's the first step.
Can someone help me with that?

The other thing is to do something with the output in my vb.net code.
If the output is the scannummer, that means that the scannummer is not found in table Product.
It must then let popup a msgbox with the scannummer in it.

the code in vb.net I have is the following
Dim CMD1 As New SqlCommand(SQL, MyConnection)
CMD1.CommandType = CommandType.StoredProcedure

CMD1.Parameters.AddWithValue("@Scannummer", vscannummer)
'
CMD1.Parameters("@outscan").Direction = ParameterDirection.Output
MyConnection.Open()
Id = CInt(CMD1.ExecuteScalar())
MyConnection.Close()

MsgBox("@outscan")

Don't now if that's correct?

Please give help.

Txs

if you do not try, it will not work

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-10-05 : 14:27:01
you must declare your output parameter in the procedure signature like

ALTER PROCEDURE [dbo].[spTelling]

@scannummer Nvarchar(1),
@outscan nvarchar(13) OUT

then change your VB where you call the proc to add the output parameter and map it to a VB variable
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-10-05 : 14:34:00
[code]
ALTER PROCEDURE [dbo].[spTelling]
(
@scannummer Nvarchar(13) OUT
)
AS[/code]


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-10-05 : 15:16:49
Please post your modified Stored Procedure definition. It sounds like the proc can exit without setting the output parameter
Go to Top of Page

JohnDW
Starting Member

45 Posts

Posted - 2014-10-05 : 15:23:45
Txs,
I've put the SP above.
Don't now where it turns wrong in the SP.

John

if you do not try, it will not work
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-10-05 : 16:15:27
at then end you have:

if (count(@premium) = NULL)
Begin
select @outscan = @scannummer
return @outscan
END

what do you mean by count(@premium)? AFAIK count() is an aggregate function that needs to be in a query somewhere not in an if statement. I believe that your test here is never true.

alSo @outscan will be null (DBNULL in VB) if count(@premium) is not null. also, you shouldn't have that return statement. Output parameters are separate from a return value (which must be an integer, btw)
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-10-05 : 16:18:03
forgot to add...you can never test anything for '= null' . you must test 'is null' or 'is not null'

in sql, null <> null
Go to Top of Page

JohnDW
Starting Member

45 Posts

Posted - 2014-10-06 : 00:48:34
Txs gbritton.

The code in the sp almost works.


ALTER PROCEDURE [dbo].[spTelling]
-- Add the parameters for the stored procedure here
@scannummer Nvarchar(13),
@outscan Nvarchar(13) output

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
declare @productid int, @unique int, @un int

select @productid = (productid) From Product Where scannummer = @scannummer

select @unique = count(productid) from Product Where scannummer = @scannummer

if (@unique > 0)
begin
select @un = count(productid) from Telling where ProductId = @productid
if (@un > 0)
begin
Update TELLING set Tal = Tal + 1 where ProductId = @productid
end

if (@un = 0)
begin
INSERT INTO Telling (ProductID, Tal, Date) VALUES (@productid,1,getdate())
end
end
if (@unique = 0)
Begin

set @outscan = @scannummer
return @outscan
EXEC dbo.spTelling @scannummer, @outscan OUTPUT
END

END


the error sql server gives:
The conversion of the nvarchar value '9782321003311' overflowed an int column.
The 'spTelling' procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead.

The problem:

if (@un = 0)
begin
INSERT INTO Telling (ProductID, Tal, Date) VALUES (@productid,1,getdate())
end
end


I'm trying to solve that.
But till now , no success.
Can you help?
Txs.

if you do not try, it will not work
Go to Top of Page

Orsla
Starting Member

5 Posts

Posted - 2014-10-06 : 02:24:42
I think that you are executing your stored procedure within your stored procedure and this is creating a loop

EXEC dbo.spTelling @scannummer, @outscan OUTPUT
END

END
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-10-06 : 07:59:24
you can't do this:

return @outscan

@outscan is not an integer
Go to Top of Page

JohnDW
Starting Member

45 Posts

Posted - 2014-10-06 : 15:13:37
[code]
ALTER PROCEDURE [dbo].[spTelling]
@scannummer Nvarchar(13),
@outscan Nvarchar(13) output
AS
BEGIN
SET NOCOUNT ON;
declare @productid int, @unique int, @un int
select @productid = (select(productid) From Product Where scannummer = @scannummer)
select @unique = (select count(productid) from Product Where scannummer = @scannummer)

Begin
if (@unique > 0)
begin
select @un = (select count(ProductId) from Telling where ProductId = @productid)

if (@un > 0)
begin
Update TELLING set Tal = Tal + 1 where ProductId = @productid
end
else
begin
INSERT INTO Telling (ProductID, Tal, Date) VALUES (@productid,1,getdate())
end
end
else

Begin
set @outscan = @scannummer
return @outscan
END
END
End
[/code]
message from management studio:
Procedure or function 'spTelling' expects parameter '@scannummer', which was not supplied.

Question:
If the scannummer is not found in the table,
how can I get @scannummer from the sp in my vb.net application.
@scannummer is a string.




if you do not try, it will not work
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-10-06 : 15:38:08
quote:
Originally posted by JohnDW


message from management studio:
Procedure or function 'spTelling' expects parameter '@scannummer', which was not supplied.

Question:
If the scannummer is not found in the table,
how can I get @scannummer from the sp in my vb.net application.
@scannummer is a string.




You need to pass the input parameter a value:
DECLARE @scannummer nvarchar(13), @outscan nvarchar(13)
SET @scannummer = 'somevalue'

EXEC dbo.spTelling @scannummer, @outscan = @outscan OUTPUT

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-10-07 : 06:27:51
What is your objective with this procedure?
Why does it call itself at the end?



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-10-07 : 06:30:46
This part will NEVER be true
if (@unique = 0)
Begin

set @outscan = @scannummer
return @outscan
EXEC dbo.spTelling @scannummer, @outscan OUTPUT
END
unless ProductID column allows for NULL, but I doubt that since it seem to be the primary key column in the Product table.

Tell us what you want to achieve!


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2014-10-07 : 06:45:44
Never mind... I believe this piece of code is what you need.

1. You get the ProductID associated with the ScanNummer.
2. Increment the Tal column with 1 if ProductID already exists, or add a new row for ProductID with Tal initialized to 1.
3. Return the incremented Tal value to the application.

ALTER PROCEDURE	dbo.spTelling
(
@ScanNummer NVARCHAR(13),
@CurrentTal INT OUT
)
AS

-- Prevent unwanted resulsets back to client
SET NOCOUNT ON;

-- Prepare output value
DECLARE @Return TABLE
(
Value INT NOT NULL
);

-- Increment the Telling Tal
MERGE dbo.Telling AS tgt
USING (
SELECT TOP(1) ProductID,
1 AS Tal,
GETDATE() AS [Date]
FROM dbo.Product
WHERE ScanNummer = @ScanNummer
) AS src ON src.ProductID = tgt.ProductID
WHEN MATCHED
THEN UPDATE
SET tgt.Tal += src.Tal
WHEN NOT MATCHED BY TARGET
THEN INSERT (
ProductID,
Tal,
[Date]
)
(
src.ProductID,
src.Tal,
src.[Date]
)
OUTPUT inserted.Tal
INTO @Return
(
Value
);

-- Return the current Tal for ScanNummer
SELECT @CurrentTal = Value
FROM @Return;



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

JohnDW
Starting Member

45 Posts

Posted - 2014-10-10 : 08:16:11
Sorry to Reply after a few days (work to do)

the code I Have:

ALTER PROCEDURE dbo.spTelling
(
@ScanNummer NVARCHAR(13),
@CurrentTal INT OUT
)
AS
-- Prevent unwanted resulsets back to client
SET NOCOUNT ON;
-- Prepare output value
DECLARE @Return TABLE
(
Value INT NOT NULL
);
-- Increment the Telling Tal
MERGE dbo.Telling AS tgt
USING (
SELECT TOP(1) ProductID,
1 AS Tal,
GETDATE() AS [Date]
FROM dbo.Product
WHERE ScanNummer = @ScanNummer
) AS src ON src.ProductID = tgt.ProductID
WHEN MATCHED
THEN UPDATE
SET tgt.Tal += src.Tal
WHEN NOT MATCHED BY TARGET
THEN INSERT (
ProductID,
Tal,
[Date]
) values
(
src.ProductID,
src.Tal,
src.[Date]
)
OUTPUT inserted.Tal
INTO @Return
(
Value
);

SELECT

-- If @Return(Value) is null (means that there's no scannummer in the table -- product, then return @scannummer
-- else (if @Return has a Value: end ( return nothing )
@CurrentTal = ISNULL(Value, CAST(@Scannummer AS INT))



FROM @Return;


The code works, but it suppose to give only an output
if the Value in table @Return is null or 0.
This means that the scannummer in table product isn't found.
The output should then be @scannummer.
Is this possible?

Hope this is clear.

Txs for the support.


if you do not try, it will not work
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-10-10 : 08:49:43
sure! Since @CurrentTal is your only output parameter, set it to NULL if @scannnummer isn't found, otherwise set it to the integer that represents @scannnummer. (you can't set it to @scannummer since the types are different)
Go to Top of Page

JohnDW
Starting Member

45 Posts

Posted - 2014-10-10 : 10:01:24
Txs,
but how to do that:

if Value is null than
@CurrentTal = @scannummer
else
END
(nothing must to output)

if you do not try, it will not work
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-10-10 : 10:36:30
You can't do that! @CurrentTal is an integer. @scannummer is NCARCHAR(13). You want something like


SET @CurrentTal = (
SELECT TOP (1) value
FROM @Return
WHERE value IS NOT NULL
ORDER BY...
)


You need something for the ORDER BY clause. It is possible that your OUTPUT clause will insert more than on row into @return
Go to Top of Page

JohnDW
Starting Member

45 Posts

Posted - 2014-10-11 : 01:07:33
Txs gbritton,
Can I cast @Scannummer as int?

@CurrentTal = ISNULL(Value, CAST(@Scannummer AS INT))
or with COALESCE(Value, CAST(@Scannummer AS INT))



if you do not try, it will not work
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-10-11 : 09:32:57
What is in scannummer? If all numerics, you might be able to cast it as int, but be aware that 13 digits is bigger than a 32-bit integer.
Go to Top of Page
    Next Page

- Advertisement -