You could try to convert the nvarchar to varchar during that join. The Table Relations can apparently be a field OR any expression. So rather than just putting the field, add a convert(varchar, @ParameterName). Hopefully that works.
Where is that option? I only see nvarchar in Parameters.
This is a separate Project table for our Sales Sharepoint site. It is not the Epicor Project table and it links to other tables not represented in Epicor.
Darn, I haven’t had much time in the EXTERNAL baq designer. You could try doing the convert in a calculated field (using that parameter) in a subquery, then filter on that converted field in your main query.
The Query Phrase from Epicor is generate a “Bad SQL statement”:
select
[Opportunity].[ProjectID] as [Opportunity_ProjectID],
[Opportunity].[OpportunityID] as [Opportunity_OpportunityID],
[Opportunity].[Name] as [Opportunity_Name],
[Employee].[FirstName] as [Employee_FirstName],
[Employee].[LastName] as [Employee_LastName],
[Order].[OrderNum] as [Order_OrderNum],
[WorkOrder].[WorkOrderID] as [WorkOrder_WorkOrderID],
(convert(nvarchar,Opportunity.ProjectID)) as [Calculated_Project]
from dbo.Opportunity as Opportunity
full outer join dbo.Order as Order on
Opportunity.ProjectID = Order.ProjectID
and Opportunity.OpportunityID = Order.OpportunityID
*and ( Order.OrderNum = @SalesOrder )*
full outer join dbo.WorkOrder as WorkOrder on
Order.OrderNum = WorkOrder.OrderNum
and ( not WorkOrder.WorkOrderID like 'x%' )
full outer join dbo.Employee as Employee on
Opportunity.SalesRepID = Employee.EmployeeID
where (Opportunity.OpportunityID = @Opportunity or Opportunity.ProjectID = cast(@Project AS varchar(25)))
order by Opportunity.ProjectID, Opportunity.OpportunityID, Order.OrderNum, WorkOrder.WorkOrderID
When I move that Order.OrderNum = @SalesOrder to the where clause in SSMS it works correctly:
Declare @SalesOrder int;
Declare @Opportunity int;
Declare @Project varchar(25);
Set @SalesOrder = 14259;
select
[Opportunity].[ProjectID] as [Opportunity_ProjectID],
[Opportunity].[OpportunityID] as [Opportunity_OpportunityID],
[Opportunity].[Name] as [Opportunity_Name],
[Employee].[FirstName] as [Employee_FirstName],
[Employee].[LastName] as [Employee_LastName],
[Order].[OrderNum] as [Order_OrderNum],
[WorkOrder].[WorkOrderID] as [WorkOrder_WorkOrderID],
(convert(nvarchar,Opportunity.ProjectID)) as [Calculated_Project]
from dbo.Opportunity as Opportunity
full outer join denyse.dbo.[Order] as [Order] on
Opportunity.ProjectID = [Order].ProjectID
and Opportunity.OpportunityID = [Order].OpportunityID
--and ( [Order].OrderNum = 14259 )
full outer join dbo.WorkOrder as WorkOrder on
[Order].OrderNum = WorkOrder.OrderNum
and ( not WorkOrder.WorkOrderID like 'x%' )
full outer join dbo.Employee as Employee on
Opportunity.SalesRepID = Employee.EmployeeID
where (Opportunity.OpportunityID = @Opportunity or Opportunity.ProjectID = cast(@Project AS varchar(25)) *or [Order].OrderNum = @SalesOrder *)
order by Opportunity.ProjectID, Opportunity.OpportunityID, [Order].OrderNum, WorkOrder.WorkOrderID
full outer join dbo.Order as Order on
Opportunity.ProjectID = Order.ProjectID
and Opportunity.OpportunityID = Order.OpportunityID
*and ( Order.OrderNum = @SalesOrder )*
Is that you just trying to highlight important code parts for us here?
full outer join dbo.Order as Order on
Opportunity.ProjectID = Order.ProjectID
and Opportunity.OpportunityID = Order.OpportunityID
*and ( Order.OrderNum = @SalesOrder )*
Should only have relationships between the tables. ( Order.OrderNum = @SalesOrder ) is a table criteria so it belongs down in the WHERE clause
I agree.
And I don’t want it there. But the Epicor code put it in there, when I used the Table Criteria on the dbo.Order:
I just want it in the Where Clause.
When I rearrange things in SSMS, it works perfectly. That’s what I was trying to say before.
I don’t understand why the BAQ puts the table criteria as a Join condition.
It kept calling “Bad SQL Statement”.
I had to go to the server log to get the code:
"Error:System.Data.SqlClient.SqlException (0x80131904): "
Need to declare @Project.
The single quotes did the trick…
Run it with a known good value for @ProjectID, and known bad values (values that should have no match) for @SalesOrder and @Oppurtunity. I bet it comes up with no records