当我运行此查询时,我收到此错误:
程序sp_GET_RANDOM_QUESTIONS,第40行 当未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式。 消息116,级别16,状态1,过程sp_GET_RANDOM_QUESTIONS,第52行 当未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式。
这是一个存储过程,用于提供随机问题,@ easy,@ medium,@ hard是随机化的问题数。 如果用户选择只给出@num,我会将该数字除以3(3个难产)并得到那些随机问题。
SELECT case
WHEN @num=0 THEN (
SELECT * FROM (
SELECT top (@easy) * from TBL_PERGUNTAS as p where p.category=1 order by NEWID()) as a
UNION ALL SELECT * FROM
(SELECT top (@medium) * from TBL_PERGUNTAS as p where p.category=2 order by NEWID()) as b
UNION ALL SELECT * FROM
(SELECT top (@hard) * from TBL_PERGUNTAS as p where p.category=3 order by NEWID()) as c)
else (
SELECT * FROM
(SELECT top (@num/3) * from TBL_PERGUNTAS as p where p.category=1 order by NEWID()) as a
UNION ALL SELECT * FROM
(SELECT top (@num/3) * from TBL_PERGUNTAS as p where p.category=2 order by NEWID()) as b
UNION ALL SELECT * FROM
(SELECT top (@num/3) * from TBL_PERGUNTAS as p where p.category=3 order by NEWID()) as c )
END
答案 0 :(得分:1)
正如评论所述,你需要使用IF/ELSE
来控制流量,试试这个:
IF @num=0 THEN
BEGIN
SELECT top (@easy) *
FROM TBL_PERGUNTAS as p
WHERE p.category=1
ORDER BY NEWID()
UNION ALL
SELECT top (@medium) *
FROM TBL_PERGUNTAS as p
WHERE p.category=2
ORDER BY NEWID()
UNION ALL
SELECT top (@hard) *
FROM TBL_PERGUNTAS as p
WHERE p.category=3
ORDER BY NEWID())
END
ELSE
BEGIN
SELECT top (@num/3) *
FROM TBL_PERGUNTAS as p
WHERE p.category=1
ORDER BY NEWID()
UNION ALL
SELECT top (@num/3) *
FROM TBL_PERGUNTAS as p
WHERE p.category=2
ORDER BY NEWID()
UNION ALL
SELECT top (@num/3) *
FROM TBL_PERGUNTAS as p
WHERE p.category=3
ORDER BY NEWID())
END