简体   繁体   English

字符串连接未在SELECT语句中按预期方式工作

[英]String concatenation not working as expected in SELECT statement

I'm using MS Access to work with an SQL Server database through an ODBC connection on Windows 7. 我正在使用MS Access通过Windows 7上的ODBC连接使用SQL Server数据库。

The ultimate goal is to append a string literal to one of the fields for a subset of rows. 最终目标是将字符串文字附加到一部分行的字段中。 Initially, though, I'm just trying to do a SELECT so I can make sure I have everything correct. 不过,起初,我只是尝试执行SELECT,以便确保一切正确。 I'm having trouble trying to append a string literal to the field. 我在尝试将字符串文字附加到字段时遇到麻烦。

The below simple SQL works well. 下面的简单SQL很好用。

SELECT Name FROM Customers WHERE CustomerType = 1;

Next step was to try and modify the displayed name slightly. 下一步是尝试稍微修改显示的名称。

SELECT Name, 'PREFIX' & Name FROM Customers WHERE CustomerType = 1;

The above also worked. 以上也有效。 Then I tried the following. 然后,我尝试了以下方法。

SELECT Name, Name & 'SUFFIX' FROM Customers WHERE CustomerType = 1;

This does not work. 这是行不通的。 The output shows just the Name field with nothing appended. 输出仅显示名称字段,未附加任何内容。 I looked around and found SQL Server seems to support CONCATENATE('a' + 'b'). 我环顾四周,发现SQL Server似乎支持CONCATENATE('a'+'b')。 I tried using that in the query but it failed with an error from Access about there not being a CONCATENATE function. 我尝试在查询中使用该函数,但由于Access错误,提示没有CONCATENATE函数而失败。

I also tried double quotes instead and + instead of &. 我也尝试用双引号代替+,而不是&。

Seems odd that the prefix case worked and the suffix case did not. 似乎奇怪的是前缀大小写有效而后缀大小写无效。

The eventual goal, again, would be to construct something like the below. 最终的目标还是要构造如下所示的内容。

UPDATE Customers SET Name = Name & 'SUFFIX' WHERE CustomerType = 1;

This would append a suffx to a text field for a subset of rows in the table. 这会将后缀添加到表中行子集的文本字段中。

Any ideas? 有任何想法吗?

In SQL Server, & is for binary masks. 在SQL Server中, &表示二进制掩码。 You want the + operator 您想要+运算符

UPDATE Customers 
SET Name = Name + 'SUFFIX' 
WHERE CustomerType = 1;

I don't know where you got CONCATENATE from - There is a CONCAT function in SQL 2012, but nothing like that in any other version 我不知道您从哪里获得CONCATENATE -SQL 2012中有一个CONCAT函数,但在任何其他版本中却没有这样的函数

My impression is you have an Access query with Customers as an ODBC link to a SQL Server table. 我的印象是您有一个与Customers的Access查询,作为到SQL Server表的ODBC链接。 If that is correct, either of these 2 query versions should work. 如果正确,那么这两个查询版本中的任何一个都应该起作用。

SELECT
    [Name],
    [Name] & 'SUFFIX'
FROM Customers
WHERE CustomerType = 1;

SELECT
    c.Name,
    c.Name & 'SUFFIX'
FROM Customers AS c
WHERE c.CustomerType = 1;

The reason for this suggestion is that Name is a reserved word . 提出此建议的原因是Name保留字 One thing that makes reserved words frustrating is that you don't know when they will bite you. 使保留字令人沮丧的一件事是,您不知道它们何时会咬您。 A reserved word might not cause trouble in one context but cause the same SQL statement to fail in another context. 保留字可能不会在一个上下文中引起麻烦,但会导致同一SQL语句在另一个上下文中失败。 Enclosing the name in square brackets or qualifying the name with an alias (or the table name) avoids confusing Access' db engine. 将名称括在方括号中或使用别名(或表名)限定名称,可以避免混淆Access的数据库引擎。

Try this for your UPDATE . 尝试此操作进行UPDATE

UPDATE Customers
SET [Name] = [Name] & 'SUFFIX'
WHERE CustomerType = 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM