这是我做的一个例子
一定要用动态SQL来写,直接写会出错。这个郁闷了我一个下午终于搞定了CREATE PROCEDURE [dbo].USP_T_GOODpjGettop( @WhereClause nvarchar (2000) ,
@OrderBy nvarchar (2000) ,
@TopSize int
)ASBEGINdeclare @SQL as nvarchar(4000)set @SQL = 'SELECT '
if @TopSize > 0 BEGINset @SQL = @SQL + ' top ' + convert(nvarchar, @TopSize)END
set @SQL = @SQL +' [ID], [Goodid], [pjitem1], [pjitem2], [pjitem3], [pjitem4], [pjitem5], [pjavg]FROM [dbo].[T_GOODpj] 'IF LEN(@WhereClause) > 0BEGIN SET @SQL = @SQL + ' WHERE ' + @WhereClauseENDIF LEN(@OrderBy) > 0BEGIN SET @SQL = @SQL + ' ORDER BY ' + @OrderByEND
exec sp_executesql @SQL
ENDGO