如何在 IN SQL 值中传递串联字符串
如何将连接字符串传递到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你知道@NextString中有多少个参数,你可以使用
TRANSPORTEUR IN (@parm1,@parm2,@parm3......)
或者您需要使用 exec 来执行 sql
声明@sql varchar(max)
set @sql='选择地址、AGENCE、AUTRE_REF、
< br>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 + '))'
执行(@sql)
并且您应该将 @NextString 设置为
'''p1'',''p2'',''p3'''
================ =================================================== ===============
2013 年 1 月 11 日更新
创建吐痰函数
,然后使用
join
而不是使用in
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
then using
join
instead of usingin