Foxtable(狐表)用户栏目专家坐堂 → 关于SQL语句中stuff的用法


  共有2137人关注过本帖树形打印复制链接

主题:关于SQL语句中stuff的用法

帅哥哟,离线,有人找我吗?
ap9709130
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:六尾狐 帖子:1467 积分:11418 威望:0 精华:0 注册:2013/11/24 22:10:00
关于SQL语句中stuff的用法  发帖心情 Post By:2019/11/27 22:06:00 [只看该作者]

 老师

我的表结构如下:
            表名  a
              dw (列名)   备注列
             34,56
             78,88
 SQL 语句  se lec t stuff((s el ec t ',' +CAST(dw as varchar) from {a} Order by _Identify For xml path('')),1,1,'')) as dw1 from {a}

        得到结果: 34,56,78,88

     这个结果正好是我想要的 另外一个表b 的_Identify的值   我想要得到  s ele ct 价格 from {b} where _Identify in (34,56,78,88)

    要怎么才能用上第一个语句的结果,来得到 s el e ct 价格 from {b} where _Identify in (34,56,78,88) ?

   
 
    

  

 回到顶部
帅哥,在线噢!
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:106308 积分:540675 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2019/11/27 22:28:00 [只看该作者]

恰恰相反,这种情况不能使用合并,而是拆分

with roy as  
 (select [dw]=cast(left([dw],charindex(',',[dw]+',')-1) as nvarchar(100)),Split=cast(stuff([dw]+',',1,charindex(',',[dw]+','),'') as nvarchar(100)) from [表A]
 union all 
 select [dw]=cast(left(Split,charindex(',',Split)-1) as nvarchar(100)),Split= cast(stuff(Split,1,charindex(',',Split),'') as nvarchar(100)) from Roy where split>'' 
 ) 
 select * from [表B] where [_Identify] in (select [dw] from roy ) option (MAXRECURSION 0) 

 回到顶部