delete_cmd := 'delete from test where rdate='||''':run_date'''||';';
上面的代码没有替换分配给“run_date”的日期“2022-09-07”。
您能帮忙解决当前的替换问题吗?
上下文有帮助,所以让我们编写一个 Snowflake 脚本块,听起来就像您所描述的那样:
declare
delete_cmd text;
run_date text;
begin
run_date := '2022-09-07';
delete_cmd := 'delete from test where rdate='||''':run_date'''||';';
return delete_cmd;
end;
果然“效果不好”
匿名区块 |
---|
从测试中删除其中 rdate=':run_date'; |
因此,查看该代码,我们看到您正在将三个字符串连接在一起,其中第二个字符串是三重引号,因此是一个带有嵌入引号的字符串,并且字符串标记
:run_date
作为文本。所以让我们去掉三引号:
delete_cmd := 'delete from test where rdate='|| :run_date ||';';
现在我们得到:
匿名区块 |
---|
从测试中删除,其中 rdate=2022-09-07; |
所以现在变量被替换了,但是引号丢失了..所以我们将嵌入的引号放在字符串部分(之前和之后)
delete_cmd := 'delete from test where rdate='''|| :run_date ||''';';
输出结果就是我们想要的:
匿名区块 |
---|
从测试中删除其中 rdate='2022-09-07'; |