我需要一种方法让@listOfPageIds 被识别为数字列表而不是字符串.我试过强制转换,删除单引号......我真的不想在 sql 中做一个循环.
I need a way to have the @listOfPageIds be recognized as a list of numbers instead of strings. I have tried casting, removing the single quotes... I really don't want to do a loop in sql.
Declare @listOfPageIds varchar(50) ;
Set @listofPageIds = '2, 3, 4, 5, 6, 7, 14, 15';
select * from mytable p where p.PageId in( @listOfPageIds);
嗯,在生产服务器上我会写一些表值函数来拆分列表,但是如果你需要快速的临时查询,这个 xml 技巧可以工作>
Well on production server I'd write some table valued function for splitting lists, but if you need quick ad-hoc query, this xml trick could work
declare @listOfPageIds varchar(50), @data xml
declare @temp table(id int)
select @listofPageIds = '2, 3, 4, 5, 6, 7, 14, 15';
select @data = '<t>' + replace(@listofPageIds, ', ', '</t><t>') + '</t>'
insert into @temp
select
t.c.value('.', 'int') as id
from @data.nodes('t') as t(c)
select * from @temp
sql 小提琴演示
这篇关于在 SQL Server 中将 varchar 列表转换为 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
将每个子标记转换为具有多个分隔符的单列-SQLConverting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(将每个子标记转换为具有多个分隔符的单列-SQ
如何从多个表创建视图?How can I create a view from more than one table?(如何从多个表创建视图?)
根据前一行内的计算值创建计算值Create calculated value based on calculated value inside previous row(根据前一行内的计算值创建计算值)
如何将表格的前两列堆叠成一列,但也仅将第三How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何将表格的前两列堆
递归 t-sql 查询Recursive t-sql query(递归 t-sql 查询)
将月份名称转换为日期/月份编号(问题和答案的组Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(将月份名称转换为日期/月份编号(问题和答案的组合