我正在尝试编写我认为简单的where子句。 FULLNAME字符串的格式如下(“Doe,John”),因此where子句和concat函数。
以下是查询:
SELECT *
FROM table
WHERE concat(LAST_NM, ', ', FIRST_NM) = FULLNAME;
查询失败,并显示以下错误:
执行数据库查询时出错。 [Macromedia] [SQLServer JDBC驱动程序] [SQLServer]','。
附近的语法不正确
答案 0 :(得分:0)
你的FullName是“Doe,John”吗? 你的FullName完整吗?
如果不是
SELECT *
FROM table
WHERE concat(LAST_NM, ', ', FIRST_NM) like ('%' + FULLNAME + '%')
答案 1 :(得分:0)
虽然我没有看到您的查询有任何问题,但我想通过尝试查找是否是导致concat函数中断的特定原始值来建议一种排除故障的方法。
我们的想法是遍历打印每个值的原始数据,以便您知道哪一个(如果有的话)失败了,并且从那里开始到下一步可能更容易。
--Copy your table data to a temporary table
--E.g.
SELECT *
into #table
FROM
(
select 'Doe' as LAST_NM , 'Jhon' as FIRST_NM
union
select 'Ryan' as LAST_NM , 'Jack' as FIRST_NM
union
select 'Dylan' as LAST_NM , 'Bob' as FIRST_NM
) a
--Iterate through the rows to see which one causes the issue
DECLARE @I INT = 1
DECLARE @TableCount INT = (select count(*) from #table)
DECLARE @FN varchar(100)
DECLARE @LN varchar(100)
WHILE @I <= @TableCount
BEGIN
SET @FN = (select Top 1 FIRST_NM from #table)
SET @LN = (select Top 1 LAST_NM from #table)
print(@I)
print(@FN)
print(@LN)
print('------------------')
delete top (1) from #table
SET @I = @I + 1
END