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.
| 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 = @oProductIDENDAny 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 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
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 sprocI 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 = 0ASBEGIN 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 ENDGO_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp
|
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2007-05-18 : 13:25:08
|
| put default values for your non needed variables_______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
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 FROMCustodial_Order_DetailsWHERE OrderID = @OrderIDWhen 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 = 0To top it off, no matter what I do, I get a infinite loop. Any thoughts? |
 |
|
|
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 intASBEGINSET NOCOUNT ON;UPDATE ciSET QTY = SUM(ci.QTY-cod.QTY)FROM custodial_inventory ci JOIN Custodial_Order_Details cod ON ci.ProductID = cod.ProductIDWHERE OrderID = @OrderIDENDGO _______________________________________________Causing trouble since 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
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 ofCREATE PROCEDURE sp_DeductOrderFromInventory @OrderID int = 0, @oProductID int = 0,@oQty int = 0,@NewQty int = 0you would have:CREATE PROCEDURE sp_DeductOrderFromInventory @OrderID int = 0And then within the body of your sp, you would haveDECLARE @oProductID int, @oQty int, @NewQty intIf you need to set a default, then have statements likeSET @oProductiID = 0SET @oQty = 0SET @NewQty = 0And 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. |
 |
|
|
luvdairish
Starting Member
6 Posts |
Posted - 2007-05-21 : 07:51:24
|
THANK YOU akashenk!!That was exactly what I needed. |
 |
|
|
|
|
|
|
|