Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否使用and if exists语句验证sql中的值?_Sql_Sql Server 2012_Validating - Fatal编程技术网

是否使用and if exists语句验证sql中的值?

是否使用and if exists语句验证sql中的值?,sql,sql-server-2012,validating,Sql,Sql Server 2012,Validating,我对任何事情都还很陌生,现在我正在尝试验证我的输入SQL 我的powershell脚本允许我使用要输入到选定数据库中的一些变量。我需要的是检查条目是否已经存在。如果它存在,我的脚本将停止 脚本部分: if exists(select * from [DB1].dbo.[table1] where Name = '$variable1') "stop script but how?" else insert into ...... 因此,我需要一些东

我对任何事情都还很陌生,现在我正在尝试验证我的输入SQL

我的powershell脚本允许我使用要输入到选定数据库中的一些变量。我需要的是检查条目是否已经存在。如果它存在,我的脚本将停止

脚本部分:

if exists(select * from [DB1].dbo.[table1]
            where Name = '$variable1')

    "stop script but how?"

else

    insert into ......
因此,我需要一些东西来代替“停止脚本,但如何停止?”)

为什么需要“停止”脚本?颠倒你的逻辑——如果这个东西不存在,那么只做剩下的事情

IF NOT EXISTS (SELECT ...)
BEGIN
    -- do all the things
END
,但谢谢。:-)该图像归功于@JNK。