SQL Server 模板 - 如何转义小于字符?

发布于 2024-08-07 13:26:55 字数 245 浏览 2 评论 0原文

我喜欢使用 SQL Server 2005 模板来运行常用查询。您可以使用以下语法在模板中包含参数:

<LastName, varchar, 'Bob'>

我有一个查询需要小于或等于运算符<=,但不幸的是,SQL Server 2005 模板将其解释为参数的开头。我一直无法找到使用 < (小于字符)作为文字的方法。

I like to use SQL Server 2005 templates to run frequently used queries. You can include parameters in your templates using this syntax:

<LastName, varchar, 'Bob'>

I have a query that needs the less than or equals to operator <= but unfortunately the SQL Server 2005 template interprets that as the start of a parameter. I have been unable to find a way to use the < (less than character) as a literal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

平生欢 2024-08-14 13:26:55

当我指定模板参数的值时,这对我来说运行良好:

select * from <xyz, varchar,YourTable> WHERE ID<=1000 AND ID>=20000

也许您没有每个参数的“<”和“>”正确配对

编辑我现在看到问题:

SELECT * FROM <xyz, varchar,YourTable> WHERE ID<=1000 AND ID>=20000 AND <xyz2,varchar,YourColumn> IS NOT NULL

结果是:

SELECT * FROM YourTable WHERE IDYourColumn IS NOT NULL

尝试制作“<”字符到参数中,如下所示:

SELECT * FROM <xyz, varchar,YourTable> WHERE ID<lessthan,char,<>=1000
AND ID>=20000 AND <<xyz2,varchar,YourColumn> IS NOT NULL

它的结果是:

SELECT * FROM YourTable WHERE ID<=1000
AND ID>=20000 AND YourColumn IS NOT NULL

或分割行,换行符似乎有所不同:

SELECT * FROM <xyz, varchar,YourTable> WHERE ID<=1000 AND ID>=20000 
AND <xyz2,varchar,YourColumn> IS NOT NULL

结果是:

SELECT * FROM YourTable WHERE ID<=1000 AND ID>=20000 
AND YourColumn IS NOT NULL

when I Specify Values for Template Parameters, this runs fine for me:

select * from <xyz, varchar,YourTable> WHERE ID<=1000 AND ID>=20000

perhaps you do not have every parameter's "<" and ">" paired properly

EDIT I see the problem now:

SELECT * FROM <xyz, varchar,YourTable> WHERE ID<=1000 AND ID>=20000 AND <xyz2,varchar,YourColumn> IS NOT NULL

results in:

SELECT * FROM YourTable WHERE IDYourColumn IS NOT NULL

try making the "<" character into a parameter, like this:

SELECT * FROM <xyz, varchar,YourTable> WHERE ID<lessthan,char,<>=1000
AND ID>=20000 AND <<xyz2,varchar,YourColumn> IS NOT NULL

it results in:

SELECT * FROM YourTable WHERE ID<=1000
AND ID>=20000 AND YourColumn IS NOT NULL

OR split the lines, line breaks seem to make a difference:

SELECT * FROM <xyz, varchar,YourTable> WHERE ID<=1000 AND ID>=20000 
AND <xyz2,varchar,YourColumn> IS NOT NULL

results in:

SELECT * FROM YourTable WHERE ID<=1000 AND ID>=20000 
AND YourColumn IS NOT NULL
只为一人 2024-08-14 13:26:55

使用模板

select top 10 * from syscolumns
where <xtype, varchar(128), xtype <= 60>

如果选择(菜单)查询>指定模板参数的值,替换的默认值是“xtype <= 60”,这是正确的,并且替换后,生成的查询文本正是

select top 10 * from syscolumns
where xtype <= 60

人们所期望的。换句话说,“<”似乎并非如此。符号需要转义。然而,“>”更有问题的是:

select top 10 * from syscolumns
where <xtype, varchar(128), xtype >= 60>

打开“指定值”对话框时这会失败。但是,在这种情况下,可以指定

select top 10 * from syscolumns
where <xtype, varchar(128), value>

并输入

xtype >= 60

“值”字段以进行替换。这

select top 10 * from syscolumns
where xtype >= 60

又产生了人们所期望的结果。所以看来替换的默认值可能不包含“>”。

With template

select top 10 * from syscolumns
where <xtype, varchar(128), xtype <= 60>

If you select (menu) Query > Specify Values for template parameters, the default value for replacement is "xtype <= 60", which is correct, and upon substitution, the resulting query text is

select top 10 * from syscolumns
where xtype <= 60

which is exactly what one would expect. In other words, it does not appear to be the case that the "<" symbol needs to be escaped. However, ">" is more problematic:

select top 10 * from syscolumns
where <xtype, varchar(128), xtype >= 60>

This will fail when opening the "specify values" dialog. However, in this instance, it is fine to specify

select top 10 * from syscolumns
where <xtype, varchar(128), value>

and enter

xtype >= 60

in the "value" field for replacement. This produces

select top 10 * from syscolumns
where xtype >= 60

which is again as one would expect. So it would seem that the default value for replacement may not contain a ">".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文