日期:2014-05-19  浏览次数:20390 次

这个SQL语句如何改?
UPDATE   AskClass
SET   AskCount   =   AskCount   +   1
WHERE   ClaId   IN   (                                                                                            
SELECT   ClaNavIdList   FROM   AskClass   WHERE   ClaId   =   7     //这里的ClaNavIdList是字符串他的值是5,6,7       执行时提示类型转换错误。。。
)


如果我这么写就对了:
UPDATE   AskClass
SET   AskCount   =   AskCount   +   1
WHERE   ClaId   IN   (                                                                                            
SELECT   5,6,7   FROM   AskClass   WHERE   ClaId   =   7
)

我应如何改??

------解决方案--------------------
如果要解决这个问题可以写个数据库的函数实现
CREATE FUNCTION f_splitSTR(
@s varchar(8000),
@split varchar(10)
)RETURNS @re TABLE(col int)
AS
BEGIN
DECLARE @t TABLE(ID int IDENTITY,b bit)
INSERT @t(b) SELECT TOP 8000 0 FROM syscolumns a,syscolumns b

INSERT @re SELECT cast(SUBSTRING(@s,ID,CHARINDEX(@split,@s+@split,ID)-ID) as int)
FROM @t
WHERE ID <=LEN(@s+ 'a ')
AND CHARINDEX(@split,@split+@s,ID)=ID
RETURN
END
GO


调用的时候
UPDATE AskClass
SET AskCount = AskCount + 1
WHERE ClaId IN (
SELECT f_splitSTR(ClaNavIdList, ", ") FROM AskClass WHERE ClaId = 7
)