Groups | Blog | Home
all groups > sql server programming > december 2004 >

sql server programming : Stored Procedure help.


cyn3rgy NO[at]SPAM hotmail.com
12/8/2004 9:38:34 PM
Hi All

I've written this stored procedure in SQL 2000. When I run it, I get
an error that I've not declared a variable. Full error is:

Inventory: Counting history, ID = 151
Server: Msg 137, Level 15, State 2, Line 1
Must declare the variable '@idHistory'. (I get this line for each
table)


this is my stored procedure code - I'm new to this so apologies for
any dodgy code :)

CREATE PROCEDURE [dbo].[InventoryCountFromidHistory] @idHistory INT
AS
IF (@IDHistory IS NULL)
RETURN -- No target, we can do nothing

-- Drop a message in the Windows Application log
DECLARE @msg nvarchar(1024)
SET @msg = 'Inventory: Counting history, ID = ' + STR(@IDHistory)
PRINT @msg

EXEC sp_MSforeachtable "Select Count(idHistory)as [?] from ? Where
idHistory=@idHistory"
GO

Can anyone help me out, I'm probably doing something totally stupid :)

thanks

oj
12/8/2004 10:05:32 PM
There are a few issues with this sproc.
1. sp_msforeachtable will force the system to step through every user table
in the database and do a count on IdHistory column. Are you sure that every
table in your current database has this column.
2. @idHistory is available outside of sp_msforeachtable scope. Thus, the
error you're getting.

The workaround for #2 is:

CREATE PROCEDURE [dbo].[InventoryCountFromidHistory] @idHistory INT
AS
IF (@IDHistory IS NULL)
RETURN -- No target, we can do nothing

-- Drop a message in the Windows Application log
DECLARE @msg nvarchar(1024)
SET @msg = 'Inventory: Counting history, ID = ' + STR(@IDHistory)
PRINT @msg

declare @sql sysname
set @sql='Select Count(idHistory)as [?] from ? Where
idHistory='+cast(@idHistory as nvarchar)

EXEC sp_MSforeachtable @sql

GO

[quoted text, click to view]

David Gugick
12/9/2004 1:20:49 AM
[quoted text, click to view]

sp_MSForEachTable is an undocumented function. You can use it, but just
beware it could disappear or change in a future release.

I'm guessing the sp_MS* function is executing dynamic SQL on each table
in the current database. You r dynamic SQL is referencing a local
variable. Try building the string using STR(@IDHistory) like you did for
the message.

I'm also not a big fan of Return statements in the middle of the code.
Better to have:

IF (@IDHistory IS NOT NULL)
Begin
-- stuff
End

Return 0





--
David Gugick
Imceda Software
www.imceda.com
Uri Dimant
12/9/2004 8:09:21 AM
Andrew
You are confused. This undocumented stored procedure does not return OUTPUT
parameter
You will probably want to look at below example from the BOL
CREATE PROCEDURE get_sales_for_title
@title varchar(80), -- This is the input parameter.
@ytd_sales int OUTPUT -- This is the output parameter.
AS

-- Get the sales for the specified title and
-- assign it to the output parameter.
SELECT @ytd_sales = ytd_sales
FROM titles
WHERE title = @title

RETURN
GO
The following program executes the stored procedure with a value for the
input parameter and saves the output value of the stored procedure in the
@ytd_sales_for_title variable local to the calling program.

-- Declare the variable to receive the output value of the procedure.
DECLARE @ytd_sales_for_title int

-- Execute the procedure with a title_id value
-- and save the output value in a variable.

EXECUTE get_sales_for_title
"Sushi, Anyone?", @ytd_sales = @ytd_sales_for_title OUTPUT

-- Display the value returned by the procedure.
PRINT 'Sales for "Sushi, Anyone?": ' +
convert(varchar(6),@ytd_sales_for_title)
GO

Sales for "Sushi, Anyone?": 4095

[quoted text, click to view]

cyn3rgy NO[at]SPAM hotmail.com
12/13/2004 7:53:00 PM
Thanks oj, that worked a treat. I was hoping a stored procedure would
be faster than doing the idHistory count from the client side (nested
select statemetns in an asp script). It doesn't seem to be. I might
have to look at redesigning the db I think. Appreciate the reply :)

thanks to everyone else for the feedback.

andrew


[quoted text, click to view]
AddThis Social Bookmark Button