t-sql update table variale 1

t-sql update table variale

UPDATE @searchResult
SET [record_id] = r.[record_id], 
    [record_name] = r.[record_name]
FROM @searchResult SR
CROSS APPLY (
    SELECT TOP 1 *
    FROM [records]
    WHERE [record_name] LIKE [name_template]   -- Your wish, but do you really need LIKE matching??
    ORDER BY [record_id] DESC
) AS r;

Here is what the above code is Doing:
1. It creates a table variable to hold the results of the search.
2. It populates the table variable with the results of the search.
3. It updates the table variable with the record_id and record_name of the record that matches the search result.
4. It returns the table variable.

Similar Posts