我的if语句似乎永远不会做任何事情。我从ASP页面输入@active,并通过从Cots Table中选择CActive来设置@cactive。你们这个陈述有什么不对吗?它为什么不执行?我正在使用SQL Server 2005
//Declare Variables Here (about 20)
AS
If @currentdate is null
SET @currentdate = GETDATE()
Update Cots
SET //UPDATE ALL FIELDS HERE
Where CoID = @CID
SET @cactive =
(
Select CActive
From Cots
Where CoID = @CID
)
If @cactive != @active
Begin
INSERT INTO Activity
(CoID,ActivityDate, ActivityName, ActivityNote, ActivityMediaContact, ActivityOwner)
Values (@CID, @currentdate, 'Co ' + @fname + ' ' + @lname + 'made inactive','Co made inactive on ' + CAST(@currentdate AS varchar(50)), 'User', 'User')
End
INSERT INTO Activity
(CoID,ActivityDate, ActivityName, ActivityNote, ActivityMediaContact, ActivityOwner)
Values (@CID, @currentdate, 'Updated ' + @fname + ' ' + @lname,'Updated on ' + CAST(@currentdate AS varchar(50)), 'User', 'User')
答案 0 :(得分:3)
如果其中任何一个为空,它们永远不会相等或不相等
If @cactive != @active
实施例
DECLARE @i int, @i2 int
IF @i <> @i2
PRINT 'not equal'
IF @i = @i2
PRINT 'equal'
使用ISNULL()/COALESCE()
或IS NULL/IS NOT NULL
答案 1 :(得分:1)
使用IS NULL最适合检查条件,就像在WHERE子句中一样。
你应该使用ISNULL()函数......
USE AdventureWorks2008R2;
GO
SELECT AVG(ISNULL(Weight, 50))
FROM Production.Product;
GO