Xavi
Xavi

Reputation: 177

SQL Insert Query With Condition

I am trying to insert values into 1 column of a table when a condition is satisfied.

Note: The table already contains data for all the columns but for 1 which is empty. I would like to insert value into this 1 column depending on the WHERE clause.

I have this query:

INSERT INTO <TABLE_NAME>
(COLUMN_NAME)
(VALUE)
WHERE <CONDITION>

I am getting an exception:

Incorrect Syntax Near WHERE Keyword

I am able to do this using UPDATE:

UPDATE <TABLE_NAME>
SET <COL_NAME>
WHERE <CONDITION>

But was wondering why the INSERT query was failing. Any advise appreciated.

Upvotes: 7

Views: 139170

Answers (10)

Madhuraank B
Madhuraank B

Reputation: 103

INSERT syntax cannot have WHERE clause. The only time you will find INSERT has WHERE clause is when you are using INSERT INTO...SELECT statement.

Upvotes: 1

Iswarya
Iswarya

Reputation: 1

If the data need to be added for a column for an existing row then it’s UPDATE.

INSERT is creating a new row in the table.

For conditional INSERT, you can use the MERGE command.

Upvotes: 0

Wongani Kaluwa
Wongani Kaluwa

Reputation: 39

I know this is kinda late, for those who still want to use the where clause in an insert query, it's kinda possible with a hack.

My understanding is that, you want to insert only if a condition is true. Let's assume you have a column in your database "surname" and you want to insert only if a surname doesn't exist from the table.

You kinda want something like INSERT INTO table_name blha blha blah WHERE surname!="this_surname".

The solution is to make that cell unique from your admin panel.

Upvotes: 2

cider
cider

Reputation: 407

Private case I found useful: Conditional insert which avoids duplications:

-- create a temporary table with desired values
SELECT 'Peter' FirstName, 'Pan' LastName
INTO #tmp
    
-- insert only if row doesn't exist    
INSERT INTO Persons (FirstName, LastName)
SELECT *
FROM #tmp t
WHERE NOT EXISTS (SELECT * FROM Persons where FirstName=t.FirstName and LastName=t.LastName)

Upvotes: 0

o-o-awake-o-o
o-o-awake-o-o

Reputation: 397

I take it the code you included is simply a template to show how you structured your query. See the SO questions here, here and the MSDN question here.

In SQL Server (which uses Transact-SQL aka T-SQL) you need an UPDATE query for INSERT where columns already have values - by using the answer @HaveNoDisplayName gave :)

If you are executing INSERT / UPDATE from code (or if you need it regularly) I would strongly recommend using a stored procedure with parameters.

You could extend the procedure further by adding an INSERT block to the procedure using an IF-ELSE to determine whether to execute INSERT new record or UPDATE an existing, as seen in this SO answer.

Finally, take a look at SQLFiddle for a sandbox playground to test your SQL without risk to your RDMS :-)

Upvotes: 0

aasanchez
aasanchez

Reputation: 169

The SQL Insert dont accept where parameters, you could check this: SQL Insert Definition...

I do not know the whole question of what you want to do, but just using the INSERT statement is not possible, however it is possible to condition the insertion of data into a table, if this data is dependent on another table or comes from another table ... check here... SQL Insert explain in wikipedia

like this:

Copying rows from other tables

INSERT INTO phone_book2
SELECT *
FROM   phone_book
WHERE  name IN ('John Doe', 'Peter Doe')

or

INSERT INTO phone_book2 ( [name], [phoneNumber] )
SELECT [name], [phoneNumber]
FROM   phone_book
WHERE  name IN ('John Doe', 'Peter Doe')

Upvotes: 3

TheBrokenSpoke
TheBrokenSpoke

Reputation: 381

In order to add a value into the one column when the rows are already populated, you will need to use the update statement.
If you need to insert a new row that has a where clause, you will need to use an insert into select statement:

INSERT INTO <table> (<columns>)
SELECT <columns>
FROM <table>
WHERE <condition>;

Upvotes: 10

HaveNoDisplayName
HaveNoDisplayName

Reputation: 8487

As I understand your problem, you already have data in one row, and one column in that row does not have value, so you want to add value in to that column.

This the scenario for Update existing row, not the insert new row. You have to use UPDATE clause when data already present and you want to modify record(s). Choose insert when You want to insert new row in table.

So in your current scenario, Update Clause is your friend with Where Clause as you want to modify subset of records not all.

UPDATE <TABLE_NAME>
SET <COL_NAME>
WHERE <CONDITION>

INSERT Clause does not have any Where Clause as per any RDBMS syntax(I think). Insert is condition less sql query, While SELECT, UPDATE, DELETE all are conditional commands, you can add Where Clause in all later ones.

Upvotes: 12

Rastislav
Rastislav

Reputation: 53

Based on your question I have the feeling that you are trying to UPDATE a column in a table rather than insert.

Something like:

UPDATE column SET value WHERE different_column_value = some_value

Upvotes: 2

Prudhvi Konda
Prudhvi Konda

Reputation: 297

Insert statement will insert a new record. You cannot apply a where clause to the record that you are inserting.

The where clause can be used to update the row that you want. update SET = where .

But insert will not have a where clause. Hope this answers your question

Upvotes: 1

Related Questions