Can anyone tell me how to access the results from a select statement within the stored procedure?
This is to implement auditting functionality, before updating a record i want select the original record and compare the contents of each field with the varibles that are passed in, if the fields contents has changed i want to write an audit entry.
So i'd like to store the results of the select statement in a variable and access it like a dataset. e.g
declare selectResult
set selectResult = Select field1,field2,field3 from table1
if selectResult.field1 <> @.field1
begin
exec writeAudit @.var1,@.var2,var3
end
Many thanks.Cursors are the answer.|||Cursors are NEVER the answer...|||You can use the dreaded temp tables or move your inner proc to a function.
insert into #temp
exec mylovelyProc
I'd love to suggest tables vars but they don't work in this situation...grrr.|||Well if you are updating one record you can do something like so:
CREATE PROC DoSomething
@.UniqueID INT,
@.Value1 VARCHAR(10),
@.Value2 INT,
@.Value3 VARCHAR(100)
AS
DECLARE @.OldValue1 VARCHAR(10)
DECLARE @.OldValue2 INT
DECLARE @.OldValue3 VARCHAR(100)SELECT @.OldValue1 = Table1.Value1, @.OldValue2 = Table1.Value2, @.OldValue3 = Table1.Value3 FROM Table1 WHERE Table1.IDField = @.UniqueID
IF @.OldValue1 <> @.Value1 OR @.OldValue2 <> @.Value2 OR @.OldValue3 <> @.Value3
EXEC writeAudit @.OldValue1, @.OldValue2, @.OldValue3
Tha's *one* way to do it. :)
No comments:
Post a Comment