Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Inserting into a table

Author  Topic 

Syan
Starting Member

3 Posts

Posted - 2005-11-30 : 04:20:06
Hi, i am new to SQL. I have this problem here, i need a little help.

How do i insert data into a table only when there is no such record in the existing table? I have this:

INSERT INTO ptable ( symptoms, weight, solutions ) VALUES ( '"+str+"', "+percent+", '"+str3+"' );

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-11-30 : 04:30:43
>>How do i insert data into a table only when there is no such record in the existing table? I have this:

Is this?

If not exists(Select * from ptable where <condition>)
Insert........

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Syan
Starting Member

3 Posts

Posted - 2005-11-30 : 21:11:25
I tried doing this:

IF NOT EXISTS (SELECT 1 FROM ptable WHERE symptoms = '"+str+"' AND weight = '"+percent+"' AND solutions = '"+str3+"') INSERT INTO ptable (symptoms,weight,solutions) VALUES ('"+str+"',"+percent+",'"+str3+"');

but it couldn't work. Is it right?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2005-11-30 : 21:18:15
quote:
Originally posted by Syan

I tried doing this:

IF NOT EXISTS (SELECT 1 FROM ptable WHERE symptoms = '"+str+"' AND weight = '"+percent+"' AND solutions = '"+str3+"') INSERT INTO ptable (symptoms,weight,solutions) VALUES ('"+str+"','"+percent+"','"+str3+"');

but it couldn't work. Is it right?


you miss the quote in percent of the VALUES

-----------------
[KH]
Go to Top of Page

Syan
Starting Member

3 Posts

Posted - 2005-11-30 : 22:20:01
Thanks for replying.

I've declared weight as an Integer. And i had also tried '"+percent+"', but it's not working. I am using DB2, is that the problem? Or is there any other way that i can do it?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2005-11-30 : 22:34:40
this is a Microsoft SQL Server forum. For DB2 forums you can try [url]http://www.dbforums.com[/url]

quote:
I've declared weight as an Integer

If weight is an integer column why are you inserting string into it ?

-----------------
[KH]
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-12-01 : 04:46:46
>>I am using DB2

This is SQL Server Forum. You should post at Relevant Forum as suggested

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -