UPDATE with a stored procedure on SQL Server 2005

北慕城南 提交于 2019-12-24 08:34:29

问题


UPDATE users 
SET field = my_sp()

in SQL Server 2005. Apparently I can't do this and have to use some form of EXEC. Can anyone help me out and let me know how to do this? This should be some easy rep.


回答1:


To assign value you need to use sql function. it is impossible to assign value from stored procedure.
Here is link how to create it.




回答2:


you need to write a scalar function that takes some parameters (or even zero) and returns what you need.




回答3:


You could store the output of the stored procedure in a temp table, then use that temp table as the basis for your update. As an example, the code below assumes your proc returns a record set with two integers.

create table #t (
    ColumnA int,
    ColumnB int
)

insert into #t
    (ColumnA, ColumnB)
    exec my_sp

update u
    set field = t.ColumnB
    from users u
        inner join #t t
            on u.UserID = t.ColumnA

drop table #t


来源:https://stackoverflow.com/questions/3842679/update-with-a-stored-procedure-on-sql-server-2005

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!