So I'm having some issues producing a query with multiple temporary tables.
First I created the temporary tables.
CREATE GLOBAL TEMPORARY TABLE DIDV (DID VARCHAR(50))
INSERT INTO DIDV VALUES ('8090630909077434001');
INSERT INTO DIDV VALUES ('7471990179373590001');
INSERT INTO DIDV VALUES ('7605256906013877001');
INSERT INTO DIDV VALUES ('1604387368720407001');
INSERT INTO DIDV VALUES ('3512739534818409001');
CREATE GLOBAL TEMPORARY TABLE KEYW (KEW VARCHAR(50))
INSERT INTO KEYW VALUES ('%Corporate Insurance Pol%');
INSERT INTO KEYW VALUES ('%Tax Return%');
INSERT INTO KEYW VALUES ('%Audit%');
INSERT INTO KEYW VALUES ('%Corporate Governance%');
INSERT INTO KEYW VALUES ('%Board%');
INSERT INTO KEYW VALUES ('%Shareholder%');
Then I write my query as follows. It seems like a simple query. However, I get the error 'ORA-00918: column ambiguously defined'. I don't know if I can do a join of the 2 temporary tables, especially they do not have any relationship. I just want to query result where the unique id's are in the DIDV table and where the title and comment columns do not contain the KEW fields.
SELECT TO_CHAR(DID) DID, TO_CHAR(DROOTPARENTID) PARENTID, DBARCODE,
DDOCTITLE, XCOMMENTS, XDIVISION, DEXTOBJECTTYPE
FROM PSA.URM_EXTITEMS_PSA, DIDV, KEYW
WHERE PSA.URM_EXTITEMS_PSA.DID = DIDV.DID
AND PSA.URM_EXTITEMS_PSA.DDOCTITLE NOT LIKE KEYW.KEW
AND PSA.URM_EXTITEMS_PSA.XCOMMENTS NOT LIKE KEYW.KEW;
Any help understanding this is appreciated.
Please let me know if any clarification is needed.
A table DIDV contains a column named DID
A table URM_EXTITEMS_PSA also contains a column named DID (I know this because a where condition contains a clause WHERE PSA.URM_EXTITEMS_PSA.DID = DIDV.DID ).
A select statement in your query contains an expression referencing the column DID:
SELECT TO_CHAR(DID) ....
Oracle doesn't really know (I don't know too) which DID do you want to get in a result - the 'DID' column which comes from DIDV table or the DID column which comes from URM_EXTITEMS_PSA table. Therefore Oracle throws an error ORA-00918: column ambiguously defined.
Just append an aliast to this DIV column in the select clause, either DIDV or URM_EXTITEMS_PSA, in this way:
SELECT TO_CHAR(DIDV.DID) ....
I know that WHERE clause contains PSA.URM_EXTITEMS_PSA.DID = DIDV.DID and actually both DIDs are equal, so Oracle should be smart enaugh to say Oh, yes, he wrote PSA.URM_EXTITEMS_PSA.DID = DIDV.DID, so I can pick DID from any table ....
Unfortunatelu Oracle is not so smart (maybe they improve this in a new version ?), and for now you must append an alias or a table name.
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.
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.
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>;
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')
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
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.
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
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.
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 :-)
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)
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.
I have a SQL Server 2008 database. The database has a stored procedure which receives two strings as parameters. One parameter is used to build a temp table which will usually only have 1 or 2 rows but theoretically could have more.
For each row in the temp table, I need to insert a row into a different table that consists of the other parameter and the contents of the temp table. Is there a way to do this without a cursor?
I've tried variations on the following:
Pseudo code:
procedure InsertLinks(#Key varchar(36), #LinkKey varchar(36)
tempLinks Table = getLinks(#LinkKey)
Insert into MyTable (Key, LinksTo) Values (#Key, Select LinksTo From tempLinks)
The VALUES clause is messed up - you have a single value comma a table. That's not valid.
The following should work just fine:
INSERT INTO MyTable (Key, LinksTo)
SELECT #Key, LinksTo
FROM tempLinks
Good day,
I was wondering if it is possible to use an INSERT-OUTPUT statement in such a way as to provide the value(s) for another, outer, INSERT statement. That way values can be added to an entity table and an intersection table in a single statement - I hope I'm wording this effectively. For example:
INSERT INTO [#tblIntersect] ([Entity1ID], [Entity2ID])
VALUES
(
INSERT INTO [#tblEntity1] ([Value])
OUTPUT [inserted].[ID] AS [entity1ID], #entity2ID AS [entity2ID]
VALUES ('One')
)
So the inner INSERT-OUTPUT statement will add a new entity to table #tblEntity1. The new entity's ID (which is set as IDENTITY(1, 1) will then be returned through the OUTPUT statement, along with a static value (which I already have in my code), to provide the two values for the outer INSERT statement.
The reason I think it might be possible is because execution of the inner INSERT-OUTPUT statement on its own returns a table anyway, and such output can usually be used to provide values for INSERT statements.
Obviously this example doesn't work; I was hoping it's just a simple syntax problem.
Thank you in advance for any comments and advice.
Your requirement is possible according to the documentation.
Assuming #tblIntersect has two matching id columns this should work
INSERT INTO [#tblEntity1] ([Value])
OUTPUT [inserted].[ID] AS [entity1ID], #entity2ID AS [entity2ID]
INTO #tblIntersect
VALUES ('One')
I have created table #temp with columns id as int identity(1,1) and name as varchar.
Say suppose I am writing the following 2 different statements for inserting rows:
insert into #temp (name) select ('Vikrant') ;
insert #temp (name) select ('Vikrant')
I want to ask what is the difference between these two types of insert statements?
Is there really any difference in between these insertions?
From the MSDN documentation:
[INTO]
Is an optional keyword that can be used between INSERT and the target table.
There is no difference between the two statements.