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
 Development Tools
 ASP.NET
 Help me, how i can write a command - if?

Author  Topic 

K100
Starting Member

4 Posts

Posted - 2007-06-28 : 02:44:07
How i can write a command and what is the syntax for:
I want to do: Check if the value exists, if not, create the value.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-06-28 : 04:55:04
You need an UPSERT routine

UPDATE ....
IF @@ROWCOUNT = 0 THEN INSERT ...


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Big J
Starting Member

2 Posts

Posted - 2007-06-28 : 05:10:59
Hard to say what your looking for. (Need more information)
If it is a sql command you need you can use:

if not exists(select 1 from...)
begin
...Some code
end
else
begin
...some other code
end

or you can also use if exists
if you need code for ASP it's been different depending on what language you using but if...then...else still works (syntax can vary depending on the language)

Jonas
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-28 : 06:38:53
2008 will make it easy with MERGE syntax

Madhivanan

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

K100
Starting Member

4 Posts

Posted - 2007-07-05 : 05:06:12
I`m beginner in sql language!
I receive a txt file with 10 values for update database, and i must to write command for every value that have to check if one value of that file exist in this database i will do nothing, but if this value doesn`t exist i have to create it.

Do you understand me?

Example:
inbound txt file pipe separated:
SERNUM Descr. Name Surname .....
1234876|KS1324345|George|Patty|.....

Now i have to check:
If the value George exist in table Users, if exist do nothing, if doesn`t exist i have to create it. And another one:

quote:
Originally posted by Big J

Hard to say what your looking for. (Need more information)
If it is a sql command you need you can use:

if not exists(select 1 from...)
begin
...Some code
end
else
begin
...some other code
end

or you can also use if exists
if you need code for ASP it's been different depending on what language you using but if...then...else still works (syntax can vary depending on the language)

Jonas

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-07-05 : 10:13:36
You need to import the file in a staging table. Then do

Insert into yourTable(emp_name)
Select emp_name from StagingTable T
where not exists
(select * from yourTable where emp_name=T.emp_name)

Madhivanan

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

K100
Starting Member

4 Posts

Posted - 2007-07-06 : 05:44:07
Thank you very much!
quote:
Originally posted by madhivanan

You need to import the file in a staging table. Then do

Insert into yourTable(emp_name)
Select emp_name from StagingTable T
where not exists
(select * from yourTable where emp_name=T.emp_name)

Madhivanan

Failing to plan is Planning to fail

Go to Top of Page
   

- Advertisement -