views:

40

answers:

3

Hello. I want to say

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.

+2  A: 

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.

Danil
Thanks, I've converted to a scalar function. You got the acceptance mark because you gave a link to the syntax.
cookiecaper
+1  A: 

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

Antonio
+1  A: 

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
Joe Stefanelli
Clever solution, thanks, but I've already converted to a function.
cookiecaper
@cookiecaper: That's the better way to go. I just wanted to give another alternative in case that wasn't an option for you.
Joe Stefanelli