动态拼字符串,嵌套if else

Jenf_Mu 2018-03-23 11:06:58

declare @v;

EXEC(
'select top '
+
(if @v = 1 '1' else '10')
+
' * from appointment'
)

想根据变量值的不同,拼出不同的SQL
...全文
812 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
中国风 2018-03-23
  • 打赏
  • 举报
回复
改为 e.g.
declare @v INT,@sql VARCHAR(max);
 
 SET @sql='select top '
+
(CASE WHEN   @v = 1 THEN '1' else '10' END)
+
' * from appointment'
EXEC(
@sql
)
Jenf_Mu 2018-03-23
  • 打赏
  • 举报
回复
回错贴了,抱歉
引用 2 楼 Jenf_Mu 的回复:
int QSqlQuery::size() const

Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes. Note that for non-SELECT statements (isSelect() returns false), size() will return -1. If the query is not active (isActive() returns false), -1 is returned.
数据库不支持返回行数,Qt也没办法。 建议用next()遍历数据集。 如果实在想知道多少行,那就查count呗。
Jenf_Mu 2018-03-23
  • 打赏
  • 举报
回复
int QSqlQuery::size() const

Returns the size of the result (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes. Note that for non-SELECT statements (isSelect() returns false), size() will return -1. If the query is not active (isActive() returns false), -1 is returned.
数据库不支持返回行数,Qt也没办法。 建议用next()遍历数据集。 如果实在想知道多少行,那就查count呗。
卖水果的net 2018-03-23
  • 打赏
  • 举报
回复
1、if 语句,你要换成 case when 2、把动态语句,在 exec 之前,先拼接完成

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