declare
    v_num number;
begin
    execute immediate 'select count(*) from tab' into v_num;
    dbms_output.put_line(v_num);
end;
/以上是Oracle中的语句,即查询tab表中的行数,并把此行数(标量)存进v_num变量,并打印输出。请问在SQL Server中如何写语句?declare @v_num int
sp_executesql 'select ... from tab' , ???
print @v_num谢谢。

解决方案 »

  1.   

    declare @v_num int
    declare @sql nvarchar(1000)
    set @sql=N'select @v_num=count(1) from tb'
    exec sp_executesql @sql,N'@v_num int output',@v_num output;
    print @v_num
      

  2.   


    declare @v_num int
    exec sp_executesql N'select @cnt = count(*) from bs_person' ,N'@cnt int output ',@cnt = @v_num output
    print @v_num
      

  3.   

    declare @SQLString nvarchar(200)
    declare @ParmDefinition nvarchar(200)
    /* Build the SQL string one time. */
    SET @SQLString =
         N'set @v_num =(SELECT count(1) FROM   TB) ';
    /* Specify the parameter format one time. */
    SET @ParmDefinition = N'@v_num int output';
    /* Execute the string with the first parameter value. */
    DECLARE @v_num int;
    EXECUTE sp_executesql @SQLString, @ParmDefinition   ,@v_num   output               
    /* Execute the same string with the second parameter value. */
    select @v_num
      

  4.   

    declare @iNum int
    declare @strSql nvarchar(max)
    set @strSql=N'select @iNum=count(1) from table'
    exec sp_executesql ''
    print @iNum
      

  5.   

    to wxf163 (小小菜) 。谢谢你。不好意思,结贴的时候,你的贴子我还没看到。