0

I have a function that accepts one parameter and returns a table/resultset. I want to set a field in a table to the first result of that recordset, passing in one of the table's other fields as the parameter. If that's too complicated in words, the query looks something like this:

UPDATE myTable
SET myField = (SELECT TOP 1 myFunctionField
        FROM fn_doSomething(myOtherField)
        WHERE someCondition = 'something')
WHERE someOtherCondition = 'somethingElse'

In this example, myField and myOtherField are fields in myTable, and myFunctionField is a field return by fn_doSomething. This seems logical to me, but I'm getting the following strange error:

'myOtherField' is not a recognized OPTIMIZER LOCK HINTS option.

Any idea what I'm doing wrong, and how I can accomplish this?

UPDATE:

Based on Anil Soman's answer, I realized that the function is expecting a string parameter and the field being passed is an integer. I'm not sure if this should be a problem as an explicit call to the function using an integer value works - e.g. fn_doSomething(12345) seems to automatically cast the number to an string. However, I tried to do an explicit cast:

UPDATE myTable
SET myField = (SELECT TOP 1 myFunctionField
        FROM fn_doSomething(CAST(myOtherField AS varchar(1000)))
        WHERE someCondition = 'something')
WHERE someOtherCondition = 'somethingElse'

Now I'm getting the following error:

Line 5: Incorrect syntax near '('.
1
  • Can you add that function code too? Feb 21, 2011 at 9:15

4 Answers 4

1

I have never done anything like this so .... all the code I have seen uses a schema on the function name - so something like:

FROM dbo.fn_doSomething(myOtherField)

2
  • +1. If it's SQL Server, then the UDF name must be prefixed with the schema name. And the default is usually dbo.
    – Andriy M
    Feb 21, 2011 at 10:23
  • thanks for the suggestion, but this was just an example query - in my actual query the function is fully qualified.
    – froadie
    Feb 21, 2011 at 16:03
1

seems like a compiler bug in SQL 2000

try in Server 2005 and join the table-valued function using CROSS APPLY or OUTER APPLY

also try this, guru huys

CREATE FUNCTION FCN_pruebaChicaBorrame(@numerito int)
RETURNS @returnTable TABLE (numerito int)
AS
BEGIN
    insert into @returnTable values(@numerito)
    return
END

Select * from FCN_pruebaChicaBorrame(20)

Select col_1
    from (  select 1 as col_1
        union   select 2
        union   select 3) as tablita

Select col_1, (select * from dbo.FCN_pruebaChicaBorrame(20) as fcnTable)
    from (  select 1 as col_1
        union   select 2
        union   select 3) as tablita

Select col_1, (select * from dbo.FCN_pruebaChicaBorrame(col_1) as fcnTable)
    from (  select 1 as col_1
        union   select 2
        union   select 3) as tablita

Select col_1, (select * from dbo.FCN_pruebaChicaBorrame(case when 1=1 then 20 else 21) as fcnTable)
    from (  select 1 as col_1
        union   select 2
        union   select 3) as tablita
0

I searched on google for this error and one person talks about missing single quotes in search condition. Is that the case with your function code? link to related blog

1
  • Hmmmm this may be my problem... possibly the field is an integer field but the function is expecting it as a string. Let me try playing with this...
    – froadie
    Feb 21, 2011 at 9:22
0

It seems that (at least in SQL Server 2000) you can't pass a column value to a table valued function. I had to set up a scalar function to get around this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.