方法一:
DECLARE @SQL NVARCHAR(256) DECLARE @ReturnID INT SET @SQL = 'SELECT [InfoID] FROM TM_TeachInfo WHERE [CourseID]=1 and [infosort]=1' EXEC(@SQL) SELECT @ReturnID = @@Rowcount SELECT @ReturnID
方法二:
ALTER PROCEDURE UP_TM_TeachInfo_Exists_ByStr @strWhere varchar(1000) AS DECLARE @ReturnID int DECLARE @Sql nvarchar(400) set @sql = 'SELECT @ReturnID = count(1) FROM TM_TeachInfo WHERE 1=1 and '+@strWhere exec sp_executesql @sql,N'@ReturnID int out',@ReturnID out IF @ReturnID = 0 RETURN 0 ELSE set @sql = 'SELECT @ReturnID = [InfoID] FROM TM_TeachInfo WHERE 1=1 and '+@strWhere exec sp_executesql @sql,N'@ReturnID int out',@ReturnID out RETURN @ReturnID