IF exists(SELECT * FROM sysobjects WHERE name = 'LD0ProcFormSearch')
	DROP PROCEDURE LD0ProcFormSearch
GO
CREATE PROCEDURE LD0ProcFormSearch (@FORMNR DECIMAL, @FRAGENR DECIMAL, @SVAR VARCHAR(100), @SVARLNR DECIMAL, @FORMLOPNR DECIMAL, @PNR VARCHAR(12), @OBJEKTNYKLLNR DECIMAL, 
@DYNDELSYS DECIMAL, @VERSION DECIMAL, @OBJEKTNYCKLNUM2 DECIMAL, @URVAL VARCHAR(60))
AS
BEGIN

BEGIN TRY
	DECLARE @OPERATOR DECIMAL(9)=(SELECT DISTINCT OPERATOR FROM LD0P0V25 WHERE FRAGENR=@FRAGENR AND SVARLNR=@SVARLNR AND URVAL = @URVAL)
END TRY
BEGIN CATCH
	PRINT 'Flera operatorer'
END CATCH

	IF @OPERATOR=1
	BEGIN
	SELECT V09.* FROM LD0V0V09 V09 
				JOIN LD0V0V10 V10 ON V10.OBJEKTNYKLNUM = V09.OBJEKTNYCKLNUM AND V10.OBJEKTNYKLLNR = V09.OBJEKTNYCKLLNR AND V09.OBJEKTNYCKLALFA = @PNR AND V10.OBJEKTNYKLNUM2 = @OBJEKTNYCKLNUM2 AND V10.DYNDELSYS = @DYNDELSYS AND V10.OBJEKTNYKLLNR = @OBJEKTNYKLLNR AND V10.VERSION = @VERSION
				JOIN LD0V0V11 V11 ON V11.SVARNR = V10.SVARNR
				WHERE 
				V09.FORMNR = @FORMNR
				AND V09.LOPNR = @FORMLOPNR 
				AND V10.FRAGENR = @FRAGENR 
				AND V11.SVAR = @SVAR
				AND V11.SVARLNR = @SVARLNR
				AND V10.OBJEKTNYKLALFA = @PNR
	END

	IF @OPERATOR=2
	BEGIN
	SELECT V09.* FROM LD0V0V09 V09 
				JOIN LD0V0V10 V10 ON V10.OBJEKTNYKLNUM = V09.OBJEKTNYCKLNUM AND V10.OBJEKTNYKLLNR = V09.OBJEKTNYCKLLNR AND V09.OBJEKTNYCKLALFA = @PNR AND V10.OBJEKTNYKLNUM2 = @OBJEKTNYCKLNUM2 AND V10.DYNDELSYS = @DYNDELSYS AND V10.OBJEKTNYKLLNR = @OBJEKTNYKLLNR AND V10.VERSION = @VERSION
				JOIN LD0V0V11 V11 ON V11.SVARNR = V10.SVARNR
				WHERE 
				V09.FORMNR = @FORMNR 
				AND V09.LOPNR = @FORMLOPNR
				AND V10.FRAGENR = @FRAGENR 
				AND V11.SVAR LIKE @SVAR
				AND V11.SVARLNR = @SVARLNR
				AND V10.OBJEKTNYKLALFA = @PNR
	END

	IF @OPERATOR=3
	BEGIN
	SELECT V09.* FROM LD0V0V09 V09 
				JOIN LD0V0V10 V10 ON V10.OBJEKTNYKLNUM = V09.OBJEKTNYCKLNUM AND V10.OBJEKTNYKLLNR = V09.OBJEKTNYCKLLNR AND V09.OBJEKTNYCKLALFA = @PNR AND V10.OBJEKTNYKLNUM2 = @OBJEKTNYCKLNUM2 AND V10.DYNDELSYS = @DYNDELSYS AND V10.OBJEKTNYKLLNR = @OBJEKTNYKLLNR AND V10.VERSION = @VERSION
				JOIN LD0V0V11 V11 ON V11.SVARNR = V10.SVARNR
				WHERE 
				V09.FORMNR = @FORMNR 
				AND V09.LOPNR = @FORMLOPNR
				AND V10.FRAGENR = @FRAGENR 
				AND V11.SVAR < @SVAR
				AND V11.SVARLNR = @SVARLNR
				AND V10.OBJEKTNYKLALFA = @PNR
	END

	IF @OPERATOR=4
	BEGIN
	SELECT V09.* FROM LD0V0V09 V09 
				JOIN LD0V0V10 V10 ON 
				V10.OBJEKTNYKLNUM = V09.OBJEKTNYCKLNUM 
				AND V10.OBJEKTNYKLLNR = V09.OBJEKTNYCKLLNR 
				AND V09.OBJEKTNYCKLALFA = @PNR 
				AND V10.OBJEKTNYKLNUM2 = @OBJEKTNYCKLNUM2 
				AND V10.DYNDELSYS = @DYNDELSYS 
				AND V10.OBJEKTNYKLLNR = @OBJEKTNYKLLNR 
				AND V10.VERSION = @VERSION
				JOIN LD0V0V11 V11 ON V11.SVARNR = V10.SVARNR
				WHERE 
				V09.FORMNR = @FORMNR 
				AND V09.LOPNR = @FORMLOPNR
				AND V10.FRAGENR = @FRAGENR 
				AND V11.SVAR > @SVAR
				AND V11.SVARLNR = @SVARLNR
				AND V10.OBJEKTNYKLALFA = @PNR
	END

	IF @OPERATOR=5
	BEGIN
	SELECT V09.* FROM LD0V0V09 V09 
				JOIN LD0V0V10 V10 ON V10.OBJEKTNYKLNUM = V09.OBJEKTNYCKLNUM AND V10.OBJEKTNYKLLNR = V09.OBJEKTNYCKLLNR AND V09.OBJEKTNYCKLALFA = @PNR AND V10.OBJEKTNYKLNUM2 = @OBJEKTNYCKLNUM2 AND V10.DYNDELSYS = @DYNDELSYS AND V10.OBJEKTNYKLLNR = @OBJEKTNYKLLNR AND V10.VERSION = @VERSION
				JOIN LD0V0V11 V11 ON V11.SVARNR = V10.SVARNR
				WHERE 
				V09.FORMNR = @FORMNR 
				AND V09.LOPNR = @FORMLOPNR
				AND V10.FRAGENR = @FRAGENR 
				AND V11.SVAR <= @SVAR
				AND V11.SVARLNR = @SVARLNR
				AND V10.OBJEKTNYKLALFA = @PNR
	END

	IF @OPERATOR=6
	BEGIN
	SELECT V09.* FROM LD0V0V09 V09 
				JOIN LD0V0V10 V10 ON V10.OBJEKTNYKLNUM = V09.OBJEKTNYCKLNUM AND V10.OBJEKTNYKLLNR = V09.OBJEKTNYCKLLNR AND V09.OBJEKTNYCKLALFA = @PNR AND V10.OBJEKTNYKLNUM2 = @OBJEKTNYCKLNUM2 AND V10.DYNDELSYS = @DYNDELSYS AND V10.OBJEKTNYKLLNR = @OBJEKTNYKLLNR AND V10.VERSION = @VERSION
				JOIN LD0V0V11 V11 ON V11.SVARNR = V10.SVARNR
				WHERE 
				V09.FORMNR = @FORMNR 
				AND V09.LOPNR = @FORMLOPNR
				AND V10.FRAGENR = @FRAGENR 
				AND V11.SVAR >= @SVAR
				AND V11.SVARLNR = @SVARLNR
				AND V10.OBJEKTNYKLALFA = @PNR
	END

END
GO
GRANT ALL ON LD0ProcFormSearch TO LD0ROANV
GO