December 11, 2006 at 6:14 am
Hello,
I have an update statement such that:
([dbo].[myStoredProcedure] takes one parameter, a date)
update dbo.mytable
set
mytable.one = source.uno
,mytable.two = source.due
,mytable.three = source.tre
from
(exec [dbo].[myStoredProcedure] GetDate()) source
where
mytable.dte = source.dte
source.type = 'BUY'
(I've added the alias' for readability - i think it does work in practice but for this exercise am more interested in the FROM clause)
It's not working...
1) Can is work?
2) Is it the right approach?
3) What would you do?
Thanks!
December 11, 2006 at 6:43 am
Hi,
1) It will not work.
2)It is not a right approach
It would be good to make a user defined function in place of stored proceduer and used it.
cheers
December 11, 2006 at 6:44 am
i believe you need to create a temp table to capture teh results of the stored proc as one step, and then update from the temp table.
alternatively, you might be able to reqwrite the procedure to be a function that returns a table...then you can do an update like you've done above, replacing the exec [dbo].[myStoredProcedure] GetDate()) source
with select * from dbo.myFunction(getdate() ) as source
Lowell
December 11, 2006 at 6:55 am
Thank you both for very considerate responses.
To stay in line with the other objects I've created, I believe that I prefer the temp table approach... How would that work considering the Stored Procedure must receive an argument (I've never used a temp table) ?
(pseudo code will be appreciated)
December 11, 2006 at 7:28 am
Ahhh, I think I have it:
CREATE TABLE #mySource (
dte varchar (8)
,uno int
,due int
,tre int
,type varchar(4) )
INSERT #mySource (
dte
,uno
,due
,tre
,type)
EXECUTE [dbo].[myStoredProcedure] GetDate()
update dbo.mytable
set
mytable.one = source.uno
,mytable.two = source.due
,mytable.three = source.tre
from
(exec [dbo].[myStoredProcedure] GetDate()) source
where
mytable.dte = source.dte
source.type = 'BUY'
December 12, 2006 at 7:04 am
This might work better :
update MT set
MT.one = MS.uno
,MT.two = MS.due
,MT.three = MS.tre
from dbo.mytable MT INNER JOIN #MySource MS ON MT.dte = MS.dte
where source.type = 'BUY'
December 12, 2006 at 7:09 am
Please, why so?
December 12, 2006 at 7:22 am
from
(exec [dbo].[myStoredProcedure] GetDate()) source
where
This code of code is forbidden. You have to use the temp tables you just created to do the update.
December 12, 2006 at 7:57 am
Ahh I see... I didn't provide you with my final corrected Pseudo code:
update dbo.mytable
set
mytable.one = source.uno
,mytable.two = source.due
,mytable.three = source.tre
from
#mySource source
where
mytable.dte = source.dte
and
source.type = 'SELL'
I assumed you saw the above and were advocating an explicit INNER JOIN rather than the implicit join that I actually used.
If you see a problem with this however, please let me know.
It does appear to work though.
December 12, 2006 at 8:02 am
Ya that should do it. The explicit join is always better, especially in avoiding unwanted cross joins. I was just pointing out again that the exec (spname) can't work the way is was presented.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply