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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Testing SQL Stored Procedure?

Author  Topic 

luvdairish
Starting Member

6 Posts

Posted - 2007-05-18 : 13:06:11
Hello all,

I have a stored procedure that I want to test, but it has some variables that it is requiring from me when I "Execute Stored Procedure" from the SQL Server Management Studio. I only want to supply it one variable. The other variables are supplied by the statements within. Is there a way for me to test by just supplying the one variable OrderID?

Here is my code:

WHILE (SELECT * FROM Custodial_Order_Details WHERE OrderID = @OrderID)
BEGIN
SELECT @oProductID = ProductID, @oQTY = QTY FROM
Custodial_Order_Details
WHERE OrderID = @OrderID

SELECT @NewQTY = SUM(QTY - @oQTY)
FROM custodial_inventory
WHERE ProductID = @oProductID

UPDATE custodial_inventory
SET QTY = @newQTY
WHERE ProductID = @oProductID
END

Any ideas? I want to make sure the statements within the loop do their part. Thanks!

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-05-18 : 13:09:03
this doesn't work?
exec yourProc @OrderId = 12345 -- or your order number

maybe you should show us your sproc



_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

luvdairish
Starting Member

6 Posts

Posted - 2007-05-18 : 13:19:56
quote:
Originally posted by spirit1

this doesn't work?
exec yourProc @OrderId = 12345 -- or your order number

maybe you should show us your sproc

I didn't try it on the command line (can you tell me how do I do that...I'm a GUI guy). I was trying it using right click on stored procedure -> then execute stored procedure.

Here it is....

CREATE PROCEDURE sp_DeductOrderFromInventory
@OrderID int = 0,
@oProductID int = 0,
@oQty int = 0,
@NewQty int = 0

AS
BEGIN
SET NOCOUNT ON;

WHILE (SELECT * FROM Custodial_Order_Details WHERE OrderID = OrderID)
BEGIN

SELECT @oProductID = ProductID, @oQTY = QTY FROM
Custodial_Order_Details
WHERE OrderID = @OrderID

SELECT @NewQTY = SUM(QTY - @oQTY)
FROM custodial_inventory
WHERE ProductID = @oProductID

UPDATE custodial_inventory
SET QTY = @newQTY
WHERE ProductID = @oProductID
END

END
GO

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp

Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-05-18 : 13:25:08
put default values for your non needed variables

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

luvdairish
Starting Member

6 Posts

Posted - 2007-05-18 : 14:05:06
I'm not sure if I understand. Please excuse me, I'm new to this.
The order ID is the only value I want to supply to this proc. The other values (@oProductID and @oQTY) are to be supplied by the SELECT statement from the sproc:

SELECT @oProductID = ProductID, @oQTY = QTY FROM
Custodial_Order_Details
WHERE OrderID = @OrderID

When I declare them, they have the value 0 (unless I am misunderstanding), which would be a default value, no?:

@OrderID int = 0,
@oProductID int = 0,
@oQty int = 0,
@NewQty int = 0

To top it off, no matter what I do, I get a infinite loop. Any thoughts?





Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-05-18 : 14:12:37
so if you only need orderid as the input parameter why do you have all others as input parameters?
simply declare them in the sproc.

this sproc is one of the best examples of procedural and not set based thinking.
see if this works better for you:

CREATE PROCEDURE sp_DeductOrderFromInventory
@OrderID int
AS
BEGIN
SET NOCOUNT ON;

UPDATE ci
SET QTY = SUM(ci.QTY-cod.QTY)
FROM custodial_inventory ci
JOIN Custodial_Order_Details cod ON ci.ProductID = cod.ProductID
WHERE OrderID = @OrderID
END
GO


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

akashenk
Posting Yak Master

111 Posts

Posted - 2007-05-18 : 16:31:25
1) I think you may be confusing "variables" with input Parameters. I would agree that your SP only should have input parameters for data that is coming from the outside. If these other values are only used internally, then they should be declared within the SP as variables, and not as part of the input parameter list. so, instead of

CREATE PROCEDURE sp_DeductOrderFromInventory
@OrderID int = 0,
@oProductID int = 0,
@oQty int = 0,
@NewQty int = 0

you would have:

CREATE PROCEDURE sp_DeductOrderFromInventory
@OrderID int = 0

And then within the body of your sp, you would have

DECLARE @oProductID int, @oQty int, @NewQty int

If you need to set a default, then have statements like

SET @oProductiID = 0
SET @oQty = 0
SET @NewQty = 0

And then follow this with your normal way of specifying the values for these "variables"


2) Asside from the SP structure issue, I believe in SQL Management Studio, when you "Execute" an SP, you are able to specify "Null" values for parameters by checking a box next to the parameter name. The resultant EXEC statement will contain "@parameter = NULL" for each parameter you do this for.

By the way... no value (or null) is different than a default value ('0' in your case), so be careful that your SP is able to gracefully handle Null input parameters.
Go to Top of Page

luvdairish
Starting Member

6 Posts

Posted - 2007-05-21 : 07:51:24
THANK YOU akashenk!!

That was exactly what I needed.
Go to Top of Page
   

- Advertisement -