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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Insert based on condition

Author  Topic 

rbharatha
Starting Member

29 Posts

Posted - 2005-11-24 : 04:34:19
Hi All..
I got 3 tables..tbl1, tbl2, tbl3
I want to insert into tbl4 with the primary key ID's of (tbl1, tbl2, tbl3) and other values
based on the where condition that tbl3.name is equal to some parameter @name...
If @name is not there in tbl3 then it should insert the record first into tbl3 and pass the value of scope_identity to the tbl4 insert sql.
for ex:-
If exists (select name tbl3 where name = @name)
insert into tbl4 (id1, id2, id3, datetimestamp)
values (@tbl1.id, @tbl2.id, tbl3.id, getdate())
else
step1 insert into tbl3 (name)
values (@name)
step2 insert into tbl4 (id1, id2, id3, datetimestamp)
values (@tbl1.id, @tbl2.id, scope_identity(), getdate())

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2005-11-24 : 04:39:12
can you post your table structure ?

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

rbharatha
Starting Member

29 Posts

Posted - 2005-11-24 : 04:45:30
Hi Khtan... as requested here is the table structure of the tables...
tbl1
columns datatype
contactid int identity (1,1) not null,
name varchar(50) null,
createdby varchar(50) null,
createddate datetime default getdate()

tbl1
columns datatype
suggestedid int identity (1,1) not null,
name varchar(50) null,
createdby varchar(50) null,
createddate datetime default getdate()

tbl1
columns datatype
namematchid int identity (1,1) not null,
name varchar(50) null,
createdby varchar(50) null,
createddate datetime default getdate()

tbl4
columns datatype
id int identity (1,1) not null,
contactid int
suggestedid int
namematchid int
code varchar(50) null,
office varchar(50) null,
createdby varchar(50) null,
createddate datetime default getdate()
Go to Top of Page
   

- Advertisement -