DROP FUNCTION LD0_EXIST_HAND
;
CREATE FUNCTION LD0_EXIST_HAND (@PI_HAND AS VARCHAR(10)) 
    RETURNS DECIMAL
AS
BEGIN
  DECLARE @N_EXIST DECIMAL(1)
  DECLARE @N_ANTAL DECIMAL(9)
  SET @N_EXIST = 0
  SET @N_ANTAL = 0
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0ATT   WHERE LAST_OPENED_HANDL    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0B   WHERE HAND    = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0B   WHERE ANSVHAND = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0B02   WHERE ANSVHAND = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0B11 WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0B11 WHERE MEDHAND = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0B21 WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0B21 WHERE MEDHAND = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0BVK WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0C   WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0C   WHERE MEDHAND = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0C02   WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0C02   WHERE MEDHAND = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0C20 WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0C20 WHERE MEDHAND = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0C22 WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0C22 WHERE MEDHAND = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0C32 WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0C41 WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0C70 WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0C70 WHERE MEDHAND = @PI_HAND)
    IF (@N_ANTAL > 0)
      SET @N_ANTAL = 1
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0E   WHERE BESFATT = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0E   WHERE HAND    = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0E   WHERE VHAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0E   WHERE MEDHAND = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0E07 WHERE HANDL   = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0E07 WHERE MEDHANDL = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0E40 WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0E40 WHERE MEDHAND = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0E55 WHERE HAND = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0F   WHERE BESFATT = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0F53  WHERE HAND    = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0FLI  WHERE HANDL    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0G   WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0H   WHERE HAND    = @PI_HAND AND RENSAD = 0)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0H   WHERE HAND2   = @PI_HAND AND RENSAD = 0)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0H02 WHERE HAND    = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0H02 WHERE MEDHAND  = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0H10 WHERE HAND1    = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0H10 WHERE HAND2    = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0H20 WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0H30 WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0H30 WHERE MEDHAND = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0H40 WHERE HAND    = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0I30 WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0I41 WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0V0L   WHERE HAND    = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0L05 WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0L93 WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0L95 WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0M21 WHERE HAND    = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0MOI WHERE HANDL    = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0N90 WHERE UTFHAND = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0N90 WHERE BESTHAND = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0N90 WHERE UTFMEDHAND = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0N91 WHERE UTFHAND = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0N91 WHERE BESTHAND = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0N900 WHERE UTFHAND = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0N900 WHERE UTFMEDHAND = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0P WHERE HAND = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0P WHERE MEDHAND = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0P20 WHERE HAND = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0R WHERE HAND = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0R50 WHERE HANDLKOD = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0S05 WHERE HAND = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0T41 WHERE PUBLISH_HANDL = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0T44 WHERE HANDFROM = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0T44 WHERE HANDTO = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0TSKEX WHERE HAND = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0U05 WHERE HAND   = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0U11 WHERE HANDLAGGARE   = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0U12 WHERE HANDLAGGARE   = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0U140 WHERE HAND   = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0U142 WHERE HAND   = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0U13 WHERE HAND1   = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0U13 WHERE HAND2   = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0U34 WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0U40 WHERE HAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0U72 WHERE HAND    = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0U72 WHERE HAND2    = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0U76 WHERE HAND    = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0U90 WHERE BESTHAND    = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0U90 WHERE MOTHAND    = @PI_HAND)
    SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0V09 WHERE HAND    = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0V49 WHERE HAND    = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0V56 WHERE HAND1   = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0V56 WHERE HAND2   = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0Z WHERE KPHANDL    = @PI_HAND)
	SET @N_ANTAL = (SELECT COUNT(*)+@N_ANTAL FROM LD0P0Z WHERE SSBT_HANDL    = @PI_HAND)
    IF (@N_ANTAL > 0)
      SET @N_EXIST = 1
  RETURN(@N_EXIST)
END
;
