我是SQL Server的新手,我希望将一列中的值与另一列中的另一个值进行比较。流程是这样的:
if value from table1 != value from table 2
continue
...但我如何只比较列中的值。
如果我SELECT @variable=COLUMN FROM TABLE and IF @variable1!=@variable2
,则无效。
我正在使用SQL Server 2012。
以下是查询:
SELECT @AgentID=AGENTID
,@CreateTime1=CREATEDATE
FROM AGENT
SELECT @Agid=AgentNo
,@CreateTime2=[Join Date]
,@AccCount2=AgentID
FROM LOCALDB_AGENT
IF @CreateTime1>@CreateTime2
BEGIN
IF @AgentID!=@Agid
BEGIN
INSERT INTO LOCALDB_AGENT (AgentName,AgentID,AgentNo,Email,[Join Date],CreatedDate)
SELECT [FIRSTNAME],[AGENTID],[AGENTID],[EMAIL],CREATEDATE,GetDate() AS CreatedDate
FROM AGENT
WHERE @AgentID!=@Agid
SELECT @AccCount2=COUNT(*)
FROM LOCALDB_AGENT
WHERE [Status]='ACTIVE' AND CreatedDate=Getdate()
SET @msg = 'New Record to DMTM' + ' = ' + @AccCount2 + char(10)
PRINT @msg
END
ELSE
BEGIN
PRINT 'Data Already Exist'
END
END
答案 0 :(得分:-1)
选项1:
IF (SELECT 1) != (SELECT 2)
BEGIN
PRINT 'CONTINUE'
END
否则,您可以使用variable
存储列值,然后比较变量。
选项2:
DECLARE @Variable1 INT
DECLARE @Variable2 INT
SELECT @Variable1 = 1
SELECT @Variable2 = 2
IF @Variable1 != @Variable2
BEGIN
PRINT 'CONTINUE'
END
使用您的查询替换SELECT Statement