473,383 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

INSERT INTO WHERE NOT EXIST into the same table

Hi,

I am trying to add a row to my table but I get the error message
"invalid column name SOBN and BN1" on this statement. Basically, I am trying
to add the row into the same table that I am searching if it does not find
SOBN = 5 and BN1 =3. What is the problem? Do I have to create a primary
key? Is it possible to have more than 1 primary key (i.e. secondary
....... ).

strCommand = "INSERT INTO tblSQL_2 (SOBN, BN1) SELECT SOBN , BN1 WHERE NOT
EXISTS (SELECT * FROM tblSQL_2 WHERE SOBN = 5 AND BN1 = 3)";

Thanks
Roy

Nov 19 '05 #1
6 27828
"Roy Gourgi" wrote...
I am trying to add a row to my table but I get the
error message "invalid column name SOBN and BN1"
on this statement.
Without knowing what database you're targeting, I would have guessed that
you rather should have gotten another error message, pointing to the fact
that you don't provide a table name for the "SELECT"

INSERT INTO tblSQL_2 (SOBN, BN1)
SELECT SOBN , BN1
(something is missing here...)
WHERE NOT EXISTS
(SELECT *
FROM tblSQL_2
WHERE SOBN = 5 AND BN1 = 3)

There could also be the case that there isn't any columns named SOBN or BN1
in your table.
Basically, I am trying to add the row into the
same table that I am searching if it does not
find SOBN = 5 and BN1 =3. What is the problem?
It isn't comprehensible what you're trying to say.

You say that you want to add some rows to a table...

From what table?

The same table? Then I would have to ask you *why*? If you can find them in
that table, then they already *are* in that table...

Say that you want to get the rows from *another* table, then it *could*
work, but consider what it is you really want to do, and what the SQL
statement really does for you.

INSERT INTO tblSQL_2 (SOBN, BN1)
SELECT SOBN , BN1
FROM anotherTable
WHERE NOT EXISTS
(SELECT *
FROM tblSQL_2
WHERE SOBN = 5 AND BN1 = 3)

