我的数据库中有一个名为PresentYear
的列,用于将特定年份设置为学校的学年。 2014-15。我添加了default constraint
,将所有值设置为'N'
。在assigncurrentyear
按钮上点击我想将其设置为'Y'
,当我将另一年设置为PresentYear
时,之前设置的PresentYear
应重置为'N'
且仅限'Y'
表格中的一行应该有academicID
。我将通过PresentYear
设置stored procedure
,我正在使用create PROCEDURE [dbo].[USP_SetAcademicYear]
@AcademicID INT
AS
BEGIN
IF(@AcademicID != 0)
BEGIN
UPDATE dbo.Academic_Master
SET presentYear = 'y'
WHERE AcademicId = @AcademicID
END
END
这样做而且我被困在这里。
存储过程应该如何?
{{1}}
答案 0 :(得分:2)
使用Not Equal To运算符:
UPDATE Academic_Master
SET presentYear='N'
WHERE AcademicId <> @AcademicID
更新: 所以你的存储过程看起来像这样
create PROCEDURE [dbo].[USP_SetAcademicYear]
@AcademicID INT
AS
BEGIN
IF(@AcademicID!=0)
BEGIN
update Academic_Master
set presentYear='y'
WHERE AcademicId=@AcademicID
UPDATE Academic_Master
SET presentYear='N'
WHERE AcademicId <> @AcademicID
END
END
答案 1 :(得分:2)
只需更新需要更新的两行:
create PROCEDURE [dbo].[USP_SetAcademicYear]
@AcademicID INT
AS
BEGIN
IF(@AcademicID != 0)
BEGIN
UPDATE dbo.Academic_Master
SET presentYear = CASE
WHEN AcademicId = @AcademicID THEN 'y'
ELSE 'N' END
WHERE AcademicId = @AcademicID or presentYear = 'y'
END
END