Quantcast
Channel: ฟอรัม Getting started with SQL Server
Viewing all articles
Browse latest Browse all 8428

Delete values for a range

$
0
0

Hi ,

     I want to delete records only for a given range. Below is the script it deletes the records based on the given MP range.

And in this Switch table screen shot i have a switchID and ControlPointID column as main reference . For the given range only those records will be deleted. Now i want to delete a different table called SSDB_WIUDATA only for the controlpointIDs deleted on this switch table. The scenario is ControlpointID will be same even out of the given range also.but switchID will be different So it was easy to delete the records in switch. But while deleting in WIU data i have  WIUDataID and ControlPointID as main column reference . How do i delete the values from WIUData only for the values deleted in switch table? Here ControlPoint ID will be same even out of the range also. So i should delete only within the range that is deleted in Switch table. I dont have Milepost refernce also in WIU table.i have only ControlPointID as reference. Pls help me 

IF (CURSOR_STATUS('global','Switch_cur')) >= -1 
BEGIN 
DEALLOCATE Switch_cur 
END


DECLARE Switch_cur CURSOR FOR
Select SwitchID,ControlPointID FROM SSDB_Switch
WHERE Milepost BETWEEN @BeginMP AND @EndMP 
AND SubdivisionID = @subdivisionID

 
OPEN Switch_cur

FETCH NEXT FROM Switch_cur INTO @SwitchID,@ControlPointID


WHILE (@@FETCH_STATUS=0)
BEGIN

   set @Swi = (select Talon_LocEqup_ID from Mapping
   where SSDB_ControlpointId_Asset_Id = @SwitchID and AssetType='PTC-Switch')
  

   
Delete from tbl_equipmentObjectPropertyValues 
where equipmentObjectPropertyValue_object_guid_fk = ''+@Swi+''
 
Delete from tbl_objects
where object_guid_pk = ''+@Swi+''
 
DELETE from Mapping
where SSDB_ControlpointId_Asset_Id = @SwitchID
 
 DELETE FROM SSDB_Clearancepoint
 WHERE SwitchID = @SwitchID
 
DELETE FROM  SSDB_TurnoutSpeed
WHERE SwitchID = @SwitchID
 
 DELETE FROM SSDB_SwitchRelationship
 WHERE SwitchID = @SwitchID
 
 DELETE FROM SSDB_Switch
 WHERE SwitchID = @SwitchID
 
  FETCH FROM Switch_cur INTO @SwitchID,@ControlPointID
 
     END----Cur End

CLOSE Switch_cur
    DEALLOCATE Switch_cur        


Deepa



Viewing all articles
Browse latest Browse all 8428

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>