如何在 IN SQL 值中传递串联字符串

发布于 2024-12-15 17:32:54 字数 1045 浏览 1 评论 0原文

如何将连接字符串传递到 SQL SELECT IN() 中?

DECLARE @NextString NVARCHAR(40)
DECLARE @Pos INT
DECLARE @NextPos INT
DECLARE @String NVARCHAR(40)
DECLARE @Delimiter NVARCHAR(40)

SET @String ='1,2'
SET @Delimiter = ','
SET @String = @String + @Delimiter
SET @Pos = charindex(@Delimiter,@String)

WHILE (@pos <> 0)
BEGIN
SET @NextString = substring(@String,1,@Pos - 1)
SELECT @NextString -- Show Results
SET @String = substring(@String,@pos+1,len(@String))
SET @pos = charindex(@Delimiter,@String)

SELECT ADRESSE, AGENCE, AUTRE_REF, CHAUFFEUR, CODE_CLIENT, CODE_DEST, CODE_MAG, CP, CREE_PAR, DATE_CLOTUR, DATE_CREE, DATE_MODIF, EMAIL, ENLEV_CREMB, ENLEV_DECL, ENLEV_UNITE, FACTURATION, FAX, INSEE, LIVRS_EXPRS, LIVRS_SAMD, LIVRS_SIGN, MODAL_MODE, MODAL_PORT, MODAL_SPEC, MODIF_PAR, NBR_COLIS, NO_ORDRE, OBS, PAYS, POID, POID_COR, REF_EXPED, RS_NOM, SIRET, STATUT_ORDRE, TEL, TRANSPORTEUR, VILLE FROM ORDRE WHERE (STATUT_ORDRE = 2) AND (TRANSPORTEUR IN (@NextString))
END 

我尝试过这个,但并没有完全按照我的预期工作。

预先感谢您, 斯蒂夫

how to pass the concatenation String into SQL SELECT IN () ?

DECLARE @NextString NVARCHAR(40)
DECLARE @Pos INT
DECLARE @NextPos INT
DECLARE @String NVARCHAR(40)
DECLARE @Delimiter NVARCHAR(40)

SET @String ='1,2'
SET @Delimiter = ','
SET @String = @String + @Delimiter
SET @Pos = charindex(@Delimiter,@String)

WHILE (@pos <> 0)
BEGIN
SET @NextString = substring(@String,1,@Pos - 1)
SELECT @NextString -- Show Results
SET @String = substring(@String,@pos+1,len(@String))
SET @pos = charindex(@Delimiter,@String)

SELECT ADRESSE, AGENCE, AUTRE_REF, CHAUFFEUR, CODE_CLIENT, CODE_DEST, CODE_MAG, CP, CREE_PAR, DATE_CLOTUR, DATE_CREE, DATE_MODIF, EMAIL, ENLEV_CREMB, ENLEV_DECL, ENLEV_UNITE, FACTURATION, FAX, INSEE, LIVRS_EXPRS, LIVRS_SAMD, LIVRS_SIGN, MODAL_MODE, MODAL_PORT, MODAL_SPEC, MODIF_PAR, NBR_COLIS, NO_ORDRE, OBS, PAYS, POID, POID_COR, REF_EXPED, RS_NOM, SIRET, STATUT_ORDRE, TEL, TRANSPORTEUR, VILLE FROM ORDRE WHERE (STATUT_ORDRE = 2) AND (TRANSPORTEUR IN (@NextString))
END 

i tried with this, but in not work exactly what i expected.

Thanks you in advance,
Stev

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

‘画卷フ 2024-12-22 17:32:54

如果你知道@NextString中有多少个参数,你可以使用
TRANSPORTEUR IN (@parm1,@parm2,@parm3......)
或者您需要使用 exec 来执行 sql
声明@sql varchar(max)
set @sql='选择地址、AGENCE、AUTRE_REF、
CHAUFFEUR、CODE_CLIENT、CODE_DEST、
CODE_MAG、CP、CREE_PAR、DATE_CLOTUR、
DATE_CREE、DATE_MODIF、电子邮件、
ENLEV_CREMB、ENLEV_DECL、ENLEV_UNITE、FACTURATION,
传真、INSEE、LIVRS_EXPRS、LIVRS_SAMD、LIVRS_SIGN、
MODAL_MODE、MODAL_PORT、MODAL_SPEC、MODIF_PAR、NBR_COLIS、
NO_ORDRE、OBS、PAYS、POID、POID_COR、REF_EXPED、RS_NOM、SIRET、STATUT_ORDRE、电话、TRANSPORTEUR、VILLE FROM ORDRE WHERE (STATUT_ORDRE = 2) AND (TRANSPORTEUR IN (' + @NextString + '))'
< br>
执行(@sql)
并且您应该将 @NextString 设置为 '''p1'',''p2'',''p3'''

================ =================================================== ===============
2013 年 1 月 11 日更新

创建吐痰函数

