Sql server 查询不通过过程显示结果

Sql server 查询不通过过程显示结果,sql-server,stored-procedures,Sql Server,Stored Procedures,我正在使用SQLServer2005DB中的一个存储过程,该过程有一个查询片段 . . . if(@zipCodeList <> '') BEGIN SET @zipCodeList = ''''+replace(replace(replace(@zipCodeList, ' ', ''),',',''','''),'_',' ')+'''' END SELECT distinct ZIPCode from zip_table where ZIPCode_col I

我正在使用SQLServer2005DB中的一个存储过程,该过程有一个查询片段

.
.
.


if(@zipCodeList <> '')
BEGIN
    SET @zipCodeList = ''''+replace(replace(replace(@zipCodeList, ' ', ''),',',''','''),'_',' ')+''''
END 

SELECT distinct ZIPCode
from zip_table
where ZIPCode_col  IN (@zipCodeList)
AND power((69.1 * (abs(Longitude) - abs(Longitude)) * cos(abs(Latitude)/57.3)),2) +
Power(69.1 * (abs(Latitude) - abs(Latitude)), 2) <= (60 * 60)
AND more conditions 
.
.
.
DB有记录,但过程并没有显示任何内容,我打印了查询,它也打印了

SELECT distinct ZIPCode
from zip_table
where ZIPCode_col  IN ('02124','23568')
AND power((69.1 * (abs(Longitude) - abs(Longitude)) * cos(abs(Latitude)/57.3)),2) +
Power(69.1 * (abs(Latitude) - abs(Latitude)), 2) <= (60 * 60)
AND more conditions
当我运行这个查询时,它会返回正确的结果。看起来@zipCodeList在运行时引起了一些问题,但没有给出任何错误,请任何人帮助我,我哪里错了


提前感谢。

如果@zipCodeList的值为1,2,3,请执行以下代码

SELECT distinct ZIPCode
from zip_table
where ZIPCode_col  IN 
(
    SELECT PARSENAME(REPLACE(Split.a.value('.', 'VARCHAR(500)'),'-','.'),1) 'Ids' 
    FROM  
    (
         SELECT CAST ('<M>' + REPLACE(@zipCodeList, ',', '</M><M>') + '</M>' AS XML) AS Data       
    ) AS A 
    CROSS APPLY Data.nodes ('/M') AS Split(a)
)
AND power((69.1 * (abs(Longitude) - abs(Longitude)) * cos(abs(Latitude)/57.3)),2) +
Power(69.1 * (abs(Latitude) - abs(Latitude)), 2) <= (60 * 60)
AND more conditions 
在@zipCodeList中,是无效的构造,不能在单个字符串变量中传递值列表。如果无法传入单个值并创建表变量,则需要将值拆分为一个集合,请参见:
SELECT distinct ZIPCode
from zip_table
where ZIPCode_col  IN 
(
    SELECT PARSENAME(REPLACE(Split.a.value('.', 'VARCHAR(500)'),'-','.'),1) 'Ids' 
    FROM  
    (
         SELECT CAST ('<M>' + REPLACE(@zipCodeList, ',', '</M><M>') + '</M>' AS XML) AS Data       
    ) AS A 
    CROSS APPLY Data.nodes ('/M') AS Split(a)
)
AND power((69.1 * (abs(Longitude) - abs(Longitude)) * cos(abs(Latitude)/57.3)),2) +
Power(69.1 * (abs(Latitude) - abs(Latitude)), 2) <= (60 * 60)
AND more conditions