The function is a scalar function that returns nvarchar(1000)
, and I'm calling it from LINQ to SQL:
var allUserBranchIDs = appContext.DataContext.GetUserBranchIDs(appContext.PersonID, branchDelimiter);
And it gives this error:
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.GetUserBranchIDs", or the name is ambiguous
It runs just fine when I call it from SQL Server Management Studio:
select [dbo].[GetUserBranchIDs](3678, ',')
Which suggests a permissions issue with the SQL user that the DataContext is using - but I'm using the exact same sa
user in LINQ to SQL that I am in SSMS!
edit: here is the LINQ to SQL generated method:
[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.GetUserBranchIDs", IsComposable=true)]
public string GetUserBranchIDs([global::System.Data.Linq.Mapping.ParameterAttribute(Name="UserID", DbType="Int")] System.Nullable<int> userID, [global::System.Data.Linq.Mapping.ParameterAttribute(Name="Delimiter", DbType="NVarChar(10)")] string delimiter)
{
return ((string)(this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), userID, delimiter).ReturnValue));
}
edit: function definition:
CREATE FUNCTION [dbo].[GetUserBranchIDs]
(
@UserID int,
@Delimiter nvarchar(10) = '/'
)
RETURNS nvarchar(1000)
AS
BEGIN
declare @result nvarchar(1000) = null;
select @result = coalesce(@result, '') + cast(a.BranchID as nvarchar(10)) + @Delimiter
from (
select distinct
b.BranchID
from vw_UserBranchCurrent a
inner join vw_BranchDataCurrent b
on a.BranchID = b.BranchID
where
((@UserID is not null) and (a.UserID = @UserID))
) a;
set @result = left(@result, len(@result) - len(@Delimiter));
return @result;
END