动态 sql 在变量中计数

dynamic sql count in a variable

这个查询有效

select @V_COUNT =COUNT(1) from TAB1

但我想将 table 作为变量传递 在 sybase 中如何做到这一点?

我试过了但是没用

select @V_COUNT ='COUNT(1) from ''||@TMP_TABLE_NAME||'''
select @LL_COUNT =  CONVERT(numeric(30),@V_COUNT)

编辑:

我做到了

 SELECT @V_COUNT ='SELECT COUNT(1) from '+ @TABLE_NAME
     execute (@V_COUNT)
     SELECT @LL_COUNT = 'SELECT convert(NUMERIC(6),'||@V_COUNT||')'

Implicit conversion from datatype 'VARCHAR' to 'NUMERIC' is not allowed. Use the CONVERT function to run this query.

您需要执行该动态查询,使用:

CREATE TABLE #Count(x int)
SELECT @sSQL = "INSERT #Count SELECT COUNT(1) from "+ @TMP_TABLE_NAME
execute (@sSQL)
SELECT @V_COUNT = x from #Count
DROP TABLE #Count
SELECT @LL_COUNT = (SELECT convert(NUMERIC(30,4), @V_COUNT))
SELECT @LL_COUNT

其中 @TMP_TABLE_NAME 可能是 SELECT @TMP_TABLE_NAME = 'tablename' 是您不需要执行或从中获取结果的简单字符串。