USE ybdevicev2
DECLARE @tenantid BIGINT =299741280206917




DELETE FROM dbo.YB_FamilyData WHERE id IN (SELECT a.Id FROM dbo.YB_FamilyData a WHERE EXISTS(SELECT 1 FROM ybdevicev2_test.dbo.YB_FamilyData WHERE TenantId=@tenantid AND a.id=id) AND a.TenantId=@tenantid
) AND YB_FamilyData.TenantId=@tenantid


DELETE FROM dbo.YB_FamilyRealData WHERE id IN (SELECT id FROM dbo.YB_FamilyRealData a WHERE EXISTS(SELECT 1 FROM ybdevicev2_test.dbo.YB_FamilyRealData WHERE TenantId=@tenantid AND a.Id=id) AND a.TenantId=@tenantid) AND TenantId=@tenantid

DELETE FROM dbo.YB_Device WHERE id IN(
SELECT id FROM dbo.YB_Device a WHERE EXISTS(SELECT 1 FROM ybdevicev2_test.dbo.YB_Device WHERE TenantId=@tenantid AND a.id=id) AND a.TenantId=@tenantid) AND TenantId=@tenantid

DELETE FROM dbo.YB_Business WHERE id IN(
SELECT id FROM dbo.YB_Business a WHERE EXISTS(SELECT 1 FROM ybdevicev2_test.dbo.YB_Business WHERE TenantId=@tenantid AND a.id=id) AND a.TenantId=@tenantid) AND YB_Business.TenantId=@tenantid

DELETE FROM dbo.YB_RegUser WHERE id IN(
SELECT id FROM dbo.YB_RegUser a WHERE EXISTS(SELECT 1 FROM ybdevicev2_test.dbo.YB_RegUser WHERE TenantId=@tenantid AND a.id=id) AND a.TenantId=@tenantid) AND YB_RegUser.TenantId=@tenantid
DELETE FROM dbo.sys_user WHERE id IN(
SELECT id FROM dbo.sys_user a WHERE EXISTS(SELECT 1 FROM ybdevicev2_test.dbo.sys_user WHERE TenantId=@tenantid AND a.id=id) AND a.TenantId=@tenantid
) AND sys_user.TenantId=@tenantid

DELETE  FROM dbo.sys_emp WHERE id IN(
SELECT id FROM dbo.sys_emp a WHERE  id IN (SELECT id FROM ybdevicev2_test.dbo.sys_user WHERE TenantId=@tenantid)
)
DELETE FROM dbo.YB_BusinessRealData WHERE id IN(
SELECT id FROM dbo.YB_BusinessRealData a WHERE EXISTS(SELECT 1 FROM ybdevicev2_test.dbo.YB_BusinessRealData WHERE a.TenantId=@tenantid AND a.Id=id) AND a.TenantId=@tenantid
) AND YB_BusinessRealData.TenantId=@tenantid

DELETE FROM dbo.YB_DeviceAlloc WHERE id IN(
SELECT id FROM dbo.YB_DeviceAlloc a WHERE EXISTS(SELECT 1 FROM ybdevicev2_test.dbo.YB_DeviceAlloc WHERE a.TenantId=@tenantid AND a.EquId=EquId) AND a.TenantId=@tenantid
) AND YB_DeviceAlloc.TenantId=@tenantid

DELETE FROM dbo.YB_Family WHERE id IN(
SELECT id FROM dbo.YB_Family a WHERE EXISTS(SELECT 1 FROM ybdevicev2_test.dbo.YB_Family WHERE a.TenantId=@tenantid AND a.id=id) AND a.TenantId=@tenantid) AND YB_Family.TenantId=@tenantid

DELETE FROM dbo.YB_Result WHERE id IN(
SELECT id FROM dbo.YB_Result a WHERE EXISTS(SELECT 1 FROM ybdevicev2_test.dbo.YB_Result WHERE a.id=id AND TenantId=@tenantid) AND a.TenantId=@tenantid
) AND YB_Result.TenantId=@tenantid

DELETE FROM dbo.YB_MeasureResult WHERE id IN(
SELECT id FROM dbo.YB_MeasureResult a WHERE EXISTS(SELECT 1 FROM ybdevicev2_test.dbo.YB_MeasureResult WHERE id=a.id AND TenantId=@tenantid) AND a.TenantId=@tenantid
) AND YB_MeasureResult.TenantId=@tenantid

DELETE FROM dbo.YB_UserResult WHERE id IN(
SELECT id FROM dbo.YB_UserResult a WHERE EXISTS(SELECT 1 FROM ybdevicev2_test.dbo.YB_UserResult WHERE id=a.id AND TenantId=@tenantid) AND a.TenantId=@tenantid
) AND YB_UserResult.TenantId=@tenantid

DELETE FROM dbo.YB_BusinessResult WHERE id IN(
SELECT id FROM dbo.YB_BusinessResult a WHERE EXISTS(SELECT 1 FROM ybdevicev2_test.dbo.YB_BusinessResult WHERE id=a.id AND TenantId=@tenantid) AND a.TenantId=@tenantid
) AND YB_BusinessResult.TenantId=@tenantid

DELETE FROM dbo.YB_OutProductDev WHERE id IN(
SELECT id FROM dbo.YB_OutProductDev a WHERE EXISTS(SELECT 1 FROM dbo.YB_OutProduct WHERE TenantId=@tenantid AND CreatedUserId=0 AND a.OrderId=id)
)
DELETE FROM dbo.YB_OutProduct WHERE id IN(
SELECT id FROM dbo.YB_OutProduct WHERE TenantId=@tenantid AND CreatedUserId=0
) AND YB_OutProduct.TenantId=@tenantid



DELETE FROM dbo.YB_FamilyReportData WHERE id IN(
	SELECT id FROM dbo.YB_FamilyReportData a WHERE TenantId=@tenantid AND EXISTS(SELECT 1 FROM ybdevicev2_test.dbo.YB_FamilyReportData WHERE a.id=id AND TenantId=@tenantid)
) AND YB_FamilyReportData.TenantId=@tenantid
