SQL For xml path on int columns?

2019-08-08 18:38发布

Can I use FOR XML PATH in sql for int columns ? So that I could use it for something like:

declare @contactIds = (select id from contacts)

and then use it like this:

select * from calls where contactId in (@contactIds)

Is it possible ?

1条回答
女痞
2楼-- · 2019-08-08 18:58

Is this what you want?

select @contactIds = stuff((select ','+cast(id as varchar(8000))
                            from contacts
                            for xml path('')
                           ), 1, 1, '');

You can also use a subquery directly or a table variable:

select *
from calls
where contactId in (select id from contacts);

My guess is that your problem is more complex than the question, so this doesn't really solve the problem.

查看更多
登录 后发表回答