SQL循环不将所有输入记录添加到临时表

时间:2014-08-14 16:07:31

标签: sql sql-server tsql

我有一个由另一个人编写的SQL查询,它有一些问题。另外,我为这个代码的物理长度大声抱歉lol。我意识到它很长,但我认为有必要包括所有邮政编码,以便我可以提出我所拥有的问题。

查询接受邮政编码列表,循环将它们全部放入表变量中,然后根据我们的数据库查询该表。

我遇到的问题是,它只是循环并获取输入的前31个邮政编码。在下面的示例中,我输入了89个邮政编码,但临时表在打印时仅显示31个记录。

--this is your list of zip codes, place a comman after every zip
SET @zip_list = 
'
30004,
31061,
30606,
27103,
28405,
53140,
60124,
60002,
44114,
77380,
75240,
77380,
75605,
72223,
72223,
76006,
78759,
76006,
50266,
50266,
50266,
49024,
78258,
67202,
92101,
92101,
92101,
92101,
90025,
90025,
55447,
90025,
92101,
90025,
55802,
55431,
55431,
55431,
37027,
02842,
10152,
10152,
02108,
07701,
08822,
94596,
94595,
55431,
92130,
92130,
95814,
95814,
93720,
95403,
94608,
92101,
19103,
08648,
18901,
18901,
18901,
19103,
85258,
85258,
85718,
85258,
85258,
98104,
98104,
97201,
89144,
92130,
92101,
92101,
97201,
97201,
97201,
63017,
63131,
22102,
21030,
22102,
22102,
20015,
22102,
20015,
20015,
20005,
06897,
' --this is your list of zip codes, place a , after every zip. Make sure the list is horizontal
---------------------------------

SET @zip_list = REPLACE(REPLACE(@zip_list,CHAR(13),''),CHAR(10),'')  --Remove new line character to place zip codes all onto one line
print @zip_list

WHILE CHARINDEX(',', @zip_list) > 0
    BEGIN
       INSERT INTO @tbl_zip_list
       --insert into @tbl_zip_list a substring equal to the first character from your string of zip codes + all the characters up to the first comma
       SELECT SUBSTRING(@zip_list, 1, CHARINDEX(',', @zip_list) - 1) 
       --set the string of zip codes equal to your original string less the first zip code, which we just added to @tbl_zip_list
       SET @zip_list = SUBSTRING(@zip_list, CHARINDEX(',', @zip_list) + 1, LEN(@zip_list))
    END
----------------------------------
SELECT *
FROM @tbl_zip_list

SELECT oad.POSTAL_CODE_1,
      o.CHANNEL
FROM dbo.OFFICE AS o
    INNER JOIN dbo.OFFICE_ALIAS AS oa
    ON o.OFFICE_ID = oa.OFFICE_ID
    INNER JOIN dbo.OFFICE_ADDRESS AS oad
    ON o.ADDRESS_1_ID = oad.OFFICE_ADDRESS_ID
WHERE oa.TRADE_FIRM = @trade_firm
  AND o.CHANNEL != (SELECT f.channel
                    FROM dbo.FIRM AS f
                        INNER JOIN dbo.FIRM_ALIAS AS fa
                        ON f.FIRM_ID = fa.FIRM_ID
                    WHERE fa.trade_firm = @trade_firm)--will return the channel of the main firm */
  AND EXISTS (SELECT 1
                    FROM @tbl_zip_list AS n1
                    WHERE n1.zip_list = oad.POSTAL_CODE_1) --this returns table of zip codes
------------------------------------------------
select f.channel as The_Default_Channel_For_Your_Firm_Is
from dbo.FIRM f INNER JOIN dbo.FIRM_ALIAS fa on f.FIRM_ID = fa.FIRM_ID
where fa.trade_firm = @trade_firm

我不知道如何修复循环,以便将所有89个邮政编码放入表中。下面是表的SQL-Server输出的屏幕截图。我只是不明白为什么它只有31条记录,应该是89条。

enter image description here

2 个答案:

答案 0 :(得分:2)

这样可行,我得到89回。

您必须声明@zip_list太小才能容纳整个字符串。

declare @tbl_zip_list table(zip varchar(10))
declare @zip_list varchar(8000)

