Thursday, March 29, 2012

Cursor to Array

Can you Assign a cursor to an array

For example:

Declare AuditInfo cursor
for
Select top 2 TRAKRID,MeterID,DelCount,ContactID,Type,Active,Cre atedDateTime,ModifiedBy,DateModified
from TRAKRAudit
where TRAKRID = @.ID
order by createdDatetime desc
--Print @.ID
open AuditInfo

-- Get all the newest values
fetch next From AuditInfo into Array()

Thanks
LJdeclare @.tbl table (...)
insert @.tbl Select top 2 TRAKRID,MeterID,DelCount,ContactID,Type,Active,Cre
atedDateTime,ModifiedBy,DateModified
from TRAKRAudit
where TRAKRID = @.ID
order by createdDatetime desc|||Ok this leads me to another ?

Is there any way to dynamically go through the columns in this Temp table.

The 2 records represent Old and New Values from Table1 and Table1_audit.

I am building a string:

Select @.NewStr = Coalesce(convert(varchar(25),@.MeterIDNew),'null')
Select @.NewStr = ('New MeterID = ' + @.NewStr)
Select @.OldStr = Coalesce(convert(varchar(25),@.MeterIDOld),'null')
Select @.OldStr = ('Old Meter ID = ' + @.OldStr)
Select @.StrOutput = @.StrOutput + @.NewStr + @.OldStr + Char(10)

Select @.NewStr = Coalesce(convert(varchar(25),@.MV90IDNew),'null')
Select @.NewStr = ('New MV90 ID = ' + @.NewStr)
Select @.OldStr = Coalesce(convert(varchar(25),@.MV90IDOld),'null')
Select @.OldStr = ('Old MV90 ID = ' + @.OldStr)
Select @.StrOutput = @.StrOutput + @.NewStr + @.OldStr + Char(10)
.
.
.

To put in the body of an e-mail.

Sample
-----------------------
New SCADA ID = 58 Old SCADA ID = 58
New MeterID = null Old Meter ID = null
New MV90 ID = Entergy Old Meter ID = Entergy
New Name = TestNamejon Old Name = TestNamejon
------------------------

I need to do this for 10 tables. I am looking for a way to do this programitaclly so I do not have to create a stored procedure for each of these Tables. And if the table def changes I do not have to change the SP.

Thanks for the speedy reply
LJ|||try gettin the names of the table from sysobjects (usign a cursor) and make generic code for ur process which will use the tablenames from the names in sysobject.

so u can do sumthin for as many tables as u want. with just one sp

No comments:

Post a Comment