exec sp_executesql N' DECLARE @ID_IndentBill uniqueidentifier DECLARE @IndentBillNo nvarchar(64) SELECT TOP 1 @IndentBillNo = IndentBill.No_IndentBill FROM IndentBill Join Customer On Customer.ID_Customer=IndentBill.ID_Customer WHERE IndentBill.No_IndentBill > @No_IndentBill And Customer.ID_Creator=case IsNull(DataLength(@Right),0) When 0 Then Customer.ID_Creator Else @Right End --added for validating right and Special_IndentBill=case @Special when 1 then Special_IndentBill else 0 End --added for Special and Customer.ID_RegionalLeader in (Select ID_RegionalLeader From Principal Where ID_Employee in(select ID_Employee from Employee where Name_Employee=@Name_Employee) ) ORDER BY IndentBill.No_IndentBill SELECT IndentBill.*, Customer.No_Customer, Customer.Name_Customer, Customer.ShortName_Customer, Customer.Type_Customer, Customer.LastDiscount_Customer, Customer.PostalCode_Customer, a.Name_User AS Name_Creator, ISNULL(b.Name_User, '''') AS Name_Assessor, ISNULL(c.Name_User, '''') AS LastModUserName, Location.No_Location, Location.Name_Location, Employee.No_Employee AS No_Principal ,Employee.Name_Employee AS Principal_IndentBill FROM IndentBill LEFT JOIN Customer ON Customer.ID_Customer = IndentBill.ID_Customer LEFT JOIN AppUser a ON a.ID_User = IndentBill.ID_Creator LEFT JOIN AppUser b ON b.ID_User = IndentBill.ID_Assessor LEFT JOIN AppUser c ON c.ID_User = IndentBill.LastModUser_IndentBill LEFT JOIN Location ON Location.ID_Location = IndentBill.ID_Location LEFT JOIN Employee ON Employee.ID_Employee = IndentBill.ID_Principal WHERE IndentBill.No_IndentBill = @IndentBillNo SELECT @ID_IndentBill = IndentBill.ID_IndentBill FROM IndentBill WHERE IndentBill.No_IndentBill = @IndentBillNo SELECT IndentBillProduct.*, Product.No_Product, Product.Name_Product, Product.Unit_Product, Product.LowSalePrice_Product, BillType.Name_BillType, (IndentBillProduct.Quantity_IndentBillProduct * IndentBillProduct.UnitPrice_IndentBillProduct) AS Amount_IndentBillProduct,@IndentBillNo as No_IndentBill FROM IndentBillProduct LEFT JOIN Product ON Product.ID_Product = IndentBillProduct.ID_Product LEFT JOIN BillType ON BillType.Value_BillType = IndentBillProduct.Type_FromBill WHERE IndentBillProduct.ID_IndentBill = @ID_IndentBill ORDER BY CAST(IndentBillProduct.No_IndentBillProduct AS INT) SELECT BillComment.* FROM BillComment WHERE BillComment.ID_Bill = @ID_IndentBill order by BillComment.No_BillComment SELECT ReportComment.*,@IndentBillNo as No_IndentBill FROM ReportComment WHERE ReportComment.ID_Bill = @ID_IndentBill order by ReportComment.No_ReportComment',N'@No_IndentBill nvarchar(64),@Right uniqueidentifier,@Special bit,@Name_Employee nvarchar(64)',@No_IndentBill=N'I1904010001',@Right=NULL,@Special=1,@Name_Employee=N'1111'