什么是Oracle中EXECUTE IMMEDIATE INTO的SQL Server等效项

时间:2019-04-10 19:10:12

标签: oracle11g sql-server-2012 dynamic-sql

Oracle中EXECUTE IMMEDIATE INTO的SQL Server等效项是什么?

例如

DECLARE QRY varchar(100);

val int;

BEGIN

QRY:='select count(*) from production.product';

**EXECUTE IMMEDIATE** QRY **into** val;

dbms_output.put_line(val);

END;

/

2 个答案:

答案 0 :(得分:1)

您不能为动态查询的结果设置标量变量,但可以将结果插入表变量中。这行得通。

DECLARE @tbl TABLE (RowCnt INT NULL);

DECLARE
   @QRY varchar(100);

BEGIN

  SET @QRY = 'select <Aggregate Function> from tableName'; --Builds the dynamic query

  INSERT @tbl
    (
      RowCnt
    )
  EXECUTE(@QRY); --Executes the query

  SELECT 
    * 
  FROM @tbl;

END;

答案 1 :(得分:0)

立即执行的T-SQL等效为动态Sql

DECLARE @intCount int
EXECUTE sp_executesql N'select @intCount=count(*) from product', N'@intCount int output', @intCount output;
Print(@intCount)

或者,您可以使用

DECLARE @intCount2 int
SELECT @intCount2 = count(*) from product