码迷,mamicode.com
首页 > 其他好文 > 详细

insert into 和 where not exists

时间:2019-03-06 12:10:21      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:span   tar   tsql   int   ros   sso   class   style   ref   

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/3569bd60-1299-4fe4-bfa1-d77ffa3e579f/insert-into-with-not-exists?forum=transactsql

错误的语法

INSERT INTO [dbo].[geo_asso_type]

([geo_asso_type_id]

,[bound_asso_type]

,[updated_date])

VALUES

(11

,Province to City

,GETDATE()

WHERE NOT EXISTS

(SELECT 1 FROM [dbo].[geo_asso_type] WHERE [geo_asso_type_id] = 11)

 

方案1,前置not exists

IF NOT EXISTS (SELECT 1 FROM [dbo].[geo_asso_type] WHERE [geo_asso_type_id] = 11)  
BEGIN 
    INSERT INTO [dbo].[geo_asso_type]   
        ([geo_asso_type_id]  
        ,[bound_asso_type]  
        ,[updated_date])  
    VALUES 
        (11  
        ,Province to City 
        ,GETDATE())  
 
END 

 

方案2,通过select的方式插入数据

INSERT INTO [dbo].[geo_asso_type]         
(     
[geo_asso_type_id],        
[bound_asso_type],     
[updated_date]     
)        
SELECT    
     11,     
     Province to City,     
     GETDATE()     
WHERE NOT EXISTS(     
    SELECT 1   
    FROM [dbo].[geo_asso_type]   
    WHERE [geo_asso_type_id] = 11  
)   

 

insert into 和 where not exists

标签:span   tar   tsql   int   ros   sso   class   style   ref   

原文地址:https://www.cnblogs.com/chucklu/p/10482311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!