Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Here is the statement which I am using.
C#
INSERT INTO Course_Data (Branch,CC,C_Title,Sem,Credits) 
 values 
(select distinct from (select * from Course_Data) union (select * from TEMP1) a order by a.CC)

I encountered with error :
Incorrect syntax near the keyword 'select'.
Incorrect syntax near 'a'.

I have a table called Course_Data to which I am importing data from excel file using SqlBulkCopy. Before it inserts into Course_Data table I need to check if the row which it is inserting is already on Course_Data table. If it row exists then it should skip that entry and if it is not then that row has to be inserted. So to achieve this I created a temporary(Temp) table, inserted all the rows from excel to Temp, then select distinct values from Course_Data and Temp, and insert distinct values to Course_Data
Posted
Comments
Maciej Los 1-Mar-15 9:15am    
Does Course_data and TEMP1 table contain the same set of columns with the same data type?
partha143 1-Mar-15 9:16am    
Yes. They both have same table definations

Try this:
SQL
insert into course_data
select branch, cc, c_title, seem, credits from temp t where not exists
(
SELECT * FROM course_data cd1 WHERE cd1.branch=t.branch and cd1.cc=t.cc and cd1.c_title=t.c_title and cd1.sem=t.sem and cd1.credits=t.credits
)
 
Share this answer
 
Comments
Maciej Los 1-Mar-15 9:24am    
Looks promissing ;)
partha143 1-Mar-15 10:03am    
I tried your solution peter, but I am still unable to avoid duplication.
The error you get happens because you should not specify VALUES keyword when inserting from a SELECT statement.

What comes to the insert select combination and the distinct values, perhaps something like
SQL
INSERT INTO Course_Data (Branch, CC, C_Title ,Sem,Credits)
SELECT Branch, CC, C_Title, Sem, Credits 
FROM TEMP1
EXCEPT 
SELECT Branch, CC, C_Title, Sem, Credits 
FROM Course_Data
 
Share this answer
 
Comments
partha143 3-Mar-15 10:36am    
Thanks a lot Mika :)
Wendelius 3-Mar-15 12:25pm    
You're welcome :)
PLease, read my comment to the question.

Try this:
SQL
INSERT INTO Course_Data (Branch,CC,C_Title,Sem,Credits)
SELECT DISTINCT Branch,CC,C_Title,Sem,Credits
FROM (
    SELECT Branch,CC,C_Title,Sem,Credits
    FROM Course_Data
    UNION ALL
    SELECT Branch,CC,C_Title,Sem,Credits
   FROM TEMP1
) AS A
ORDER BY A.CC)


For further information, please see: INSERT (SQL)[^]
 
Share this answer
 
Comments
partha143 1-Mar-15 9:50am    
I tried your code and I am still unable to avoid insertion of duplicate values.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900