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 2000 Forums
 Transact-SQL (2000)
 Sub-select in Scalar function

Author  Topic 

Kristen
Test

22859 Posts

Posted - 2004-07-22 : 10:17:59
I have some code in a function that does

CREATE FUNCTION MyFunction
(
@foo int
)
RETURNS varchar(7500)
AS
RETURN
(
SELECT MAX(MyColumn)
-- Start of Test Execute
-- SELECT TOP 10 *
FROM MyTable XXX
WHERE ... some stuff ...
AND NOT EXISTS
(
SELECT *
FROM MyTable YYY
WHERE ... Some Stuff2 ...
AND YYY.ID <> XXX.ID
)

-- End of Test Execute
)
GO

If I highlight the section within the "Test Execute" markers it runs OK.

When I try to create the function I get
The column prefix 'XXX' does not match with a table name or alias name used in the query.
(The only XXX alias references are in the sub-select)

If I comment-out the blue stuff it creates OK.

Are sub-selects not allowed in scalar functions?

Kristen

X002548
Not Just a Number

15586 Posts

Posted - 2004-07-22 : 14:53:43
quote:
Originally posted by Kristen

Are sub-selects not allowed in scalar functions?



Ummmm.....

No


USE Northwind
GO

CREATE FUNCTION udf_myFunction99(@rs int)
RETURNS int
AS
BEGIN
DECLARE @x int
SELECT @x =
(SELECT COUNT(*)
FROM Orders o
WHERE EXISTS (SELECT *
FROM Orders i
WHERE o.OrderId = i.OrderId)
)
RETURN @x
END
GO

SELECT dbo.udf_myFunction99(1)
GO

DROP FUNCTION udf_myFunction99
GO



Why not post the actual function...and why use a function for this?



Brett

8-)
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2004-07-22 : 15:07:49
yes, i agree with Brett, i suspect there is something else in your code that you have not posted that is causing this error.

- Jeff
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-07-22 : 16:46:06
OK, that's good to hear, thanks. I changed it to an OUTER JOIN and it worked, but I'll go back and get my Fine Tooth Comb out.

Kristen
Go to Top of Page
   

- Advertisement -