Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem getting print to show variable 2

Status
Not open for further replies.

tigerjade

Programmer
Mar 17, 2004
237
US
I have an SP that, among other things, needs to sum some values and play with it. It seems to be dying, because all I get is the two selected values at the top:

Code:
ALTER PROCEDURE sp_addItemsAccounting
@ItemID int
,@Received int
,@UserID int
,@InvoiceID int
,@Success int Output
AS

DECLARE @Quantity int, @CurrentSum int

SELECT CONVERT(int, Quantity) AS Quantity FROM Item WHERE ID = @ItemID
SELECT SUM(Received) AS CurrentSum FROM OutstandingItemsAccounting WHERE ItemID = @ItemID

IF (@Quantity >= (@CurrentSum + @Received))
BEGIN
	IF NOT EXISTS (SELECT ID FROM OutstandingItemsAccounting WHERE ItemID = @ItemID AND DATEDIFF(ss, DateVerified, GetDate()) < '10')
	BEGIN
		INSERT INTO OutstandingItemsAccounting (ItemID, Received, DateVerified, UserID, InvoiceID) 
		VALUES (@ItemID, @Received, GetDate(), @UserID, @InvoiceID)
		SET @Success = 1
	END
	ELSE
	BEGIN
		SET @Success = 0
	END
END
ELSE
BEGIN
	SET @Success = 0
END

GO

When I run this in QA, I see the CurrentSum and Quantity values, but nothing else. I can't get QA to print anything at all, so I can't find out what the combined value of the two is or if it's even entering the if/else clause.

What am I doing wrong?

thanks!

tigerjade

"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding


 
Until Mircosoft comes with Mind-Reading Development Kit (tm), that won't fill @variables as you expect. Try:
Code:
SELECT [b]@Quantity =[/b] CONVERT(int, Quantity) FROM Item WHERE ID = @ItemID
Ditto for other two variables.

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
vongrunt, when I try what you suggest, I don't get a value returned at all. I had left some test code in that when I posted; here's what I'm trying currently:

Code:
ALTER PROCEDURE sp_addItemsAccounting
@ItemID int
,@Received int
,@UserID int
,@InvoiceID int
,@Success int Output
AS

DECLARE @Quantity int, @CurrentSum int

SELECT Quantity FROM Item WHERE ID = @ItemID
SELECT SUM(Received) AS CurrentSum FROM OutstandingItemsAccounting WHERE ItemID = @ItemID

PRINT 'some nonsense here'
IF (@Quantity >= (@CurrentSum + @Received))
BEGIN
    PRINT 'insert'
	IF NOT EXISTS (SELECT ID FROM OutstandingItemsAccounting WHERE ItemID = @ItemID AND DATEDIFF(ss, DateVerified, GetDate()) < '10')
	BEGIN
		INSERT INTO OutstandingItemsAccounting (ItemID, Received, DateVerified, UserID, InvoiceID) 
		VALUES (@ItemID, @Received, GetDate(), @UserID, @InvoiceID)
		SET @Success = 1
	END
	ELSE
	BEGIN
		SET @Success = 0
	END
END
ELSE
BEGIN
    PRINT 'forget it'
	SET @Success = 0
END

GO

There are no null values, 'cause it's returning a result for both CurrentSum and Quantity when I test in QA. However, that very next Print statement doesn't print, and neither do the Print statements in the If/Else clause. All I see in QA when I run the SP is:

Code:
Quantity
1      8

CurrentSum
1      7

Am I not using the Print statements correctly?


thanks!

tigerjade

"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding


 
This:
Code:
SELECT Quantity FROM Item WHERE ID = @ItemID
... is not what I suggested. :(

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
you are doing a select not print
SELECT Quantity FROM Item WHERE ID = @ItemID
instead of

declare @Quantity int
SELECT Quantity =Quantity FROM Item WHERE ID = @ItemID
print @Quantity for example

The print statements will be in the messages output window

Denis The SQL Menace
SQL blog:
Personal Blog:
 
No, it's not. :) If I use
Code:
SELECT @Quantity = CONVERT(int, Quantity) FROM Item WHERE ID = @ItemID

I don't get a return at all. That's why I figured it wasn't working. Am I wrong? Is it working if it DOESN'T show up in the QA window?

tigerjade :)

"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding


 
That WAS it, vongrunt! I was mistaking output with variable assignment, apparently. Guess that's what happens when you learn these things on the fly. Thanks, guys!

tigerjade :)

"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top