存储过程中的数据库优先级异常

时间:2014-05-20 06:33:53

标签: c# sql asp.net-mvc linq entity-framework

我的存储过程是::

USE [DatabaseName]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AdvancedSearch] 
    @CompanyID bigint,
    @BookingReference nvarchar(15)

AS
Begin

declare @ExecuteSQL nvarchar(2000)
declare @WhereSQL nvarchar(2000)

if rtrim(isnull(@BookingReference,''))<>''
begin
set @WhereSQL = concat(@WhereSQL,' and
exists(select top 1 null from document (nolock) where document.dossierID=DossierHeader.DossierID and bookingreference like ''%' , @BookingReference , '%'' and Document.CompanyID=',@CompanyID,')')
end

set @ExecuteSQL=concat('
select top 10 dossier.DisplayID,dossierid,CustomerID,isnull(Customer.ShortName,Customer.[CustomerName]) as Customer,DossierHeader.InvoiceNumber,
from DossierHeader
join Customer
on DossierHeader.CustomerID=Customer.ID
where 
DossierHeader.CompanyID=',@CompanyID,@WhereSQL)

EXEC SP_EXECUTESQL @ExecuteSQL

end

当我在我的代码中使用它然后在Debug上我得到一些异常,如:: SP Exception

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

如果希望返回嵌套选择的结果(存储过程return执行返回代码或输出参数),则应将stored procedure更改为table value function

区别:

Function vs. Stored Procedure in SQL Server

实体框架支持表值函数:

http://blogs.msdn.com/b/efdesign/archive/2011/01/21/table-valued-function-support.aspx