如何将文本框中输入的文本用作 SQL 中的变量?I'm a beginner with application builders and I want to achieve the following: I'm currently retrieving data from a database using SQL with a specific time period. However, instead of directly modifying the SQL code to change the period, I want to be able to change it via a GUI. I'm currently executing this with JSL, and I want to replace the [HERE] part of the SQL query.
JSL 示例:
// Get a handle to the data table
dt = Current Data Table();
unique = Function({a}, // input: List, output: List
{aa=Associative Array(),nr=NItems(a),i,aKeys,nUnique,aUnique={}},
For( i=1, i<=nr, i++, aa << insert( a[i] ) );
aKeys = aa << GetKeys;
nUnique = NItems( aKeys );
For( i = 1, i <= nUnique, i++, InsertInto(aUnique,aKeys[i]) );
aUnique
);
dofun = Function({},{months,varName,dType},
months = SampleRateInput << Get;
varName = VarList << GetSelected;
dType = Column(dt,varName[1]) << GetDataType( "English");
If(dType!="Character",
Dialog(Title("Alert"),"Data Type of Variable should be Character. ");
Throw()
);
dt << ClearSelect;
dt << SelectRows( selIdx );
);
OKButtonPress=Function({this},
// This function is called when the OK button is pressed
(thisModuleInstance << Get Box) << Close Window;
dofun;
);
CancelButtonPress=Function({this},
// This function is called when the Cancel button is pressed
(thisModuleInstance << Get Box) << Close Window;
);
New SQL Query(
Version( 130 ),
Connection( ),
QueryName( ),
Select( ),
From(
Table( ),
),
Where(
Custom(
"t1.msr_dt >= DATEADD(month, -[HERE], CURRENT_DATE)",
UI( Custom( Base( "Continuous" ) ) )
)
)
) << Run Foreground( UpdateTable( Current Data Table() ) );
数据访问回复:如何将文本框中输入的文本用作 SQL 中的变量?我的JMP是19版本。
回复:如何将文本框中输入的文本用作 SQL 中的变量?您是否有自定义的 GUI 来更改值?您能提取该值吗?我认为您可以直接在 Where(Custom()) 块中计算变量(至少使用 JMP18 即可)。
Names Default To Here(1);
myval = 150;
dt = New SQL Query(
Version(130),
Connection("JMP"),
JMP Tables(["Big Class" => "\C:\Program Files\JMP\JMPPRO\18\Samples\Data\Big Class.jmp"]),
QueryName("SQLQuery2"),
Select(Column("name", "t1")),
From(Table("SQLQuery1", Alias("t1"))),
Where(
Custom(
Eval Insert("t1.weight >= ^myval^"),
UI(Custom(Base("Continuous"))))
)
) << Run Foreground;
回复:如何将文本框中输入的文本用作 SQL 中的变量?jthi-san,Arigatougozaimasu。
之前 JSL 中描述的 GUI 是示例应用程序 JSL 的精简版。我实际想要创建的 GUI 就像附图中那样。
我知道我可以通过用“^”括起来的方式在 SQL 语句中传递变量!
当按下 GUI 中的“确定”按钮时,我应该编写什么 JSL 代码来执行新的 SQL 查询? 
回复:如何将文本框中输入的文本用作 SQL 中的变量?你好@TamedZebra ,
与 JSL 方面无关,但您是否尝试过应用数据库查询生成器?它提供了一个 GUI,可提供用于更改日期范围的过滤器。
谢谢,
本
回复:如何将文本框中输入的文本用作 SQL 中的变量?@Ben_BarrIngh ,Arigatougozaimasu。
我们可以在数据表上保存一个脚本,如果脚本是“SQL查询”,我们可以通过一键从数据库更新数据表。
但是在脚本或查询生成器上更改 SQL 需要花费一些时间和精力。
因此,我想通过在数据表上按下脚本弹出 GUI 来编辑 SQL。

回复:如何将文本框中输入的文本用作 SQL 中的变量?我不使用应用程序构建器,而是用 JSL 自己编写所有代码。这个示例应用程序不会对数据库执行 SQL 查询,而是使用 JMP 表作为“数据库”。这是一个相当简化的版本,但可以提供一些通用的思路。
Names Default To Here(1);
initval = 150;
run_query = Expr(
months_to_query = neb_months << get;
dt = New SQL Query(
Version(130),
Connection("JMP"),
JMP Tables(["Big Class" => "\C:\Program Files\JMP\JMPPRO\18\Samples\Data\Big Class.jmp"]),
QueryName("QueryMonths"),
Select(Column("name", "t1")),
From(Table("Big Class", Alias("t1"))),
Where(
Custom(
Eval Insert("t1.weight >= ^months_to_query^"),
UI(Custom(Base("Continuous"))))
)
) << Run Foreground;
wait(0);
nw << Close Window;
);
nw = New Window("Query Data",
V List Box(
Panel Box("How Long",
Lineup Box(N Col(2),
Text Box("Months: "),
neb_months = Number Edit Box(initval),
)
),
Lineup Box(N Col(2),
Button Box("OK",
run_query
),
Button box("Cancel",
nw << close window;
)
)
)
);
Write();
回复:如何将文本框中输入的文本用作 SQL 中的变量?获取 --> 获取文本
我能够在 JMP 19 中执行上述更改。我实现了我的目标。谢谢!