Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MS-SQL: Insert value only if value not present

Posted on 2004-09-25
7
Medium Priority
?
616 Views
Last Modified: 2012-05-05
I'm trying to be more selective on my row inserts. Probably better than deleting duplicate rows later. So, I have the following syntax which is incorrect, but you'll get the idea I'm attempting.........

INSERT INTO table1 (col_1) VALUES ( '64.39.21.112' )
(select col_1, count(*) as counted
from table1
where col_1 value not exist)
0
Comment
Question by:kvnsdr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12152269
INSERT INTO table1 (col_1)
SELECT '64.39.21.112'
WHERE NOT EXISTS (SELECT '64.39.21.112' FROM table1)
0
 
LVL 6

Expert Comment

by:jchopde
ID: 12153449
DECLARE @myValue varchar(20)
SET @myValue = '64.39.21.112'

IF NOT EXISTS (SELECT 1 FROM table1 WHERE col_1 = @myValue)
  INSERT INTO table1 (col_1) values (@myValue)
ELSE
  --whatever you want to do if the record exists already
  --No need for ELSE if you want to do nothing when the record exists

Alternatively, slightly alter jdlambert1's solution above as follows --

DECLARE @myValue varchar(20)
SET @myValue = '64.39.21.112'

INSERT INTO table1 (col_1)
SELECT @myValue
FROM table1
WHERE NOT EXISTS (SELECT 1 FROM table1 WHERE col_1 = @myValue)
0
 
LVL 7

Expert Comment

by:FDzjuba
ID: 12154285
you can use trigger, i have made it as DELETE from inserted just because i don't know what columns you want to validate


create INS_table1 on table1
INSTEAD OF INSERT
as
DELETE inserted
FROM (SELECT * FROM authors)

insert into table1
SELECT * FROM inserted
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 7

Expert Comment

by:FDzjuba
ID: 12154297
sorry, got the above wrong, trigger would be like

CREATE TRIGGER INS_table1 on table1
INSTEAD OF INSERT
as

INSERT INTO table1
SELECT * FROM inserted WHERE NOT EXISTS (SELECT * FROM table1 INNER JOIN inserted ON inserted.col1=table1.col1)

with this trigger you will always avoid inserting dublicated col1
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12155192
The current code needs to be amended somehow. It inserts string values into the table. I use C# .NET.........

INSERT INTO table1 (col_1) VALUES ( ' " + str_col_1.Trim() + " ' ) ";
0
 
LVL 6

Accepted Solution

by:
jchopde earned 500 total points
ID: 12155310
I do not claim to know C# at all but based on what your wrote above, use the following --

"IF NOT EXISTS (SELECT 1 FROM table1 WHERE col_1 = '" + str_col_1.Trim() + "')  INSERT INTO table1 (col_1) values ('" + str_col_1.Trim() + " ' ) ";
0
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12155675
"IF NOT EXISTS... INSERT INTO..." is two separate commands. While it can work, it's better to use a single command:

If you have string variables named "IP" & "sql":

IP = "64.39.21.112"
sql = "INSERT INTO table1 (col_1) SELECT '" + IP + "'WHERE NOT EXISTS (SELECT '" + IP + "' FROM table1)"
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question