I am new in stored procedures.
ALTER PROCEDURE [dbo].[SP_MY_STORE_PROCEDURED]
(
@ID INT,
@NAME VARCHAR(50)
)
AS
BEGIN
UPDATE MY_TABLE
SET
WHEN
(( ID ) > 1)
THEN
ID=@ID
,
NAME = @NAME
END
I try to use when then for update my ID and Name
If Id is greater than 1 i want to update otherwise no update.
How can i do it ms sql?
Any help will be appreciated.
Thanks.
I think this is what you are after:
ALTER PROCEDURE [dbo].[SP_MY_STORE_PROCEDURED]
(
@ID INT,
@NAME VARCHAR(50)
)
AS
BEGIN
UPDATE MY_TABLE
SET NAME = @NAME
WHERE ID = @ID
END
You do not need to check ID>1 since you are checking the equality with @ID. If you want to be sure that this doesn't happen if @ID <=1 then you may try the following:
ALTER PROCEDURE [dbo].[SP_MY_STORE_PROCEDURED]
(
@ID INT,
@NAME VARCHAR(50)
)
AS
BEGIN
IF @ID > 1
UPDATE MY_TABLE
SET NAME = @NAME
WHERE ID = @ID
END
I'm not sure exactly what you're trying to update. Are you trying to change the name on a user record with id = @ID?
ALTER PROCEDURE [dbo].[SP_MY_STORE_PROCEDURED]
(
@ID INT,
@NAME VARCHAR(50)
)
AS
BEGIN
UPDATE MY_TABLE
SET Name = @Name
WHERE Id = @ID and @ID > 1
END
This should do it.
ALTER PROCEDURE [dbo].[SP_MY_STORED_PROCEDURE] ( @ID INT, @NAME VARCHAR(50) )
AS
BEGIN
IF @ID > 1
UPDATE MY_TABLE
SET Name = @Name
WHERE Id = @ID
END
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.