在“选择查询”中创建逗号分隔符

时间:2014-01-27 02:40:55

标签: sql sql-server delimiter for-xml-path

我这里有一个查询,它提供两行结果:

SELECT @SlsChannelDiv= a.division,@SlsChannel= a.department
                    FROM dbo.m_SalesCode a left join dbo.m_SalesCode b 
                    ON a.up1_code=b.sales_code left join vwEmployeeAds v 
                    ON a.user_id=v.login
                    WHERE v.login = @SalesLogin

我想将@SlsChannel参数用于其他查询。

Select * from wo where sales in (@SlsChannel)

但这总是给出第一行结果。

我可以通过提供XML路径来修改我的第一个查询:

SELECT @SlsChannelDiv= a.division,@SlsChannel= a.department
                        FROM dbo.m_SalesCode a left join dbo.m_SalesCode b 
                        ON a.up1_code=b.sales_code left join vwEmployeeAds v 
                        ON a.user_id=v.login
                        WHERE v.login = @SalesLogin For XML PATH ('')

但是,我收到错误消息,例如:The FOR XML clause is not allowed in a ASSIGNMENT statement.

是否可以在@SLSChannel参数中创建逗号分隔的In值?

1 个答案:

答案 0 :(得分:1)

Select * from wo where sales in (SELECT a.department
                FROM dbo.m_SalesCode a left join dbo.m_SalesCode b 
                ON a.up1_code=b.sales_code left join vwEmployeeAds v 
                ON a.user_id=v.login
                WHERE v.login = @SalesLogin)

如此查询呢? :)