我不知道为什么在存储过程中出现此错误。在此示例中,@SearchQuery
的值将为email:tony.stark@starkindustries.com
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[cust_a91150_sp_LiveSearch]
@PersonID INT,
@SearchMode VARCHAR(MAX),
@SearchQuery VARCHAR(MAX),
@OrganizationID INT
AS
IF @SearchMode = 'People'
BEGIN
SELECT DISTINCT TOP 7
p.[guid] [id],
CASE
WHEN p.[nick_name]+' '+p.[last_name] LIKE @SearchQuery+'%' THEN p.[nick_name]+' '+p.[last_name]
WHEN p.[first_name]+' '+p.[last_name] LIKE @SearchQuery+'%' THEN p.[first_name]+' '+p.[last_name]
END AS [name],
p.[last_name],
p.[nick_name],
'Person' [type]
FROM core_person p
LEFT JOIN core_person_email e on e.person_id = p.person_id
WHERE (p.[organization_id] = @OrganizationID)
AND (p.[first_name] <> '' AND p.[nick_name] <> '')
AND (p.[record_status] = 0)
AND (
(p.[first_name]+' '+p.[last_name] LIKE @SearchQuery+'%' OR p.[nick_name]+' '+p.[last_name] LIKE @SearchQuery+'%' AND @SearchQuery NOT LIKE 'email:%')
-- Problem here on this next line
OR (e.[email] = LTRIM(RIGHT(@SearchQuery, LEN(@SearchQuery) - 6)) AND @SearchQuery LIKE 'email:%')
)
ORDER BY p.[last_name] ASC,
p.[nick_name] ASC
END
/** End People search mode **/
....
我只是在IF / ELSE之外尝试了查询的主要部分,它工作正常,没有问题。但是从存储过程运行时,它立即崩溃。请帮助:(
更新: 这是调用该存储过程的代码。这是一个Web服务调用。对于此示例:
用户:1 模式:“人” 查询:“ email:tony.stark@starkindustries.com” orgId:1
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetResults(string mode, string query)
{
int user = ArenaContext.Current.Person.PersonID;
JavaScriptSerializer serializer = new JavaScriptSerializer();
-- SPROC call on below line
DataTable DataSource = new LiveSearchServiceModel().LiveSearch_DT(user, mode, query, Arena.Core.ArenaContext.Current.Organization.OrganizationID);
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in DataSource.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in DataSource.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
以及数据库类/方法:
public class LiveSearchServiceModel : SqlData
{
public LiveSearchServiceModel() { }
public DataTable LiveSearch_DT(int user, string searchMode, string searchQuery, int orgId)
{
ArrayList lst = new ArrayList();
lst.Add(new SqlParameter("@PersonID", user));
lst.Add(new SqlParameter("@SearchMode", searchMode));
lst.Add(new SqlParameter("@SearchQuery", searchQuery));
lst.Add(new SqlParameter("@OrganizationID", orgId));
try
{
return this.ExecuteDataTable("cust_a91150_sp_LiveSearch", lst);
}
catch (SqlException ex)
{
throw ex;
}
finally
{
lst = null;
}
}
}
答案 0 :(得分:1)
我猜是问题所在:
RIGHT(@SearchQuery, LEN(@SearchQuery) - 6)
当您传递短字符串时,您将获得:
RIGHT(@SearchQuery, -1)
-- Invalid length parameter passed to the right function.
我会检查LEN(@SearchQuery) - 6 >= 0
RIGHT(@SearchQuery, CASE WHEN LEN(@SearchQuery) - 6>=0 THEN LEN(@SearchQuery) - 6
ELSE LEN(@SearchQuery) END)