获取返回值问题
方法一:
DECLARE @C INT
SELECT @C=COUNT(*) FROM T_MATERIAL;
PRINT @C
方法二:
DECLARE @C INT
-- EXEC( 'SELECT COUNT(*) FROM T_MATERIAL ');
EXEC( 'SELECT @C= COUNT(*) FROM T_MATERIAL ');
PRINT @C
通过方法一我可以获得T_MATERIAL表的记录数,
我想通过方法二也想获得T_MATERIAL表的记录数,但我的方法有错,请高手帮我解决!
------解决方案--------------------方法一就不说了
DECLARE @C INT
SELECT @C=COUNT(*) FROM T_MATERIAL;
PRINT @C
方法二:
EXEC( 'DECLARE @C INT
SELECT @C= COUNT(*) FROM T_MATERIAL
PRINT @C ');
或
CREATE TABLE #C (C int)
insert into #C EXEC( 'SELECT COUNT(*) FROM T_MATERIAL ')
SELECT C FROM #C
------解决方案--------------------DECLARE @C INT
-- EXEC( 'SELECT COUNT(*) FROM T_MATERIAL ');
EXEC( 'SELECT '+@C '+= COUNT(*) FROM T_MATERIAL ');
PRINT @C
试试看吧。呵呵
------解决方案--------------------declare @num int,
@sqls nvarchar(4000)
set @sqls= 'SELECT @C= COUNT(*) FROM T_MATERIAL '
exec sp_executesql @sqls,N '@C int output ',@num output
select @num
------解决方案--------------------SELECT @C= COUNT(*) FROM T_MATERIAL
哈哈哈!