Gaz
2008-02-05 15:34:46 UTC
Hi,
Im trying to write a scalar function that basically does a count based
a set of criteria. What i first tried to do was a case statement
within the where clause which looked like this...
select @ReturnMe=count(column)
from table
where
t1.column in (
case when Upper(@value) = 'ME' then
1,9
case when Upper(@Value) = 'YOU' then
8
)
if i use single values then it works and my function returned a value
of the count. So i thought i would try and dynamically create the SQL
statement and then exec it but SQL Server gets upset and throws up an
error message.
This is my code...
-------------------------------------------------------------------------------------------------------------------------------
DECLARE @ReturnMe int,
@SQL nvarchar(max)
set @ReturnMe = 0
set @SQL = 'SELECT @ReturnMe = Count(t1.column) '+
'FROM table.column t1 ' +
'WHERE t1.column in('+ dbo.CustomFunction(@Type)+')'
exec sp_executesql @SQL
RETURN @ReturnMe
----------------------------------------------------------------------------------------------------------------------------
The CustomFunction function is returning a string like 1,9 or 8 for
example. But when i execute this i get this message
Only functions and extended stored procedures can be executed from
within a function.
Invalid attempt to read when no data is present.
Can anyone offer any advice? All i want to do is return the value of
the count column to the calling store procedure
Im trying to write a scalar function that basically does a count based
a set of criteria. What i first tried to do was a case statement
within the where clause which looked like this...
select @ReturnMe=count(column)
from table
where
t1.column in (
case when Upper(@value) = 'ME' then
1,9
case when Upper(@Value) = 'YOU' then
8
)
if i use single values then it works and my function returned a value
of the count. So i thought i would try and dynamically create the SQL
statement and then exec it but SQL Server gets upset and throws up an
error message.
This is my code...
-------------------------------------------------------------------------------------------------------------------------------
DECLARE @ReturnMe int,
@SQL nvarchar(max)
set @ReturnMe = 0
set @SQL = 'SELECT @ReturnMe = Count(t1.column) '+
'FROM table.column t1 ' +
'WHERE t1.column in('+ dbo.CustomFunction(@Type)+')'
exec sp_executesql @SQL
RETURN @ReturnMe
----------------------------------------------------------------------------------------------------------------------------
The CustomFunction function is returning a string like 1,9 or 8 for
example. But when i execute this i get this message
Only functions and extended stored procedures can be executed from
within a function.
Invalid attempt to read when no data is present.
Can anyone offer any advice? All i want to do is return the value of
the count column to the calling store procedure