使用存储过程检查实体是否存在的条件

时间:2014-09-11 05:41:25

标签: sql-server validation stored-procedures parameters

我有一个存储过程:

CREATE Procedure [dbo].[GetCOIOrganizations]  
(  
 @orgName varchar(75)   
)  
AS  
BEGIN  

 SET NOCOUNT ON  

 Select Id,  
  [Name],  
  Description,   
  AdmPersonId,  
  Active,  
  Reviewed,  
  DateCreated  
 FROM COIOrganization Org  
 WHERE Org.[Name] like @orgName + '%'  
  AND (Org.Reviewed = 0 OR (Org.Reviewed = 1 AND Org.Active = 1))  

 order by [Name] ASC  

END  

我想在此包含一个条件,如果实体(org。[Name])已经存在,那么给出一些验证。我该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以像这样使用IF EXISTS检查:

IF EXISTS ( SELECT TOP 1
                1
            FROM
                COIOrganization Org
            WHERE
                Org.[Name] LIKE @orgName + '%' )
BEGIN
    SET
            flag = 1
END
ELSE
BEGIN
    SET
            flag = 0
END