Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Sql Server中将varchar列表转换为int_Sql_Sql Server_Sql Server 2008_Tsql_Ssms - Fatal编程技术网

在Sql Server中将varchar列表转换为int

在Sql Server中将varchar列表转换为int,sql,sql-server,sql-server-2008,tsql,ssms,Sql,Sql Server,Sql Server 2008,Tsql,Ssms,我需要一种方法将@ListofPageId识别为数字列表而不是字符串。我试过铸造,删除单引号。。。我真的不想在sql中执行循环 Declare @listOfPageIds varchar(50) ; Set @listofPageIds = '2, 3, 4, 5, 6, 7, 14, 15'; select * from mytable p where p.PageId in( @listOfPageIds); 声明@YourTable 动态SQL解决方案 递归CTE解 声明@Your

我需要一种方法将@ListofPageId识别为数字列表而不是字符串。我试过铸造,删除单引号。。。我真的不想在sql中执行循环

Declare @listOfPageIds varchar(50) ;

Set @listofPageIds = '2, 3, 4, 5, 6, 7, 14, 15';

select * from mytable p where p.PageId in( @listOfPageIds);
声明@YourTable 动态SQL解决方案 递归CTE解 声明@YourTable 动态SQL解决方案 递归CTE解
在生产服务器上,我会编写一些用于拆分列表的表值函数,但如果您需要快速的即席查询,这种xml技巧可能会奏效

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
declare@listOfPageIds varchar(50),@data-xml
声明@temp表(id int)
选择@ListofPageId='2,3,4,5,6,7,14,15';
选择@data=''+replace(@listofpageid',','','')+''
插入到@temp中
选择
t、 c.作为id的值('.',int')
从@data.nodes('t')到t(c)
从@temp中选择*

在生产服务器上,我会编写一些用于拆分列表的表值函数,但如果您需要快速的即席查询,这种xml技巧可能会奏效

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
declare@listOfPageIds varchar(50),@data-xml
声明@temp表(id int)
选择@ListofPageId='2,3,4,5,6,7,14,15';
选择@data=''+replace(@listofpageid',','','')+''
插入到@temp中
选择
t、 c.作为id的值('.',int')
从@data.nodes('t')到t(c)
从@temp中选择*


您不能使用这样的变量。您必须使用拆分器解析列表,或者使用动态sql。是否可以使用用户定义的表类型而不是csv值列表?无论如何,列表都是字符串。如果您需要这些作为数字,则必须将它们拆分为单独的行。您在TSQL中搜索过与分隔CSV(逗号分隔值)相关的内容吗?我搜索过。。。我不想在分隔符上拆分,但显然我必须这样做。你不能使用这样的变量。您必须使用拆分器解析列表,或者使用动态sql。是否可以使用用户定义的表类型而不是csv值列表?无论如何,列表都是字符串。如果您需要这些作为数字,则必须将它们拆分为单独的行。您在TSQL中搜索过与分隔CSV(逗号分隔值)相关的内容吗?我搜索过。。。我不想在定界符上出现分歧,但显然我必须这样做。如果选民们对这个答案的不足之处发表评论,这可能会有所帮助,因为它值得投反对票。我不知道。OP说他不想循环,但递归解决方案不是循环。所以我不知道。实际上,像这样用于计数的递归cte和幕后的循环是一样的。此外,您发布的解决方案不会返回OP正在查找的数据,这可能也没有什么帮助。我最终不得不对这两个答案使用类似的解决方案,并且必须循环并作为表返回的格式才是正确的答案。我不知道,如果没有这种递归,它是不可能简单地完成的两个答案都很好。如果选民们评论这个答案的不足之处,认为它值得投反对票,这可能会有所帮助。我不知道。OP说他不想循环,但递归解决方案不是循环。所以我不知道。实际上,像这样用于计数的递归cte和幕后的循环是一样的。此外,您发布的解决方案不会返回OP正在查找的数据,这可能也没有什么帮助。我最终不得不对这两个答案使用类似的解决方案,并且必须循环并作为表返回的格式才是正确的答案。我不知道,如果没有这种递归,它是不可能简单地完成的这两个答案都很好。只是想补充一点,如果您的值列表中的每个逗号后面都没有空格,比如
select@listofPageIds='2,3,4,5,6,7,14,15'您将收到一个sql转换错误。下面将修复
SET@listofPageIds=replace(@listofPageIds,,,'')
SELECT@data=''+replace(@listofPageIds,,,'')+''
只是想添加一点,如果您的值列表在每个逗号后面不包含空格,就像
SELECT@listofPageIds='2,3,4,5,6,7,14,15'您将收到一个sql转换错误。以下内容将修复
SET@listofPageIds=replace(@listofPageIds,,'')
SELECT@data=''+replace(@listofPageIds,,'','')+''
DECLARE @listOfPageIds nvarchar(255);
SET @listOfPageIds = '2, 3, 4, 5, 6, 7, 14, 15'
SET @listOfPageIds = REPLACE(@listOfPageIds,' ','') + ',';  -- Put the end comma there instead of having to use a case statement in my query
                                                            -- As well as getting rid of useless white space with REPLACE()

WITH CTE
AS
(
    SELECT 1 row_count, CAST(SUBSTRING(@listOfPageIds,0,CHARINDEX(N',',@listOfPageIds,0)) AS NVARCHAR(255)) AS search_val, CHARINDEX(',',@listOfPageIds,0) + 1 AS starting_position
    UNION ALL
    SELECT row_count + 1,CAST(SUBSTRING(@listOfPageIds,starting_position,CHARINDEX(',',@listOfPageIds,starting_position) - starting_position) AS NVARCHAR(255)) AS search_val, CHARINDEX(',',@listOfPageIds,starting_position) + 1 AS starting_position
    FROM CTE
    WHERE row_count < (LEN(@listOfPageIds) - LEN(REPLACE(@listOfPageIds,',','')))
)

SELECT *
FROM @yourTable
WHERE col1 IN (SELECT CAST(search_val AS INT) FROM CTE)
col1
-----------
2
3
4
5
6
7
14
15
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