I have the following table:
RecordID
Name
Col1
Col2
....
ColN
The RecordID
is BIGINT PRIMARY KEY CLUSTERED IDENTITY(1,1)
and RecordID
and Name
are initialized. The other columns are NULLs.
I have a function which returns information about the other columns by Name
.
To initialized my table I use the following algorithm:
- Create a LOOP
- Get a row, select its
Name
value - Execute the function using the selected name, and store its result in temp variables
- Insert the temp variables in the table
- Move to the next record
Is there a way to do this without looping?
You can simply say the following if you already have the records in the table.
While inserting new records, assuming RecordID is auto-generated, you can say
where @Name contains the value for the Name column.
Cross apply was basically built for this
Using APPLY
using cross apply in an update statement