I want to write a t-sql stored procedure (aka sproc) which selects 3 columns from ‘MyTable’. In addition, I want to update the table in the same sproc:
- I select the third value from the table.
- If it equals ‘true’, I want to update the relevant record in the table to ‘false’
I wasn’t sure what syntax should I use. Could you help me out?
ALTER procedure [dbo].[My_PROC]
@ID varchar(10)
AS
BEGIN
declare @Col3 bit;
set @Col3 = select Col3
from dbo.MyTable with (nolock)
where @ID = ID
if @Col3 = 'true'
update dbo.dbo.MyTable set col3 = 'false'
where @ID = ID
select Col1,
Col2,
Col3
from dbo.MyTable table with (nolock) where @ID = ID,
table.Col1,
table.Col2,
@Col3
END
edit: I want to return the original Col3 (not the updated value).
Use:
I don’t know what you’re intending for the final
SELECT, but I can update it once I understand what you intended.