我有一个存储过程
CREATE PROCEDURE [dbo].[Proc_SplitAndRemoveDuplicates]
@sep VARCHAR(32),
@s NVARCHAR(MAX),
@hcp NVARCHAR(MAX) OUTPUT
AS
BEGIN
DECLARE @t TABLE (val NVARCHAR(MAX))
SET @hcp = ''
DECLARE @xml XML
SET @xml = N'<root><r>' + REPLACE(@s, @sep, '</r><r>') + '</r></root>'
PRINT CAST (@xml AS NVARCHAR(MAX))
INSERT INTO @t(val)
SELECT RTRIM(LTRIM(r.value('.','NVARCHAR(MAX)'))) as Item
FROM @xml.nodes('//root/r') AS RECORDS(r)
;WITH cte AS
(
SELECT ROW_NUMBER() OVER (PARTITION BY val ORDER BY val desc) RN
FROM @t
)
DELETE FROM cte
WHERE RN > 1
SET @hcp = (SELECT SPACE(1) + val + ',' from @t WHERE val <> '' FOR XML PATH ('') )
END
现在,当我在参数中传递相同的值时,它给出了不同的结果,任何想法为什么会发生这种情况?
以下是提供不同结果的查询:
DECLARE @hcp NVARCHAR(MAX)
EXEC [Proc_SplitAndRemoveDuplicates] ',', 'Marie-Louise Coenen, Christiaan Genbrugge, Kurt Geldof, Philippe Gilbert, Benoit Calcus, Michel Gijssels, Leon Ghyselinck, Ivan Ghillebert, Peter Chielens, Jozef Gevers, Bernard Gevenois, Paul Geusens, Jozef Geukens, Sabine Goolaerts, Yves Heeren, Val?rie Goffin, Pierro Fattizzo, Philippe Farr, Sophie Schyns, Jean-Pierre Falla, Jean-Michel Gobeaux, Marie-Annick G?tze, Pierre Firket, France Gosse, Johanna Gomez Tercero, Marianne Schoofs, Philippe Schockaert, Lieven De Norre, Val?rie Denef, Kathleen Geens, Walter Geeraerts, Fran?oise Gerard, Claudia Schomus, Ellen Schorkops, Bernard Gerbaux, Eveline Schleich, Micheline Demeyer, Brigitte Gadisseux, Brenda Laera, * GHERALDI, * VAUTHIER, . MARC, *THOMAS* RABERN, * BLANDIN, . BARON, A GUICHARD LEBRUN, A FRANCE VENTRE, A ILAH NOURY, A MARIE BONNERIC BRETON, A PAULE GIMENEZ ROQUEPLOT, A rakoto NIRHY LANTO, A sophie CHOQUET, A marie COURTEL, A MARIE HAOND, A sophie CALLOC H, A marie MAILLOTTE, AABDALLAH JOIDATE, AANYO KUZEAWU, ABBAS AL MAKKI, Abakar ABAKAR MAHAMAT, Ab nasser DOUMI, ABBAS SROUR, ABDAKA BELLOUTI, ABBOUD ABO, Abdallah MOUSTEAU, Marie-Louise Coenen, Christiaan Genbrugge, Kurt Geldof, Philippe Gilbert, Benoit Calcus, Michel Gijssels, Leon Ghyselinck, Ivan Ghillebert, Peter Chielens, Jozef Gevers, Bernard Gevenois, Paul Geusens, Jozef Geukens, Sabine Goolaerts, Yves Heeren, Val?rie Goffin, Pierro Fattizzo, Philippe Farr, Sophie Schyns, Jean-Pierre Falla, Jean-Michel Gobeaux, Marie-Annick G?tze, Pierre Firket, France Gosse, Johanna Gomez Tercero, Marianne Schoofs, Philippe Schockaert, Lieven De Norre, Val?rie Denef, Kathleen Geens, Walter Geeraerts, Fran?oise Gerard, Claudia Schomus, Ellen Schorkops, Bernard Gerbaux, Eveline Schleich, Micheline Demeyer, Brigitte Gadisseux, Brenda Laera, * GHERALDI, * VAUTHIER, . MARC, *THOMAS* RABERN, * BLANDIN, . BARON, A GUICHARD LEBRUN, A FRANCE VENTRE, A ILAH NOURY, A MARIE BONNERIC BRETON, A PAULE GIMENEZ ROQUEPLOT, A rakoto NIRHY LANTO, A sophie CHOQUET, A marie COURTEL, A MARIE HAOND, A sophie CALLOC H, A marie MAILLOTTE, AABDALLAH JOIDATE, AANYO KUZEAWU, ABBAS AL MAKKI, Abakar ABAKAR MAHAMAT, Ab nasser DOUMI, ABBAS SROUR, ABDAKA BELLOUTI, ABBOUD ABO, Abdallah MOUSTEAU, Marie-Louise Coenen, Christiaan Genbrugge, Kurt Geldof, Philippe Gilbert, Benoit Calcus, Michel Gijssels, Leon Ghyselinck, Ivan Ghillebert, Peter Chielens, Jozef Gevers, Bernard Gevenois, Paul Geusens, Jozef Geukens, Sabine Goolaerts, Yves Heeren, Val?rie Goffin, Pierro Fattizzo, Philippe Farr, Sophie Schyns, Jean-Pierre Falla, Jean-Michel Gobeaux, Marie-Annick G?tze, Pierre Firket, France Gosse, Johanna Gomez Tercero, Marianne Schoofs, Philippe Schockaert, Lieven De Norre, Val?rie Denef, Kathleen Geens, Walter Geeraerts, Fran?oise Gerard, Claudia Schomus, Ellen Schorkops, Bernard Gerbaux, Eveline Schleich, Micheline Demeyer, Brigitte Gadisseux, Brenda Laera, * GHERALDI, * VAUTHIER, . MARC, *THOMAS* RABERN, * BLANDIN, . BARON, A GUICHARD LEBRUN, A FRANCE VENTRE, A ILAH NOURY, A MARIE BONNERIC BRETON, A PAULE GIMENEZ ROQUEPLOT, A rakoto NIRHY LANTO, A sophie CHOQUET, A marie COURTEL, A MARIE HAOND, A sophie CALLOC H, A marie MAILLOTTE, AABDALLAH JOIDATE, AANYO KUZEAWU, ABBAS AL MAKKI, Abakar ABAKAR MAHAMAT, Ab nasser DOUMI, ABBAS SROUR, ABDAKA BELLOUTI, ABBOUD ABO, Abdallah MOUSTEAU, Marie-Louise Coenen, Christiaan Genbrugge, Kurt Geldof, Philippe Gilbert, Benoit Calcus, Michel Gijssels, Leon Ghyselinck, Ivan Ghillebert, Peter Chielens, Jozef Gevers, Bernard Gevenois, Paul Geusens, Jozef Geukens, Sabine Goolaerts, Yves Heeren, Val?rie Goffin, Pierro Fattizzo, Philippe Farr, Sophie Schyns, Jean-Pierre Falla, Jean-Michel Gobeaux, Marie-Annick G?tze, Pierre Firket, France Gosse, Johanna Gomez Tercero, Marianne Schoofs, Philippe Schockaert, Lieven De Norre, Val?rie Denef, Kathleen Geens, Walter Geeraerts, Fran?oise Gerard, Claudia Schomus, Ellen Schorkops, Bernard Gerbaux, Eveline Schleich, Micheline Demeyer, Brigitte Gadisseux, Brenda Laera, * GHERALDI, * VAUTHIER, . MARC, *THOMAS* RABERN, * BLANDIN, . BARON, A GUICHARD LEBRUN, A FRANCE VENTRE, A ILAH NOURY, A MARIE BONNERIC BRETON, A PAULE GIMENEZ ROQUEPLOT, A rakoto NIRHY LANTO, A sophie CHOQUET, A marie COURTEL, A MARIE HAOND, A sophie CALLOC H, A marie MAILLOTTE, AABDALLAH JOIDATE, AANYO KUZEAWU, ABBAS AL MAKKI, Abakar ABAKAR MAHAMAT, Ab nasser DOUMI, ABBAS SROUR, ABDAKA BELLOUTI, ABBOUD ABO, Abdallah MOUSTEAU, Marie-Louise Coenen, Christiaan Genbrugge, Kurt Geldof, Philippe Gilbert, Benoit Calcus, Michel Gijssels, Leon Ghyselinck, Ivan Ghillebert, Peter Chielens, Jozef Gevers, Bernard Gevenois, Paul Geusens, Jozef Geukens, Sabine Goolaerts, Yves Heeren, Val?rie Goffin, Pierro Fattizzo, Philippe Farr, Sophie Schyns, Jean-Pierre Falla, Jean-Michel Gobeaux, Marie-Annick G?tze, Pierre Firket, France Gosse, Johanna Gomez Tercero, Marianne Schoofs, Philippe Schockaert, Lieven De Norre, Val?rie Denef, Kathleen Geens, Walter Geeraerts, Fran?oise Gerard, Claudia Schomus, Ellen Schorkops, Bernard Gerbaux, Eveline Schleich, Micheline Demeyer, Brigitte Gadisseux, Brenda Laera, * GHERALDI, * VAUTHIER, . MARC, *THOMAS* RABERN, * BLANDIN, . BARON, A GUICHARD LEBRUN, A FRANCE VENTRE, A ILAH NOURY, A MARIE BONNERIC BRETON, A PAULE GIMENEZ ROQUEPLOT, A rakoto NIRHY LANTO, A sophie CHOQUET, A marie COURTEL, A MARIE HAOND, A sophie CALLOC H, A marie MAILLOTTE, AABDALLAH JOIDATE, AANYO KUZEAWU, ABBAS AL MAKKI, Abakar ABAKAR MAHAMAT, Ab nasser DOUMI, ABBAS SROUR, ABDAKA BELLOUTI, ABBOUD ABO, Abdallah MOUSTEAU, Marie-Louise Coenen, Christiaan Genbrugge, Kurt Geldof, Philippe Gilbert, Benoit Calcus, Michel Gijssels, Leon Ghyselinck, Ivan Ghillebert, Peter Chielens, Jozef Gevers, Bernard Gevenois, Paul Geusens, Jozef Geukens, Sabine Goolaerts, Yves Heeren, Val?rie Goffin, Pierro Fattizzo, Philippe Farr, Sophie Schyns, Jean-Pierre Falla, Jean-Michel Gobeaux, Marie-Annick G?tze, Pierre Firket, France Gosse, Johanna Gomez Tercero, Marianne Schoofs, Philippe Schockaert, Lieven De Norre, Val?rie Denef, Kathleen Geens, Walter Geeraerts, Fran?oise Gerard, Claudia Schomus, Ellen Schorkops, Bernard Gerbaux, Eveline Schleich, Micheline Demeyer, Brigitte Gadisseux, Brenda Laera, * GHERALDI, * VAUTHIER, . MARC, *THOMAS* RABERN, * BLANDIN, . BARON, A GUICHARD LEBRUN, A FRANCE VENTRE, A ILAH NOURY, A MARIE BONNERIC BRETON, A PAULE GIMENEZ ROQUEPLOT, A rakoto NIRHY LANTO, A sophie CHOQUET, A marie COURTEL, A MARIE HAOND, A sophie CALLOC H, A marie MAILLOTTE, AABDALLAH JOIDATE, AANYO KUZEAWU, ABBAS AL MAKKI, Abakar ABAKAR MAHAMAT, Ab nasser DOUMI, ABBAS SROUR, ABDAKA BELLOUTI, ABBOUD ABO, Abdallah MOUSTEAU, Marie-Louise Coenen, Christiaan Genbrugge, Kurt Geldof, Philippe Gilbert, Benoit Calcus, Michel Gijssels, Leon Ghyselinck, Ivan Ghillebert, Peter Chielens, Jozef Gevers, Bernard Gevenois, Paul Geusens, Jozef Geukens, Sabine Goolaerts, Yves Heeren, Val?rie Goffin, Pierro Fattizzo, Philippe Farr, Sophie Schyns, Jean-Pierre Falla, Jean-Michel Gobeaux, Marie-Annick G?tze, Pierre Firket, France Gosse, Johanna Gomez Tercero, Marianne Schoofs, Philippe Schockaert, Lieven De Norre, Val?rie Denef, Kathleen Geens, Walter Geeraerts, Fran?oise Gerard, Claudia Schomus, Ellen Schorkops, Bernard Gerbaux, Eveline Schleich, Micheline Demeyer, Brigitte Gadisseux, Brenda Laera, * GHERALDI, * VAUTHIER, . MARC, *THOMAS* RABERN, * BLANDIN, . BARON, A GUICHARD LEBRUN, A FRANCE VENTRE, A ILAH NOURY, A MARIE BONNERIC BRETON, A PAULE GIMENEZ ROQUEPLOT, A rakoto NIRHY LANTO, A sophie CHOQUET, A marie COURTEL, A MARIE HAOND, A sophie CALLOC H, A marie MAILLOTTE, AABDALLAH JOIDATE, AANYO KUZEAWU, ABBAS AL MAKKI, Abakar ABAKAR MAHAMAT, Ab nasser DOUMI, ABBAS SROUR, ABDAKA BELLOUTI, ABBOUD ABO, Abdallah MOUSTEAU, Marie-Louise Coenen, Christiaan Genbrugge, Kurt Geldof, Philippe Gilbert, Benoit Calcus, Michel Gijssels, Leon Ghyselinck, Ivan Ghillebert, Peter Chielens, Jozef Gevers, Bernard Gevenois, Paul Geusens, Jozef Geukens, Sabine Goolaerts, Yves Heeren, Val?rie Goffin, Pierro Fattizzo, Philippe Farr, Sophie Schyns, Jean-Pierre Falla, Jean-Michel Gobeaux, Marie-Annick G?tze, Pierre Firket, France Gosse, Johanna Gomez Tercero, Marianne Schoofs, Philippe Schockaert, Lieven De Norre, Val?rie Denef, Kathleen Geens, Walter Geeraerts, Fran?oise Gerard, Claudia Schomus, Ellen Schorkops, Bernard Gerbaux, Eveline Schleich, Micheline Demeyer, Brigitte Gadisseux, Brenda Laera, * GHERALDI, * VAUTHIER, . MARC, *THOMAS* RABERN, * BLANDIN, . BARON, A GUICHARD LEBRUN, A FRANCE VENTRE, A ILAH NOURY, A MARIE BONNERIC BRETON, A PAULE GIMENEZ ROQUEPLOT, A rakoto NIRHY LANTO, A sophie CHOQUET, A marie COURTEL, A MARIE HAOND, A sophie CALLOC H, A marie MAILLOTTE, AABDALLAH JOIDATE, AANYO KUZEAWU, ABBAS AL MAKKI, Abakar ABAKAR MAHAMAT, Ab nasser DOUMI, ABBAS SROUR, ABDAKA BELLOUTI, ABBOUD ABO, Abdallah MOUSTEAU, ', @hcp OUTPUT
SELECT @hcp
任何帮助都会非常准确。
答案 0 :(得分:2)
我注意到存储过程中存在两个可疑内容。首先是这一行:
(SELECT ROW_NUMBER() OVER (PARTITION BY val ORDER BY val desc) as RN
在SQL中排序不稳定。这意味着具有相同键值的行可以按任何顺序出现 - 并且该顺序可以从一次运行更改为下一次运行。这意味着要删除的特定行是不确定的(尽管它总是具有正确的val
值)。
其次,您有for xml path
,但没有order by
。如果不同的结果只是“错误”的顺序,您可以通过在该语句中加入order by
来轻松解决这个问题。
这些只是我发现的两个问题。可能还有其他问题。