CREATE PROC CHECK_MATERIAL_BY_LEX
	@ORGID int,
	@materialNumber varchar(2000)
AS
BEGIN

	;WITH #LEX_MaterialNumber AS(
		SELECT b.billno
		FROM (SELECT CAST('<v>'+REPLACE(@materialNumber,',','</v><v>')+'</v>' AS xml) billno) a	
		OUTER APPLY (SELECT T.C.value('.','varchar(50)') billno FROM a.billno.nodes('/v') AS T(C)) b
	)
	SELECT * 
	FROM (
		SELECT t0.FMATERIALID,t0.FNUMBER,t0_l.FNAME,t0_l.FSPECIFICATION,t1.FNUMBER Unit
			,CONCAT('FF',ISNULL(t0ap.FAUXPROPERTYID,0)) AS 'FAUXPROPERTYID'
			,CONVERT(int,t0ap.FISENABLE) FISENABLE
		FROM T_BD_MATERIAL t0
			INNER JOIN T_BD_MATERIAL_L t0_l on t0_l.FMATERIALID = t0.FMATERIALID 
				AND t0_l.FLOCALEID = 2052
			INNER JOIN T_BD_MATERIALSALE t0s on t0s.FMATERIALID = t0.FMATERIALID 
			INNER JOIN T_BD_UNIT t1 on t1.FUNITID = t0s.FSALEUNITID
			INNER JOIN #LEX_MaterialNumber tx on CHARINDEX(tx.billno,t0_l.FNAME) > 0
			LEFT JOIN t_BD_MaterialAuxPty t0ap on t0.FMATERIALID = t0ap.FMATERIALID
		WHERE 1=1
			AND t0.FUSEORGID = @ORGID
	) t0
	PIVOT
	(
		SUM(t0.FISENABLE) FOR
		t0.FAUXPROPERTYID IN (FF100001,FF100002)
	) t1

END