日期:2014-05-18 浏览次数:20477 次
-- 0. 传入参数 @tablename NVARCHAR(32) = '', --表名 @fieldname NVARCHAR(32) = '', --字段名 @sqlstring NVARCHAR(1024) = '', --查询条件,不含WHERE @results NVARCHAR(256) = '' OUTPUT --输出结果 AS DECLARE @sql NVARCHAR(2048); BEGIN BEGIN TRAN SET @sql =N'SELECT @results = ' + @fieldname + ' FROM ' + @tablename; IF (LTRIM(RTRIM(@sqlstring))<>'' AND @sqlstring IS NOT NULL) SET @sql = @sql + ' WHERE ' + @sqlstring; ELSE SET @sqlstring = ''; EXECUTE sp_executesql @sql,N'@results NVARCHAR(256) OUT',@results OUTPUT IF @results IS NULL OR LTRIM(RTRIM(@results)) = '' SELECT @results = ''; ELSE SELECT @results = LTRIM(RTRIM(@results)); IF @@ERROR <> 0 BEGIN ROLLBACK TRAN RETURN ''; END ELSE BEGIN COMMIT TRAN RETURN @results; END END