I have two tables personaltbl and coursetbl .
In both table p_id is common field column
I want to add record inside coursetbl if p_id value not present inside coursetbl but present inside the personaltbl.
But I am facing problem to check p_id value inside the personaltbl but same p_id value not present inside coursetbl. if this p_id value is not present inside the coursetbl then I want to insert record inside coursetbl.
For example:
personaltbl coursetbl
p_id int pk c_id int
p_name char c_name char
p_add char c_fees bigint
p_id Fk
some records
personaltbl coursetbl
p_id p_name p_add | c_id c_name c_add p_id
1 xyz abc | 1 qqqq yyyyy 1
3 pqr def | 2 eeee mmm 7
7 ert hgh |
Here p_id value 3 is present inside personaltbl but it absent in coursetbl . I want to insert record inside coursetbl having p_id value 3 by checking p_id value inside coursetbl.
shortly I want to insert record inside coursetbl by checking p_id value if present inside personaltbl but absent in coursetbl.
please suggest sql query or procedure or trigger or cursor. thank you.
Loading

Santhosh Kumar JayaramanPosted Jul 24, 2012, 10:14 AM
Sorry no need to remove foreign key reference, I misunderstood it.
.Something like
declare @pid as int
--set ur pid value.
then
If (Select count(1) from personaltbl where p_id=@pid)>0)
begin
If (Select count(1) from coursetble where p_id=@pid)=0)\
begin
--insert here
insert into coursetbl values (3, 'rrrr','rrrr',null)
end
end
the above code wil check if there are any records in personaltbl.Only if pid is present inside it, it will check whether the pid is present inside coursetbl.If its present, then it wont do anything, if it didnt present, then it will insert.
ThomasPosted Jul 25, 2012, 6:04 PM
If you know the P-ID that is missing those queries all work. However if you don't know what is missing and just want to fill in all the gaps.
First make sure that c_ID is an Identity field so that it auto generates the values when new records are inserted.
insert into coursetbl (c_name, c_add, p_id)
Select 'rrrr', 'ddd', p_id from personaltbl
Where p_id not in (select P_id from coursetbl)
This will insert result in the following records in your coursetbl
personaltbl coursetbl
p_id p_name p_add | c_id c_name c_add p_id
1 xyz abc | 1 qqqq yyyyy 1
3 pqr def | 2 eeee mmm 7
7 ert hgh | 3 rrrr dddd 3
This may not be the fastest code if you are dealing with millions of records but for most small to medium databases it should be fine.
Rahul BhattPosted Jul 25, 2012, 4:19 AM
For achieve this task No need to check two time, Simply check as following way,
First you need to check Data Exists in course table or not
For that you need to take inner join between two table and filter by p_id
If record not exists means need to insert
declare @pid as int = 3
IF NOT Exists(select c.p_id from personaltbl as p inner join coursetbl as c on p.p_id = c.p_id where c.p_id = @pid)
BEGIN
Insert into coursetbl(c_id,c_name,c_fees,p_id) values (3, 'rrrr',555,@pid)
END
kishor chourePosted Jul 24, 2012, 10:37 AM
kishor chourePosted Jul 24, 2012, 10:08 AM
Santhosh Kumar JayaramanPosted Jul 24, 2012, 9:36 AM
insert into coursetbl values (3, 'rrrr','rrrr',null)
kishor chourePosted Jul 24, 2012, 9:34 AM
Santhosh Kumar JayaramanPosted Jul 24, 2012, 9:16 AM