--this is your list of zip codes, place a comman after every zip
SET @zip_list = 
'
30004,
31061,
30606,
27103,
28405,
53140,
60124,
60002,
44114,
77380,
75240,
77380,
75605,
72223,
72223,
76006,
78759,
76006,
50266,
50266,
50266,
49024,
78258,
67202,
92101,
92101,
92101,
92101,
90025,
90025,
55447,
90025,
92101,
90025,
55802,
55431,
55431,
55431,
37027,
02842,
10152,
10152,
02108,
07701,
08822,
94596,
94595,
55431,
92130,
92130,
95814,
95814,
93720,
95403,
94608,
92101,
19103,
08648,
18901,
18901,
18901,
19103,
85258,
85258,
85718,
85258,
85258,
98104,
98104,
97201,
89144,
92130,
92101,
92101,
97201,
97201,
97201,
63017,
63131,
22102,
21030,
22102,
22102,
20015,
22102,
20015,
20015,
20005,
06897,
' --this is your list of zip codes, place a , after every zip. Make sure the list is horizontal
---------------------------------

SET @zip_list = REPLACE(REPLACE(@zip_list,CHAR(13),''),CHAR(10),'')  --Remove new line character to place zip codes all onto one line
print @zip_list

WHILE CHARINDEX(',', @zip_list) > 0
    BEGIN
       INSERT INTO @tbl_zip_list
       --insert into @tbl_zip_list a substring equal to the first character from your string of zip codes + all the characters up to the first comma
       SELECT SUBSTRING(@zip_list, 1, CHARINDEX(',', @zip_list) - 1) 
       --set the string of zip codes equal to your original string less the first zip code, which we just added to @tbl_zip_list
       SET @zip_list = SUBSTRING(@zip_list, CHARINDEX(',', @zip_list) + 1, LEN(@zip_list))
    END
----------------------------------
SELECT *
FROM @tbl_zip_list

答案 1 :(得分:0)

如果在dbo.OFFICE_ADDRESS中确实没有匹配的邮政编码,则可能是另一个问题。要进行验证,请运行以下内容并查看您获得的结果数量(如果您只获得31,那么您知道这是因为您的邮政编码与列表中的邮政编码不匹配)。否则,很可能是JiggsJedi所说的,而且变量太短了

测试查询:

DECLARE @zip_list AS varchar(max)
DECLARE @tbl_zip_list TABLE (zip INT)
SET @zip_list = 
'
30004,
31061,
30606,
27103,
28405,
53140,
60124,
60002,
44114,
77380,
75240,
77380,
75605,
72223,
72223,
76006,
78759,
76006,
50266,
50266,
50266,
49024,
78258,
67202,
92101,
92101,
92101,
92101,
90025,
90025,
55447,
90025,
92101,
90025,
55802,
55431,
55431,
55431,
37027,
02842,
10152,
10152,
02108,
07701,
08822,
94596,
94595,
55431,
92130,
92130,
95814,
95814,
93720,
95403,
94608,
92101,
19103,
08648,
18901,
18901,
18901,
19103,
85258,
85258,
85718,
85258,
85258,
98104,
98104,
97201,
89144,
92130,
92101,
92101,
97201,
97201,
97201,
63017,
63131,
22102,
21030,
22102,
22102,
20015,
22102,
20015,
20015,
20005,
06897,
' --this is your list of zip codes, place a , after every zip. Make sure the list is horizontal
---------------------------------

SET @zip_list = REPLACE(REPLACE(@zip_list,CHAR(13),''),CHAR(10),'')  --Remove new line character to place zip codes all onto one line
print @zip_list

WHILE CHARINDEX(',', @zip_list) > 0
    BEGIN
       INSERT INTO @tbl_zip_list
       --insert into @tbl_zip_list a substring equal to the first character from your string of zip codes + all the characters up to the first comma
       SELECT SUBSTRING(@zip_list, 1, CHARINDEX(',', @zip_list) - 1) 
       --set the string of zip codes equal to your original string less the first zip code, which we just added to @tbl_zip_list
       SET @zip_list = SUBSTRING(@zip_list, CHARINDEX(',', @zip_list) + 1, LEN(@zip_list))
    END
----------------------------------
SELECT DISTINCT zip.zip
FROM @tbl_zip_list zip
    JOIN dbo.OFFICE_ADDRESS AS oad ON zip.zip_list = oad.POSTAL_CODE_1