Oracle 中 EXECUTE IMMEDIATE INTO 的 SQL 服务器等价物是什么
What is the SQL Server equivalent of EXECUTE IMMEDIATE INTO in Oracle
Oracle 中 EXECUTE IMMEDIATE INTO
的 SQL 服务器等效项是什么?
例如
DECLARE QRY varchar(100);
val int;
BEGIN
QRY:='select count(*) from production.product';
**EXECUTE IMMEDIATE** QRY **into** val;
dbms_output.put_line(val);
END;
/
T-SQL 等同于 EXECUTE IMMEDIATE 是 Dynamic 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
您不能将标量变量设置为动态查询的结果,但可以将结果插入到 table 变量中。这行得通。
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;
Oracle 中 EXECUTE IMMEDIATE INTO
的 SQL 服务器等效项是什么?
例如
DECLARE QRY varchar(100);
val int;
BEGIN
QRY:='select count(*) from production.product';
**EXECUTE IMMEDIATE** QRY **into** val;
dbms_output.put_line(val);
END;
/
T-SQL 等同于 EXECUTE IMMEDIATE 是 Dynamic 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
您不能将标量变量设置为动态查询的结果,但可以将结果插入到 table 变量中。这行得通。
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;