Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
A.O.A

hi,
im having a problem in sql, can anyone help me
i have 3 tables
sublocation, postingdetail and employee

i have to delete sublocid from sublocation table
but first i have to check that is there any employee working on that sublocation from postingdetail table, if yes then the record shouldn't be deleted.

plz reply


SQL
table sublocation
------------
 sublocid
 sublocname
 mainlocid

table postingdetail
============
 empid
 subloc
 mainloc

table employee
==========
 empid
 empname
 sublocid
 mainlocid
Posted
Updated 6-Jan-12 19:32pm
v3
Comments
Tejas Vaishnav 7-Jan-12 1:39am    
Hello friend please provide the defination of PK and FK so we will give the out put for your question....
Tejas Vaishnav 7-Jan-12 1:40am    
and also what type of data will be in postingdetail.subloc is it id or name...

table : Sublocation
====================
   columnName        DataType        Length    Allow Nulls
PK	SubLocID	    numeric               9       	0
	SubLocName	varchar	             50    	1
FK	MainLocID	   numeric                9     	1
	ShrtDesc	    varchar	            50    	1


table : Posting Detail
====================
   columnName        DataType        Length    Allow Nulls
PK	TID	          numeric	        9			0
FK	LocID	        numeric	        9			1
FK	SubLocId		numeric				9			1
	DesigId			numeric	        9			1
	RegPosting		numeric	        9			1
	Posted	        numeric			9			1


table : Employee
====================
   columnName        DataType     Length    Allow Nulls
PK	TID                numeric        9             0
FK	EMPID             varchar     	 50	        1
	EmpName		  varchar        250            1
	EmpCodePrefix     varchar         50            1
	EmpNo             varchar         50            0
FK	DeptID            numeric            9           1
FK	MainLocID         numeric           9            1
FK	SubLocID	numeric	         9	1
 
Share this answer
 
v3
Comments
Tejas Vaishnav 7-Jan-12 1:53am    
please do not post a solution for your question improvement, insted use a improve question link to add or modify your question
If the postingdetail.subloc is the sublocid then use this query to delete sublocation from sublocation where any employee will not working

SQL
DELETE FROM sublocation WHERE sublocid NOT IN (
	SELECT sublocid FROM employee
	WHERE sublocid NOT IN ( SELECT subloc FROM postingdetail) 
)
 
Share this answer
 
Comments
Tejas Vaishnav 7-Jan-12 1:56am    
if my answer solve your problem then accept it as ans and also rate it...
You can delete your sublocations by using a stored procedure like this :

SQL
create procedure sp_DeleteSubLoc
@sublocid int
as
begin
  if ((select count(*) from [posting detail] where sublocid = @sublocid) = 0)
  begin
     delete sublocation where sublocid = @sublocid
     return 0
  end
  else
  begin
     return 1
  end
end


you can use it by this way :
SQL
DECLARE	@return_value int

EXEC	@return_value = sp_DeleteSubLoc 15

SELECT	'Return Value' = @return_value


15 is an example for @sublocid, if it succeeds returns 0 otherwise returns 1.

Hope it helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900