使用存储过程导致更新语句
我有一个包含两列(除其他外)的表:id 和created_in_variant 以及一个基于id 计算created_in_variant 值的存储过程。 我想做这样的事情:
UPDATE [dbo].[alerts]
SET [created_in_variant] = find_root [id]
有什么好的方法吗?
I've got a table with two columns(among others): id and created_in_variant and a stored procedure that calculates the created_in_variant value basing on the id.
I'd like to do something like this:
UPDATE [dbo].[alerts]
SET [created_in_variant] = find_root [id]
Is there a nice way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
对于此类问题,您可能需要考虑使用标量值函数(也称为用户定义函数)而不是存储过程。
编辑:以下是有关 SVF 的一些信息:单击
编辑 2:以下是来自 15 Seconds
You may want to look at using a scalar valued function (also known as a user defined function) instead of a stored procedure for this type of problem.
EDIT: Here is some information concerning SVFs : Click
EDIT 2: Here is some more information from 15 Seconds
将您的过程更改为 UDF,您基本上可以按照原样调用它
Change your proc into a UDF and you basically call it exactly as you got
如果您被迫使用存储过程来实现您所要求的内容,您只需将找到的根值保存在变量中并将其传递即可。
除非您像其他人提到的那样使用 UDF(用户定义函数),否则没有比这更简单的方法了。
这是使用存储过程时最简单的答案之一,因为您无法将存储过程直接传递给 UPDATE 语句。 没有像 C# 或 Java 等其他语言那样的插值。
If you are forced to use stored procedure to implement what you have asked, you can simply save the found root value in a variable and pass it along.
There is no simpler way unless you use a UDF (user-defined function) like others have mentioned.
This is one of the simplest answers when using stored procedure because you cannot pass stored procedure directly to the
UPDATE
statement. There is no interpolation like in other languages like C# or Java.将存储过程重写为标量值函数。 然后您可以使用它进行更新。
Rewrite the stored procedure as a scalar valued function. Then you can use this for your update.
您可以将created_in_variant设为计算字段吗?
Can you make the created_in_variant a computed field?
您可以使用 UDF 代替存储过程。
You could use a UDF instead of a stored procedure.
我不这么认为。 如果您将存储过程转换为可以工作的缩放器函数。
如果您想使用存储过程,则必须为每个 id(在游标中)调用存储过程并返回 OUTPUT 变量中的值,然后使用输出变量对每个 id 进行更新。 但是,使用 UDF 会更好。
I don't think so. If you turned the stored procedure into scaler function that'd work.
If you wanted to use a stored procedure you have to call the stored procedure for each id (in a cursor) and return the value in a OUTPUT variable then do the update for each id w/ the output variable. However, It'd be much better to use a UDF.
您可以将存储过程重新编程为用户定义的函数并使用它。
You could reprogram the stored procedure to a user defined function and use that.
你只是缺少 dbo.,DJ 的回答应该已经很好了。 我今天也遇到了同样的问题。
You are simply missing dbo., DJ answer shuold already be good. i had the same exact problem today.