日期:2014-05-17 浏览次数:20523 次
USE [1112b] GO declare @in_kemu nvarchar(10) set @in_kemu = '语文' --这个大段是生成临时表 declare @QueryString nvarchar(500) declare @kemu nvarchar(10) set @kemu =@in_kemu --设置需传入动态语句的参数的值为@kemu set @QueryString ='SELECT '+@kemu+' as 科目, [ROW_NUMBER] = ROW_NUMBER() OVER(ORDER BY '+@kemu+' DESC), [DENSE_RANK] = DENSE_RANK() OVER(ORDER BY '+@kemu+' DESC), [PERCENT] = (ROW_NUMBER() OVER(ORDER BY '+@kemu+' DESC))*1./(SELECT COUNT(1) FROM scores_xx where 年级 ='+'1'+') into #biaozhunfen FROM scores_xx where 年级 = '+'1' exec (@QueryString) --请注意参数的顺序 --生成临时表结束 --在临时表中执行查询 declare @QueryString2 nvarchar(500) declare @kemu2 nvarchar(10) set @QueryString2 = 'SELECT TOP 1 '+@kemu2+' FROM ( SELECT * FROM #biaozhunfen T1 WHERE NOT EXISTS (SELECT 1 FROM #biaozhunfen WHERE [DENSE_RANK] = T1.[DENSE_RANK] AND [ROW_NUMBER] > T1.[ROW_NUMBER]) ) AS A ORDER BY ABS([PERCENT]-0.8)' set @kemu2 =@in_kemu --设置需传入动态语句的参数的值为@kemu exec (@QueryString2) --请注意参数的顺序