如何使用相关子查询更新记录?
我有一个函数,它接受一个参数并返回一个表/结果集。我想将表中的一个字段设置为该记录集的第一个结果,并传入表的其他字段之一作为参数。如果用文字来说太复杂,则查询看起来像这样:
UPDATE myTable
SET myField = (SELECT TOP 1 myFunctionField
FROM fn_doSomething(myOtherField)
WHERE someCondition = 'something')
WHERE someOtherCondition = 'somethingElse'
在本例中,myField
和 myOtherField
是 myTable
中的字段,而 >myFunctionField
是 fn_doSomething
返回的字段。这对我来说似乎是合乎逻辑的,但我收到以下奇怪的错误:
'myOtherField' is not a recognized OPTIMIZER LOCK HINTS option.
知道我做错了什么,以及如何完成此操作?
更新:
根据 Anil Soman 的回答,我意识到该函数需要一个字符串参数,并且传递的字段是一个整数。我不确定这是否应该是一个问题,因为使用整数值显式调用函数是有效的 - 例如 fn_doSomething(12345) 似乎会自动将数字转换为字符串。但是,我尝试进行显式转换:
UPDATE myTable
SET myField = (SELECT TOP 1 myFunctionField
FROM fn_doSomething(CAST(myOtherField AS varchar(1000)))
WHERE someCondition = 'something')
WHERE someOtherCondition = 'somethingElse'
现在我收到以下错误:
Line 5: Incorrect syntax near '('.
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 '('.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我从来没有做过这样的事情,所以......我看到的所有代码都在函数名称上使用模式 - 所以类似于:
FROM dbo.fn_doSomething(myOtherField)
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)
似乎是 SQL 2000 中的编译器错误
尝试在 Server 2005 中并使用 CROSS APPLY 或 OUTER APPLY 加入表值函数
也尝试一下,guru huys
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
我在谷歌上搜索了这个错误,有人谈到搜索条件中缺少单引号。你的函数代码也是这样吗? 相关博客链接
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
似乎(至少在 SQL Server 2000 中)您无法将列值传递给表值函数。我必须设置一个标量函数来解决这个问题。
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.