我有以下 sql 存储过程,但 c# 代码通过以下消息捕获异常:
Conversion failed when converting the nvarchar value to data type int
SQL 存储过程
@byTestType int
AS
DECLARE @qry nvarchar(3000)
IF @byTestType = 4
BEGIN
SET @qry = 'SELECT * FROM aTbl ORDER BY aCol'
END
ELSE
BEGIN
SET @qry = 'SELECT * FROM aTbl WHERE aCol= '
SET @qry = @qry +''''+CAST(@byTestType as varchar(3))+''''
END
EXEC sp_executesql @qry
aCol 数据类型是 varchar(3)
不明白是什么问题!