参数化临时脚本
Parameterizing adhoc scripts
在 SQL 服务器中,我总是有一组诊断脚本,并且始终确保使用标识符声明变量,以便我的其他 select 和更新能够利用它们。我在 Oracle 中采用这种模式时遇到问题。
我可能有 4 或 5 个 select 查询,然后还有一些更新,一旦我验证了结果,我可能会取消评论。我想在输出中查看 select 查询的结果。
我正在使用 SQL 开发人员。
首先我尝试使用 DEFINE
块,但这似乎必须与 BEGIN/END 块配对,并且一旦查询在块内,查看结果。我看到的示例要么涉及设置光标然后遍历光标以打印结果,要么必须打印更麻烦的单个值。
所以我尝试使用变量,因为我可以在没有 declare/begin/end 的情况下引用它们,但是我在设置变量的值时遇到了问题:
variable customerid number;
customerid := 1234;
但是我得到这个错误:
Error starting at line : 5 in command - customerid := 1234 Error
report - Unknown Command
我也试过了
select t.customerid into :customerid
from customer t
where t.customerid = 1234
并得到:
SQL Error: ORA-01006: bind variable does not exist
01006. 00000 - "bind variable does not exist"
我的目标是将我的 id 声明放在我设置值的顶部,并且能够 运行 脚本和我所有的临时 select 出现在输出.
您需要在 PL/SQL 上下文中设置绑定变量,或者使用 execute
syntactic wrapper:
variable customerid number;
exec :customerid := 1234;
或稍微明确一点:
variable customerid number;
begin
:customerid := 1234;
end;
/
这(几乎)是等价的,但如果你想设置多个变量可能会更方便。您也可以像您尝试的那样从查询中填充绑定变量,但这也需要在 PL/SQL 上下文中:
begin
select t.customerid into :customerid
from customer t
where t.customerid = 1234;
end;
/
注意 customerid
之前的冒号,表示它是一个绑定变量,在所有这些中。当您稍后引用它时,您需要它,例如在 SQL 查询中(不需要在 PL/SQL 块中):
select * from customer where customerid = :customerid;
您可以在以后的更新中使用相同的机制。使用冒号的例外情况是您只想查看变量的值;你可以 select :customerid from dual
,但是 there is also the ability to
print customerid
如果您的变量是 refcursor
。
,那将更有用
define
是一种完全不同的机制,用于 substitution variables 而不是绑定变量。您也不需要为此使用 PL/SQL 块:
define customerid=1234
select * from customer where customerid = &customerid;
注意这次没有冒号。并且还要注意,如果你的变量是字符串,使用的时候需要用引号括起来:
define name=aaron
select * from users where first_name = '&name';
您还可以使用查询结果填充替换变量,使用 the new_value
syntax。
在 SQL 服务器中,我总是有一组诊断脚本,并且始终确保使用标识符声明变量,以便我的其他 select 和更新能够利用它们。我在 Oracle 中采用这种模式时遇到问题。
我可能有 4 或 5 个 select 查询,然后还有一些更新,一旦我验证了结果,我可能会取消评论。我想在输出中查看 select 查询的结果。
我正在使用 SQL 开发人员。
首先我尝试使用 DEFINE
块,但这似乎必须与 BEGIN/END 块配对,并且一旦查询在块内,查看结果。我看到的示例要么涉及设置光标然后遍历光标以打印结果,要么必须打印更麻烦的单个值。
所以我尝试使用变量,因为我可以在没有 declare/begin/end 的情况下引用它们,但是我在设置变量的值时遇到了问题:
variable customerid number;
customerid := 1234;
但是我得到这个错误:
Error starting at line : 5 in command - customerid := 1234 Error report - Unknown Command
我也试过了
select t.customerid into :customerid
from customer t
where t.customerid = 1234
并得到:
SQL Error: ORA-01006: bind variable does not exist 01006. 00000 - "bind variable does not exist"
我的目标是将我的 id 声明放在我设置值的顶部,并且能够 运行 脚本和我所有的临时 select 出现在输出.
您需要在 PL/SQL 上下文中设置绑定变量,或者使用 execute
syntactic wrapper:
variable customerid number;
exec :customerid := 1234;
或稍微明确一点:
variable customerid number;
begin
:customerid := 1234;
end;
/
这(几乎)是等价的,但如果你想设置多个变量可能会更方便。您也可以像您尝试的那样从查询中填充绑定变量,但这也需要在 PL/SQL 上下文中:
begin
select t.customerid into :customerid
from customer t
where t.customerid = 1234;
end;
/
注意 customerid
之前的冒号,表示它是一个绑定变量,在所有这些中。当您稍后引用它时,您需要它,例如在 SQL 查询中(不需要在 PL/SQL 块中):
select * from customer where customerid = :customerid;
您可以在以后的更新中使用相同的机制。使用冒号的例外情况是您只想查看变量的值;你可以 select :customerid from dual
,但是 there is also the ability to
print customerid
如果您的变量是 refcursor
。
define
是一种完全不同的机制,用于 substitution variables 而不是绑定变量。您也不需要为此使用 PL/SQL 块:
define customerid=1234
select * from customer where customerid = &customerid;
注意这次没有冒号。并且还要注意,如果你的变量是字符串,使用的时候需要用引号括起来:
define name=aaron
select * from users where first_name = '&name';
您还可以使用查询结果填充替换变量,使用 the new_value
syntax。