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) ASBEGIN SET NOCOUNT ON; declare @productid int, @premium int,@prem intselect @premium = productid From Product Where scannummer = @scannummerif 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()) endENDIf I put an output to the sp then it doesn't work:ALTER PROCEDURE [dbo].[spTelling] @scannummer Nvarchar(13) ASBEGIN SET NOCOUNT ON; declare @productid int, @premium int,@prem intselect @premium = productid From Product Where scannummer = @scannummerif 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()) endelse Declare @outscan nvarchar(13) EXEC @outscan= @scannummer OUTPUTENDThat'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.Txsif 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) OUTthen change your VB where you call the proc to add the output parameter and map it to a VB variable |
|
|
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 |
|
|
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 |
|
|
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.Johnif you do not try, it will not work |
|
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2014-10-05 : 16:15:27
|
at then end you have:if (count(@premium) = NULL)Beginselect @outscan = @scannummerreturn @outscanENDwhat 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) |
|
|
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 |
|
|
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 ASBEGIN -- 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 intselect @productid = (productid) From Product Where scannummer = @scannummerselect @unique = count(productid) from Product Where scannummer = @scannummerif (@unique > 0) begin select @un = count(productid) from Telling where ProductId = @productidif (@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 endif (@unique = 0)Beginset @outscan = @scannummerreturn @outscan EXEC dbo.spTelling @scannummer, @outscan OUTPUTENDEND 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 |
|
|
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 loopEXEC dbo.spTelling @scannummer, @outscan OUTPUTENDEND |
|
|
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 |
|
|
JohnDW
Starting Member
45 Posts |
Posted - 2014-10-06 : 15:13:37
|
[code]ALTER PROCEDURE [dbo].[spTelling] @scannummer Nvarchar(13), @outscan Nvarchar(13) output ASBEGIN SET NOCOUNT ON;declare @productid int, @unique int, @un intselect @productid = (select(productid) From Product Where scannummer = @scannummer)select @unique = (select count(productid) from Product Where scannummer = @scannummer)Begin if (@unique > 0) beginselect @un = (select count(ProductId) from Telling where ProductId = @productid) if (@un > 0) beginUpdate TELLING set Tal = Tal + 1 where ProductId = @productid end else beginINSERT INTO Telling (ProductID, Tal, Date) VALUES (@productid,1,getdate()) end end else Begin set @outscan = @scannummer return @outscan ENDENDEnd[/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 |
|
|
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 OUTPUTTara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
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 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2014-10-07 : 06:30:46
|
This part will NEVER be trueif (@unique = 0)Beginset @outscan = @scannummerreturn @outscan EXEC dbo.spTelling @scannummer, @outscan OUTPUTEND 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 |
|
|
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 clientSET NOCOUNT ON;-- Prepare output valueDECLARE @Return TABLE ( Value INT NOT NULL );-- Increment the Telling TalMERGE dbo.Telling AS tgtUSING ( SELECT TOP(1) ProductID, 1 AS Tal, GETDATE() AS [Date] FROM dbo.Product WHERE ScanNummer = @ScanNummer ) AS src ON src.ProductID = tgt.ProductIDWHEN MATCHED THEN UPDATE SET tgt.Tal += src.TalWHEN NOT MATCHED BY TARGET THEN INSERT ( ProductID, Tal, [Date] ) ( src.ProductID, src.Tal, src.[Date] )OUTPUT inserted.TalINTO @Return ( Value );-- Return the current Tal for ScanNummerSELECT @CurrentTal = ValueFROM @Return; Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA |
|
|
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 clientSET NOCOUNT ON;-- Prepare output valueDECLARE @Return TABLE ( Value INT NOT NULL );-- Increment the Telling TalMERGE dbo.Telling AS tgtUSING ( SELECT TOP(1) ProductID, 1 AS Tal, GETDATE() AS [Date] FROM dbo.Product WHERE ScanNummer = @ScanNummer ) AS src ON src.ProductID = tgt.ProductIDWHEN MATCHED THEN UPDATE SET tgt.Tal += src.TalWHEN NOT MATCHED BY TARGET THEN INSERT ( ProductID, Tal, [Date] ) values ( src.ProductID, src.Tal, src.[Date] )OUTPUT inserted.TalINTO @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 outputif 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 |
|
|
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) |
|
|
JohnDW
Starting Member
45 Posts |
Posted - 2014-10-10 : 10:01:24
|
Txs, but how to do that:if Value is null than@CurrentTal = @scannummerelseEND(nothing must to output)if you do not try, it will not work |
|
|
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 likeSET @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 |
|
|
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 |
|
|
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. |
|
|
Next Page
|