找出表中是否已存在值

时间:2014-03-14 12:05:30

标签: sql-server

我在表名中有一列作为LOCATION CODE作为Company_Location 我想知道,如果用户输入Location_Code,它不应该已经存在于表

3 个答案:

答案 0 :(得分:2)

也许是这样的:

IF NOT EXISTS(
              SELECT NULL FROM Company_Location 
              WHERE Company_Location.[LOCATION CODE]=<Some value>)
BEGIN
    INSERT INTO Company_Location([LOCATION CODE])VALUES(<Some value>)
END

答案 1 :(得分:0)

    if exists (select Location_code from Company_Location where Location_code=<something>
begin
    ...Do something here!!
end
else 
PRINT 'records already exist'

答案 2 :(得分:0)

也许

SELECT case when [LOCATION CODE] IS NOT NULL then 'Yes'  else 'No' end
FROM Company_Location
WHERE companyID = <value>