ado*_*lot 4 sql-server-2005 sql-server dynamic-sql t-sql parameter
对于一份报告,我正在查询用户假设必须根据百分比选择 TOP 值或固定行数。
我有两个想法
根据传递的参数调用两个不同的子存储过程。
if @param ='percent'
begin
exec sp_data_TOP_by_Percent
end
if @param ='perRow'
begin
exec sp_data_TOP_by_PerRow
end
Run Code Online (Sandbox Code Playgroud)
另一个想法是使动态 TSQL 查询像这样
declare @command nchar(max)
select @command = 'select top(10) '
+ case @param
when 'percent' then percent
else ' '
end
+ ' * '
+ 'from table
order by 1';
exec sp_executesql @command
Run Code Online (Sandbox Code Playgroud)
像这样的事情有第三种解决方案吗?
什么是更好的方法?第一个避免动态 TSQL,但在两个地方更难维护代码。我使用 MSSQL2005 作为数据库。
归档时间: |
|
查看次数: |
433 次 |
最近记录: |