CREATE FUNCTION [dbo].[udf_Split]    
( @Words nvarchar(MAX)    
, @splitStr varchar(50)    
)    
RETURNS @Result_Table TABLE    
(    
[word] nvarchar(max) NULL    
)    
BEGIN    
Declare @TempStr nvarchar(MAX)    
WHILE (CHARINDEX(@splitStr,@Words)>0)    
BEGIN    
Set @TempStr=SUBSTRING(@Words,1,CHARINDEX(@splitStr,@Words)-1)    
Insert into @Result_Table (word) Values (rtrim(ltrim(@TempStr)))    
Set @Words = REPLACE(@Words,@TempStr+@splitStr,'')    
END    
IF(LEN(RTRIM(LTRIM(@Words)))>0 And CHARINDEX(@splitStr,RTRIM(LTRIM(@Words)))=0)    
Begin    
Set @TempStr=@Words    
Insert into @Result_Table (word) Values (rtrim(ltrim(@TempStr)))    
End    
RETURN    
END 

,然后使用 join 而不是使用 in

SELECT ADRESSE,
AGENCE,
AUTRE_REF,
CHAUFFEUR,
CODE_CLIENT,
CODE_DEST,
CODE_MAG,
CP,
CREE_PAR,
DATE_CLOTUR,
DATE_CREE,
DATE_MODIF,
EMAIL,
ENLEV_CREMB,
ENLEV_DECL,
ENLEV_UNITE,
FACTURATION,
FAX,
INSEE,
LIVRS_EXPRS,
LIVRS_SAMD,
LIVRS_SIGN,
MODAL_MODE,
MODAL_PORT,
MODAL_SPEC,
MODIF_PAR,
NBR_COLIS,
NO_ORDRE,
OBS,
PAYS,
POID,
POID_COR,
REF_EXPED,
RS_NOM,
SIRET,
STATUT_ORDRE,
TEL,
TRANSPORTEUR,
VILLE
FROM ORDRE a
INNER JOIN udf_split(@NextString, ',') b
ON b.word = a.TRANSPORTEUR
WHERE (STATUT_ORDRE = 2)

if you know how many parameters in @NextString, you can use
TRANSPORTEUR IN (@parm1,@parm2,@parm3......)
OR you need to use the exec to execute the sql
declare @sql varchar(max)
set @sql='SELECT ADRESSE, AGENCE, AUTRE_REF,
CHAUFFEUR, CODE_CLIENT, CODE_DEST,
CODE_MAG, CP, CREE_PAR, DATE_CLOTUR,
DATE_CREE, DATE_MODIF, EMAIL,
ENLEV_CREMB, ENLEV_DECL, ENLEV_UNITE, FACTURATION,
FAX, INSEE, LIVRS_EXPRS, LIVRS_SAMD, LIVRS_SIGN,
MODAL_MODE, MODAL_PORT, MODAL_SPEC, MODIF_PAR, NBR_COLIS,
NO_ORDRE, OBS, PAYS, POID, POID_COR, REF_EXPED, RS_NOM, SIRET, STATUT_ORDRE, TEL, TRANSPORTEUR, VILLE FROM ORDRE WHERE (STATUT_ORDRE = 2) AND (TRANSPORTEUR IN (' + @NextString + '))'

exec (@sql)
and you should set the @NextString as '''p1'',''p2'',''p3'''

==================================================================================
update on 11-Jan-2013

create the spiting function

CREATE FUNCTION [dbo].[udf_Split]    
( @Words nvarchar(MAX)    
, @splitStr varchar(50)    
)    
RETURNS @Result_Table TABLE    
(    
[word] nvarchar(max) NULL    
)    
BEGIN    
Declare @TempStr nvarchar(MAX)    
WHILE (CHARINDEX(@splitStr,@Words)>0)    
BEGIN    
Set @TempStr=SUBSTRING(@Words,1,CHARINDEX(@splitStr,@Words)-1)    
Insert into @Result_Table (word) Values (rtrim(ltrim(@TempStr)))    
Set @Words = REPLACE(@Words,@TempStr+@splitStr,'')    
END    
IF(LEN(RTRIM(LTRIM(@Words)))>0 And CHARINDEX(@splitStr,RTRIM(LTRIM(@Words)))=0)    
Begin    
Set @TempStr=@Words    
Insert into @Result_Table (word) Values (rtrim(ltrim(@TempStr)))    
End    
RETURN    
END 

then using join instead of using in

SELECT ADRESSE,
AGENCE,
AUTRE_REF,
CHAUFFEUR,
CODE_CLIENT,
CODE_DEST,
CODE_MAG,
CP,
CREE_PAR,
DATE_CLOTUR,
DATE_CREE,
DATE_MODIF,
EMAIL,
ENLEV_CREMB,
ENLEV_DECL,
ENLEV_UNITE,
FACTURATION,
FAX,
INSEE,
LIVRS_EXPRS,
LIVRS_SAMD,
LIVRS_SIGN,
MODAL_MODE,
MODAL_PORT,
MODAL_SPEC,
MODIF_PAR,
NBR_COLIS,
NO_ORDRE,
OBS,
PAYS,
POID,
POID_COR,
REF_EXPED,
RS_NOM,
SIRET,
STATUT_ORDRE,
TEL,
TRANSPORTEUR,
VILLE
FROM ORDRE a
INNER JOIN udf_split(@NextString, ',') b
ON b.word = a.TRANSPORTEUR
WHERE (STATUT_ORDRE = 2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文