错误信息:System.Data.SqlClient.SqlException: 传递到 SUBSTRING 函数的长度参数无效
数据库执行下面的sql 没问题,但是用。net 获取datatable 或者用dataread去读取的时候,会报错误 具体的:LEFT(contxt,LEN(contxt)-1
如果把left去掉,不报错,取出来的 contxt是空,是我字符串太长了吗?
with student as
( SELECT TOP 100 PERCENT t1.UserID ,t2.UserName,t2.[group],t2.sort,t1.StyleImagePath+'^'+t1.StyleCode+'^'+CONVERT(VARCHAR(10),XYDdate,120) +'^'
+CONVERT(VARCHAR(10),DBWCdate,120)+'^'+MLDYdate+'^'+MLDLdate+'^'+FLDYdate+'^'+CONVERT(VARCHAR(10),FZWCdate,120)+'^'+Remark AS contxt
FROM [dy_result] t1 LEFT JOIN dy_user t2 ON t1.UserID=t2.UserID
ORDER BY t2.[Group],t2.sort asc )
SELECT UserID,UserName,CAST(LEFT(contxt,LEN(contxt)-1) AS Text) as contxt FROM(
SELECT [group],sort,UserID,UserName,
(SELECT contxt+'$' FROM student
WHERE UserID=A .UserID AND UserName=A.UserName
FOR XML PATH('')) AS contxt
FROM student A GROUP BY [group],sort,UserID,UserName) B ORDER BY [group],sort
contxt为null时, len(contxt)-1以及left函数这两个总有一个会出错了。
你要么排除contxt为null的情况,要么作点啥处理。
嗯,你说的的确是,但是为为什么我不用函数处理SELECT UserID,UserName, contxt ,这样直接字段,我有数据,但是datatable和datareader都读出来都都是空呢?比较奇怪