This will select SOBN and BN1 from *all* rows in anotherTable, but only if
there *isn't* a row in tblSQL_2 with SOBN = 5 and BN1 = 3. If there is any
row in tblSQL_2 with SOBN = 5 and BN1 = 3, then *no* rows will be inserted.
Do I have to create a primary key?
No, you don't have to, and the question of PKs or not doesn't seem relevant
in what you're trying to do.
Is it possible to have more than 1 primary key (i.e. secondary


Not in general, though a SQL-compliant database should have the possibility
to add more unique indexes, which would fill about the same purpose as
additional PKs.

However, I don't think that's really the problem here.

// Bjorn A
Nov 19 '05 #2
strCommand = "INSERT INTO tblSQL_2 (SOBN, BN1) SELECT SOBN , BN1 FROM
tblSQL_2 WHERE NOT
EXISTS (SELECT * FROM tblSQL_2 WHERE SOBN = 5 AND BN1 = 3)";

You forgot from tblSQL_2. Aside from that I have nothing else nice to say
about this so I will refrain.
--

Derek Davis
dd******@gmail.com

"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:cj********************@weber.videotron.net...
Hi,

I am trying to add a row to my table but I get the error message
"invalid column name SOBN and BN1" on this statement. Basically, I am
trying
to add the row into the same table that I am searching if it does not find
SOBN = 5 and BN1 =3. What is the problem? Do I have to create a primary
key? Is it possible to have more than 1 primary key (i.e. secondary
...... ).

strCommand = "INSERT INTO tblSQL_2 (SOBN, BN1) SELECT SOBN , BN1 WHERE NOT
EXISTS (SELECT * FROM tblSQL_2 WHERE SOBN = 5 AND BN1 = 3)";

Thanks
Roy

Nov 19 '05 #3
Hi,

What I meant to do is this actually, as I did not understant how it worked
before. This works without the From.
strCommand = "INSERT INTO tblSQL_2 (SOBN ,BN1 ) select 5,3 WHERE not exists
(select * from tblSQL_2 where SOBN = 5 AND BN1 = 3)";

Thanks

Roy

"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:cj********************@weber.videotron.net...
Hi,

I am trying to add a row to my table but I get the error message
"invalid column name SOBN and BN1" on this statement. Basically, I am
trying
to add the row into the same table that I am searching if it does not find
SOBN = 5 and BN1 =3. What is the problem? Do I have to create a primary
key? Is it possible to have more than 1 primary key (i.e. secondary
...... ).

strCommand = "INSERT INTO tblSQL_2 (SOBN, BN1) SELECT SOBN , BN1 WHERE NOT
EXISTS (SELECT * FROM tblSQL_2 WHERE SOBN = 5 AND BN1 = 3)";

Thanks
Roy

Nov 19 '05 #4
It worked when I used the constants 5,3 after the select statement, but what
if I wanted to use variables instead, how would I do that. In other words I
would like to do this.

int var1 = 5;
int var2 = 3;

strCommand = "INSERT INTO tblSQL_2 (SOBN ,BN1 ) values (var1,var2) WHERE not
exists
(select * from tblSQL_2 where SOBN = 5 AND BN1 = 3)";

So basically what I want to do is search the table for SOBN=5 and BN1=3 and
if it does not find them, then I want to add them in the same table. How
would I do that.

Thanks
Roy

"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:cj********************@weber.videotron.net...
Hi,

I am trying to add a row to my table but I get the error message
"invalid column name SOBN and BN1" on this statement. Basically, I am
trying
to add the row into the same table that I am searching if it does not find
SOBN = 5 and BN1 =3. What is the problem? Do I have to create a primary
key? Is it possible to have more than 1 primary key (i.e. secondary
...... ).

strCommand = "INSERT INTO tblSQL_2 (SOBN, BN1) SELECT SOBN , BN1 WHERE NOT
EXISTS (SELECT * FROM tblSQL_2 WHERE SOBN = 5 AND BN1 = 3)";

Thanks
Roy

Nov 19 '05 #5
Hi Roy,

A SQL statement is a string. As such, you can build it to contain any values
you wish. Alternatively, and this is probably a better solution, use a
parameterized Stored Procedure.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
If you push something hard enough,
it will fall over.
- Fudd's First Law of Opposition

"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:wp****************@weber.videotron.net...
It worked when I used the constants 5,3 after the select statement, but
what if I wanted to use variables instead, how would I do that. In other
words I would like to do this.

int var1 = 5;
int var2 = 3;

strCommand = "INSERT INTO tblSQL_2 (SOBN ,BN1 ) values (var1,var2) WHERE
not exists
(select * from tblSQL_2 where SOBN = 5 AND BN1 = 3)";

So basically what I want to do is search the table for SOBN=5 and BN1=3
and if it does not find them, then I want to add them in the same table.
How would I do that.

Thanks
Roy

"Roy Gourgi" <ro***@videotron.ca> wrote in message
news:cj********************@weber.videotron.net...
Hi,

I am trying to add a row to my table but I get the error message
"invalid column name SOBN and BN1" on this statement. Basically, I am
trying
to add the row into the same table that I am searching if it does not
find
SOBN = 5 and BN1 =3. What is the problem? Do I have to create a primary
key? Is it possible to have more than 1 primary key (i.e. secondary
...... ).

strCommand = "INSERT INTO tblSQL_2 (SOBN, BN1) SELECT SOBN , BN1 WHERE
NOT
EXISTS (SELECT * FROM tblSQL_2 WHERE SOBN = 5 AND BN1 = 3)";

Thanks
Roy


Nov 19 '05 #6
"Roy Gourgi" wrote...
It worked when I used the constants 5,3 after the select
statement, but what if I wanted to use variables instead,
how would I do that. In other words I would like to do this.

int var1 = 5;
int var2 = 3;

strCommand = "INSERT INTO tblSQL_2 (SOBN ,BN1 ) values (var1,var2) WHERE
not exists
(select * from tblSQL_2 where SOBN = 5 AND BN1 = 3)";


The "easy" solution:

strCommand = "INSERT INTO tblSQL_2 (SOBN, BN1) " +
"values (" + var1 + ", " + var2 + ") "
"WHERE not exists " +
"(select 'x' from tblSQL_2 where SOBN = " +
var1 + " AND BN1 = " + var2 + ")";

The "better" solution involves a parameterized statement, where you simply
have the SQL string with "placeholders" for the values.

OdbcCommand cmd = new OdbcCommand();

...

strCommand = "INSERT INTO tblSQL_2 (SOBN, BN1) " +
"values ( ?, ? ) " +
"WHERE not exists " +
" (select 'x' from tblSQL_2 " +
"WHERE SOBN = ? AND BN1 = ? )";

cmd.CommandText = strCommand;

OdbcParameter par1 = new OdbcParameter();
par1.OracleDbType = OdbcType.Int;
par1.Value = var1;

OdbcParameter par2 = new OdbcParameter();
par2.OracleDbType = OdbcType.Int;
par2.Value = var2;

cmd.Parameters.Add(par1);
cmd.Parameters.Add(par2);
cmd.Parameters.Add(par1);
cmd.Parameters.Add(par2);

(Note 1: Syntax of "placeholders" differ between ADO.NET providers)

(Note 2: I replaced * with 'x' as it "may" increase performance, depending
on which database you're targeting)
// Bjorn A


Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Karen Middleton | last post by:
In MS Access I can do in one SQL statement a update if exists else a insert. Assuming my source staging table is called - SOURCE and my target table is called - DEST and both of them have the...
1
by: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
28
by: Giggle Girl | last post by:
Can someone show me how to insert a row at any given row index of an already created table? It only has to work in IE6 (used on intranet at work). Specifically, if a table is 20 rows in total...
3
by: Mike Charney | last post by:
I have a two part question: First I want to insert data into a table and I am using the following command: INSERT INTO tblmain SELECT field1, field2, etc... FROM tblimport WHERE ?????? The...
9
by: rhaazy | last post by:
Using MS SQL 2000 I have a stored procedure that processes an XML file generated from an Audit program. The XML looks somewhat like this: <ComputerScan> <scanheader>...
6
by: rn5a | last post by:
During registration, users are supposed to enter the following details: First Name, Last Name, EMail, UserName, Password, Confirm Password, Address, City, State, Country, Zip & Phone Number. I am...
1
by: deepaks85 | last post by:
Dear Friends, Please help me out where I am doing mistake...I am trying to insert records into my MSSQL database...but it always take me into the "page cannot be displayed"....Here is the code.......
2
by: lenygold via DBMonster.com | last post by:
Hi Everebody: I have a table: CREATE TABLE CROSS_REFERENCE (ROW# INTEGER NOT NULL ,KEY_WORD CHAR(16) NOT NULL ,QUERY_DESCR VARCHAR(330) NOT NULL ,PRIMARY KEY (ROW#,KEY_WORD)); It is a...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.