7

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.

5
  • 1
    Could you clarify what you're trying to do? Perhaps supply some sample data? Commented Aug 16, 2015 at 19:30
  • 3
    What would an insert with a where condition do? Are you trying to UPDATE maybe? Commented Aug 16, 2015 at 19:33
  • No condition can be satisfied by a row that is not yet in the table, I'm afraid. Commented Aug 16, 2015 at 19:33
  • What is means- trying to insert values into 1 column. Does it means other column values are already present but a specific column value is absent? Commented Aug 16, 2015 at 19:34
  • @Mureinik the title of the post is very clear. How does one make an insert dependent on a condition? Commented Jul 10, 2020 at 22:37

10 Answers 10

12

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.

Sign up to request clarification or add additional context in comments.

Comments

10

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>;

1 Comment

INSERT INTO SELECT command first select the record from another table, then insert, there is no where clause or conditional insert into main table
3

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')

Comments

2

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

Comments

2

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.

Comments

1

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

Comments

1

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.

Comments

0

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 :-)

Comments

0

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)

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.