0

I am still pretty new to everything and now I am trying to validate my input SQL.

My powershell script allows me to se some variables which I want to enter in a selected database. What I need is a check if the entry already exists or not. And if it exists my script shall stop.

Script part :

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

    "stop script but how?"

else

    insert into ......

So I need something which replaces "stop script but how?" somehow :)

RayofCommand
  • 4,054
  • 17
  • 56
  • 92

1 Answers1

7

Why do you need to "stop" the script? Reverse your logic - if the thing doesn't exist, only then do the rest of the stuff.

IF NOT EXISTS (SELECT ...)
BEGIN
    -- do all the things
END
Andriy M
  • 76,112
  • 17
  • 94
  • 154
Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
  • 1
    [I'd prefer a prize, I think](http://www.biography.com/imported/images/Biography/Images/Profiles/P/Vincent-Price-9446990-1-402.jpg), but thanks. :-) Credit to @JNK for the image. – Aaron Bertrand Jan 08 '14 at 16:24