I'm trying to create a stored procedure that runs a select query and pulls an id (variable) and then do an update query to that id. Any help would be appreciated.
This is what I have:
CREATE PROCEDURE dbo.Lead_usp_getLead
@LeadId int output
AS
SELECT TOP 1
Leadid, LeadInitials, LeadFirstName, LeadSurname,
LeadHomeTelephoneNumber, LeadWorkTelephoneNumber,
LeadCellularNumber, LeadEMailAddress, IsLocked, uploadedDate
FROM
dbo.Lead
WHERE
IsLocked = 'False'
ORDER BY
uploadedDate;
UPDATE dbo.Lead
SET IsLocked = 'TRUE'
WHERE LeadId = @LeadId
DECLARE @leadid int
EXEC dbo.Lead_usp_getLead @leadId;
You can just combine them. No need to do two queries:
with toupdate as (
SELECT TOP 1 l.*
FROM dbo.Lead l
WHERE l.IsLocked = 'False'
ORDER BY l.uploadedDate
)
Update toupdate
SET IsLocked = 'TRUE';
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments