简体   繁体   English

如果填充,则跳过其他语句

[英]If Else Statement to Skip Value if populated

I am trying to create an if else script to skip a record if there is a specific value in a column of the table and continue to insert records into a different table. 我试图创建一个if else脚本,以在表的列中有特定值时跳过一条记录,并继续将记录插入到另一个表中。

How do I create the script to perform this action? 如何创建脚本来执行此操作?

IF EXISTS (Select * From Table A where X =1)
BEGIN
Do nothing
END
ELSE
BEGIN 
INSERT INTO TABLE Y
SELECT * FROM TABLE Z
END

Instead, write a single statement: 而是编写一个语句:

INSERT INTO TABLE Y
    SELECT *
    FROM TABLE Z
    WHERE NOT EXISTS (Select 1 From Table A where X = 1);

The conditional is not needed at all. 完全没有条件的。

If you want it in a proc to execute, use it this way 如果您希望它在proc中执行,请以这种方式使用

create procedure usp_insert 
as
BEGIN 
declare @rowcount int = (Select count(*) From TableA where X <>'1')
Begin
if @rowcount>=1 
INSERT INTO TABLE Y
SELECT * FROM TABLE Z
end
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM