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
 General SQL Server Forums
 New to SQL Server Programming
 UDF Table "Must declare the scalar variable"

Author  Topic 

moooris
Starting Member

11 Posts

Posted - 2009-06-14 : 03:47:37
Using sql2005 im trying to compile this function:


----------------------------------------------------------
CREATE FUNCTION Reports.fTotalDev(@LotID VARCHAR(15))

RETURNS @FailsTable TABLE(LeftFails INT,RightFails INT)

AS
BEGIN

DECLARE @LeftDevice INT,@RightDevice INT
DECLARE @LeftFails INT,@RightFails INT

SET @LeftDevice = (SELECT TOP 1 DeviceID FROM Tests.Registry WHERE LotID = @LotID ORDER BY DeviceID ASC)

IF (SELECT COUNT((SELECT DISTINCT DeviceID FROM Tests.Registry WHERE LotID = @LotID))) > 1
BEGIN
SET @RightDevice = (SELECT TOP 1 DeviceID FROM Tests.Registry WHERE LotID = @LotID ORDER BY DeviceID DESC)
END

SET @LeftFails = (SELECT COUNT(Id) FROM Tests.Registry WHERE Fail = 'DEV' AND LotID = @LotID AND DeviceID = @LeftDevice)
SET @RightFails = (SELECT COUNT(Id) FROM Tests.Registry WHERE Fail = 'DEV' AND LotID = @LotID AND DeviceID = @RightDevice)

INSERT INTO @FailsTable (LeftFails,RightFails) VALUES (@LeftFails,@RightFails )

RETURN @FailsTable

END
GO
----------------------------------------------------------

But then im getting this error:

Msg 137, Level 15, State 2, Procedure fTotalDev, Line 24
Must declare the scalar variable "@FailsTable".


If i will try to move the deceleration to the body, i would also get an error.

How can i write this properly?
Thanks.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-14 : 03:49:51
[code]
CREATE FUNCTION Reports.fTotalDev(@LotID VARCHAR(15))

RETURNS @FailsTable TABLE(LeftFails INT,RightFails INT)

AS
BEGIN

DECLARE @LeftDevice INT,@RightDevice INT
DECLARE @LeftFails INT,@RightFails INT

SET @LeftDevice = (SELECT TOP 1 DeviceID FROM Tests.Registry WHERE LotID = @LotID ORDER BY DeviceID ASC)

IF (SELECT COUNT((SELECT DISTINCT DeviceID FROM Tests.Registry WHERE LotID = @LotID))) > 1
BEGIN
SET @RightDevice = (SELECT TOP 1 DeviceID FROM Tests.Registry WHERE LotID = @LotID ORDER BY DeviceID DESC)
END

SET @LeftFails = (SELECT COUNT(Id) FROM Tests.Registry WHERE Fail = 'DEV' AND LotID = @LotID AND DeviceID = @LeftDevice)
SET @RightFails = (SELECT COUNT(Id) FROM Tests.Registry WHERE Fail = 'DEV' AND LotID = @LotID AND DeviceID = @RightDevice)

INSERT INTO @FailsTable (LeftFails,RightFails) VALUES (@LeftFails,@RightFails )

RETURN @FailsTable

END
GO
[/code]
try like above.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-14 : 03:52:33
also, seeing your query it seems like you will have same value in @LeftDevice & @RightDevice always when the distinct count of devices for passed on LotID is > 1
Go to Top of Page

moooris
Starting Member

11 Posts

Posted - 2009-06-14 : 04:13:35
Thanks alot for the fast help!

It works perfect now :D

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-14 : 04:15:11
welcome
Go to Top of Page
   

- Advertisement -