7

I am trying to programmatically enter values into my table.

I cannot use a straight Select @variables. I have to use the keyword Values.

How can I create a where clause when using Values in the insert into.

I am trying to avoid duplicates

  DECLARE @MyID INT
  DECLARE @Phone varchar(10)
  DECLARE @MyDATE DateTime
  DECLARE @Agent as varchar(50)
  DECLARE @Charge as varchar(50)
  DECLARE @Vendor as varchar(50)

  SET @MyID = 215199999
  SET @Phone = '9999999999'
  SET @MyDATE = '2010-12-04 11:56:12.000'
  SET @Agent = 'fbrown'
  SET @Charge = 'NO'
  SET @Vendor = 'NO'

  INSERT INTO [MyDB].[dbo].[Accounts]
  (MyID,Phone,MyDate,Agent,Charge,Vendor)
  VALUES (
  @MyID
  ,@Phone
  ,@MyDATE
  ,@Agent
  ,@Charge
  ,@Vendor 
  ) WHERE MyID NOT IN (@MyID)
Internet Engineer
  • 2,514
  • 8
  • 41
  • 54

5 Answers5

24
IF NOT EXISTS(SELECT 1 FROM [MyDB].[dbo].[Accounts] WHERE MyID = @MyID)
    INSERT INTO [MyDB].[dbo].[Accounts]
        (MyID, Phone, MyDate, Agent, Charge, Vendor)
        VALUES 
        (@MyID, @Phone, @MyDATE, @Agent, @Charge, @Vendor)
Joe Stefanelli
  • 132,803
  • 19
  • 237
  • 235
3

Try using

if not exists ( select top 1 * from [MyDB].[dbo].[Accounts] Where MyID = @MyID )
INSERT INTO [MyDB].[dbo].[Accounts]
  (MyID,Phone,MyDate,Agent,Charge,Vendor)
  VALUES (
  @MyID
  ,@Phone
  ,@MyDATE
  ,@Agent
  ,@Charge
  ,@Vendor 
  )
Siva Charan
  • 17,940
  • 9
  • 60
  • 95
1

Also the Merge (UPSERT) option is a good option for a single execute. in this example the when matched is not filled, but you could add the WHEN matched statement and update timestamps or counters.

 MERGE
   Accounts AS target
USING
(select  @MyID as myID ) AS source
ON
   target.myID = source.myID 

WHEN NOT MATCHED THEN
INSERT (MyID,Phone,MyDate,Agent,Charge,Vendor)
  VALUES (
  @MyID
  ,@Phone
  ,@MyDATE
  ,@Agent
  ,@Charge
  ,@Vendor 
  );
langeleppel
  • 121
  • 2
  • 3
1

If you are trying to make sure that the MyID column doesn't contain any duplicates, you have at least 3 choices: 1) make the column unique (create an index on that column and declare it as unique, or, better yet, a primary key) 2) make the column auto-increment. This way, you don't even need to assign values to it. 4) you can use Joe Stefanelli's solution (on this thread). It's programmer friendly and alows you to assign any value you want.

Constantin Baciu
  • 224
  • 4
  • 11
0

As a rule of thumb, I prefer to avoid Branching Logic (using IF) whenever possible.
This is because the Query Plan is Cached for future runs based on the 1st-Run it happened to use
    (which is only one scenario of possible branches/routes you could take).

To avoid this, I include Where-Clauses in all DML Statements (when applicable).
This ensure all possible DML Operations are included in the Query Plan.
If I find this isn't possible (or too over-complicated), then that's the code-smell I use to determine if I should create a new Sproc to Branch my logic into.

With that out of the way, this is how you may use a Where-Clause for your Insert-Statement:

INSERT [MyDB].[dbo].[Accounts]
       (MyID, Phone, MyDate, Agent, Charge, Vendor)
SELECT @MyID,@Phone,@MyDate,@Agent,@Charge,@Vendor
 WHERE NOT EXISTS (SELECT * FROM [MyDB].[dbo].[Accounts] WHERE MyID = @MyID)

Side Note: Avoid T-SQL's MERGE (Upsert) at all cost. In my experience it is best to write out your Inserts and Updates separately and to always Update before Insert. This avoids potential random/intermittent Deadlocks when Updating a Newly Inserted Record (that is still temporarily Locked after being freshly minted).

MikeTeeVee
  • 18,543
  • 7
  • 76
  • 70