如何在SQL中的IF EXISTS中使用AND条件?

前端之家收集整理的这篇文章主要介绍了如何在SQL中的IF EXISTS中使用AND条件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'X' AND COLUMN_NAME = 'Y') 

IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Z' AND COLUMN_NAME = 'A')
BEGIN
    UPDATE [dbo].[X] 
        SET Y= (SELECT inst.[A] FROM [dbo].[Z] s WHERE s.[B] = [dbo].[x].[B]);
END    
    GO

我想结合2 IF的限制,只有当两者都满意时才执行更新.有没有什么方法我可以俱乐部2如果EXISTS?

解决方法

简单:
IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'X' AND COLUMN_NAME = 'Y') 
AND EXISTS(SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Z' AND COLUMN_NAME = 'A')

BEGIN
    UPDATE [dbo].[X] 
        SET Y= (SELECT inst.[A] FROM [dbo].[Z] s WHERE s.[B] = [dbo].[x].[B]);
END    
GO
原文链接:https://www.f2er.com/mssql/82767.html

猜你在找的MsSQL相关文章