如何使用相关子查询更新记录?

发布于 2024-10-18 12:04:18 字数 1104 浏览 4 评论 0原文

我有一个函数,它接受一个参数并返回一个表/结果集。我想将表中的一个字段设置为该记录集的第一个结果,并传入表的其他字段之一作为参数。如果用文字来说太复杂,则查询看起来像这样:

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

在本例中,myFieldmyOtherFieldmyTable 中的字段,而 >myFunctionFieldfn_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晚雾 2024-10-25 12:04:18

我从来没有做过这样的事情,所以......我看到的所有代码都在函数名称上使用模式 - 所以类似于:

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)

凹づ凸ル 2024-10-25 12:04:18

似乎是 SQL 2000 中的编译器错误

尝试在 Server 2005 中并使用 CROSS APPLY 或 OUTER APPLY 加入表值函数

也尝试一下,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

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
归属感 2024-10-25 12:04:18

我在谷歌上搜索了这个错误,有人谈到搜索条件中缺少单引号。你的函数代码也是这样吗? 相关博客链接

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

风铃鹿 2024-10-25 12:04:18

似乎(至少在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文