Click to See Complete Forum and Search --> : how to insert into a table where not exist?


fabdulla
01-07-2008, 10:54 AM
I have table in sql database. I have two varialbles that update two columns. I only want to update new data, if the data that is read exist in the table don't insert it. I am using vb.net -- sql commands. How can I used something like this
[code]
myCom.CommandText = ("INSERT INTO Jits ([Jit Number] , [Revision Date]) VALUES ('" & sname & "', '" & smodified & "') where not exists( select [Jit Number] from Jits where [Jit number]='" & sname & "')")
[\code]

Phil Weber
01-07-2008, 10:59 AM
http://www.google.com/search?q=sql+if+not+exist+insert

fabdulla
01-07-2008, 11:50 AM
i looked at the link it did not help me. i have two varialbes that reading file names in a folder. then the variables get inserted into a table in sql database. Now let say that some added a file to the folder the program will read the entire folder and file names. when I go insert into the table I don't want to insert similar names again. I want to check for new names and add them only. How to go by that. Thanks

mdb002
01-07-2008, 11:54 AM
You need to associate the exists clause with the insert table.



myCom.CommandText = ("INSERT INTO Jits ([Jit Number] , [Revision Date]) VALUES ('" & sname & "', '" & smodified & "') where not exists( select [Jit Number] from Jits AS J where [J].[Jit number]=[Jits].[Jit Number])")

I left out a "[" in the where clause.

fabdulla
01-07-2008, 12:05 PM
I ran what you send it giving an error "synatax near the word where"

Hack
01-09-2008, 07:31 AM
Did the inclusion of the "[" fix the error for you?

fabdulla
01-09-2008, 08:09 AM
You see what i have is a vb.net code that reads in file names from a folder directory. the file name and modified date are to be saved in a database in order for me to use it to a gridview. Also there is another column which is the title of a document. Now sice I have three columns , File Name, Title, Modified date. when I run this and bind it to gridview I want to be able to click on the any file name and be able to open the file from the directory. Also The Titles has to be entered from the data gridview using edit/Update command. Initially the Title is empty until a user addes a file to the folder they have to add the title from the gridview. If anyone knows how to do this or another way of approaching this task please let me know. Thanks

Hack
01-09-2008, 08:16 AM
:confused: Is this an entirely different question or are we still on the insert into a table if it doesn't exist' question?

fabdulla
01-09-2008, 10:57 AM
it is the same project. I just wrote the entire scope of the project. I was trying to test the where exists to see if i inserted more file names from the folder to the table I don't want to repeate or replace what is there. I want to check if the file name exists then don't add it unless it is modified.
something like this