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.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-12-30 : 05:03:48
|
| Adam writes "I have read the FAQ's and many posts, but from what I can gather, you cannot use the EXEC statement in a User Defined Function. I need to return a table of results in a UDF obtained from passing an 'IN' clause. I may have missed something, but I get errors when using:CREATE FUNCTION dbo.fnScheduleCountItems (@RecordType as smallint, @JobIDs as nvarchar(1900))RETURNS @TheTable TABLE (quantity float,recordtype int,recordID int)ASBEGINDeclare @SQL VarChar(2000)select @sql = '' if @RecordType = 17 select @SQL= 'insert into @TheTable select count(ID) as Quantity, 17, CID from JobsC where ID in (' + @JobIDs + ') group by CID' else if @RecordType = 19 select @SQL = 'insert into @TheTable select count(ID) as Quantity, 19, AID from JobsA where ID in ( ' + @JobIDs + ' ) group by AID' else if @RecordType = 5 select @SQL = 'insert into @TheTable select count(ID) as Quantity, 5, PID from JobsP where ID in ( ' + @JobIDs + ' ) group by PID'if @SQL <> '' exec @SQLRETURNENDWhen you select from it it says that it cant find the stored procedure.Any thoughts, as I'm banging my head on this.ThanksAdam" |
|
|
harshal_in
Aged Yak Warrior
633 Posts |
Posted - 2002-12-30 : 05:14:27
|
quote: Adam writes "I have read the FAQ's and many posts, but from what I can gather, you cannot use the EXEC statement in a User Defined Function. I need to return a table of results in a UDF obtained from passing an 'IN' clause. I may have missed something, but I get errors when using:CREATE FUNCTION dbo.fnScheduleCountItems (@RecordType as smallint, @JobIDs as nvarchar(1900))RETURNS @TheTable TABLE (quantity float,recordtype int,recordID int)ASBEGINDeclare @SQL VarChar(2000)select @sql = '' if @RecordType = 17 select @SQL= 'insert into @TheTable select count(ID) as Quantity, 17, CID from JobsC where ID in (' + @JobIDs + ') group by CID' else if @RecordType = 19 select @SQL = 'insert into @TheTable select count(ID) as Quantity, 19, AID from JobsA where ID in ( ' + @JobIDs + ' ) group by AID' else if @RecordType = 5 select @SQL = 'insert into @TheTable select count(ID) as Quantity, 5, PID from JobsP where ID in ( ' + @JobIDs + ' ) group by PID'if @SQL <> '' exec @SQLRETURNENDWhen you select from it it says that it cant find the stored procedure.Any thoughts, as I'm banging my head on this.ThanksAdam"
not sure about this :alter FUNCTION dbo.fnScheduleCountItems (@RecordType as smallint, @JobIDs as nvarchar(1900)) RETURNS @TheTable TABLE ( quantity float, recordtype int, recordID int ) AS BEGIN Declare @SQL VarChar(2000) select @sql = '' if @RecordType = 17 set @SQL= 'insert into @TheTable select count(ID) as Quantity, 17, CID from JobsC where ID in (' + @JobIDs + ') group by CID' else if @RecordType = 19 set @SQL = 'insert into @TheTable select count(ID) as Quantity, 19, AID from JobsA where ID in ( ' + @JobIDs + ' ) group by AID' else if @RecordType = 5 set @SQL = 'insert into @TheTable select count(ID) as Quantity, 5, PID from JobsP where ID in ( ' + @JobIDs + ' ) group by PID' if @SQL <> '' exec @SQL RETURN END Expect the UnExpected |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2002-12-30 : 08:47:21
|
I think the problem is that there is no table returned from the SQL statement executed inside the function. Its an INSERT statement. So I dont think its possible to something like SELECT * FROM dbo.fnScheduleCountItemsOR quote: When you select from it it says that it cant find the stored procedure
I hope Adam is not trying to run something likeEXEC fnScheduleCountItems OS |
 |
|
|
verronep
Starting Member
15 Posts |
Posted - 2002-12-30 : 08:55:36
|
| How are you calling the function and from where?I often get that error message when I forget to put the dbo before the function name..."I have not failed. I have just found 10,000 ways that won't work." - Thomas Alva Edison (1847-1931) |
 |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2002-12-30 : 18:43:05
|
Also,Suggest using sp_executesql.From BOL.quote: sp_executesql can be used instead of stored procedures to execute a Transact-SQL statement a number of times when the change in parameter values to the statement is the only variation. Because the Transact-SQL statement itself remains constant and only the parameter values change, the Microsoft® SQL Server™ query optimizer is likely to reuse the execution plan it generates for the first execution.
|
 |
|
|
|
|
|
|
|