472,351 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,351 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 27674
"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...
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...
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...
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)....
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,...
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: ...
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,...
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...
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...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

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.