March 19, 2010 at 4:00 am
I have two tables Table1 and Table2.
Table1:
BugId, EngineerId,EngineerName, BugStatus
Table2:
EngineerId, EngineerName, No of Resolved Bugs.
I just wanted to take out the EngineerId and EngineerName from Table1 and put it Table2.
Whenever I insert BugId, EngineerId,EngineerName,and BugStatus into Table1, I just wanted look at the table2 and if engineerid is not there I have to insert into Table2 or leave it.
Can you just give me some idea how to use if not exists or any other scenario?
Thanks.
March 19, 2010 at 4:16 am
chandrasekaran.ganapathy (3/19/2010)
Can you just give me some idea how to use if not exists or any other scenario?
Try thisdeclare @t table ( EngineerId int )
insert into @t
select 1
union select 3
union
select 5
select * from @T
declare @t2 table ( EngineerId int )
insert into @t2
select 5 union select 3
select * from @T2
insert into @t2
select * from @t
where EngineerId not in ( select EngineerId from @t2)
select * from @T2
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
March 19, 2010 at 4:39 am
This is ok if the table1 does not have the repetition of EngineerId. But Table1 has the repetition of EngineerId. I've tried your code. It says Primary Key violation.
Can you give me any other solution.
Please mail me "Chandrasekaran.ganapathy@patni.com"
March 19, 2010 at 4:43 am
chandrasekaran.ganapathy (3/19/2010)
This is ok if the table1 does not have the repetition of EngineerId. But Table1 has the repetition of EngineerId. I've tried your code. It says Primary Key violation.Can you give me any other solution.
Please mail me "Chandrasekaran.ganapathy@patni.com"
You can use the DISTINCT Clause to remove the duplicates..
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
March 19, 2010 at 4:54 am
Cool it is working... Thanks for the gr8 help.
March 19, 2010 at 5:14 am
Insert into Table2(EngineerId,EngineerName)
select Distinct(EngineerId),EngineerName from Table1
where EngineerId not in (select (EngineerId) from Table2)
Why it is not retrieving EngineerName?
March 19, 2010 at 5:20 am
chandrasekaran.ganapathy (3/19/2010)
Why it is not retrieving EngineerName?
it should . can you post some sample data where this query is failing ?
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
March 19, 2010 at 5:28 am
BugId EngineerId EngineerName State
10233 64867 Chandrasekaran Resolved
10235 64865 Srikanth Addagalla Assigned
........
March 19, 2010 at 5:36 am
there is no duplicate values and secondly is this from table A or table B ?
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
March 19, 2010 at 5:41 am
This is from Table1.
I have just given some sample values, Duplications are allowed in this table. Because one engineer can raise any no. of bugs. only thing is bug id is uhique.
March 19, 2010 at 5:52 am
chandrasekaran.ganapathy (3/19/2010)
select Distinct(EngineerId),EngineerName from Table1where EngineerId not in (select (EngineerId) from Table2)
this query will exclude all the records/engineerid which are present in table2.
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
March 19, 2010 at 5:59 am
Will it retrieve engineername or not?
March 19, 2010 at 6:11 am
NO, it will give only those engineerid and engineername which are not present in table2
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
March 19, 2010 at 6:11 am
Does the below query return any NULL values
select (EngineerId) from Table2
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
March 19, 2010 at 6:18 am
No it is not bringing null.
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply