I have a situation where I am displaying records on a page, and I need a way for the user to select a subset of those records to be displayed on another page. These records aren't stored anywhere then, it is a dynamically generated thing. I know I could use jquery to pass in a comma-separated value to my other web page, but I'm not sure what is the best way to in sql say where uniqueid is in this list of ids not in a table etc. I know I could dynamically construct the sql with a bunch of ors, but that seems like a hack. anyone else have any suggestions?
我有一种情况,我在页面上显示记录,我需要一种方法让用户选择要在另一页上显示的那些记录的子集。这些记录不存储在任何地方,它是动态生成的东西。我知道我可以使用jquery将逗号分隔的值传递给我的其他网页,但是我不确定在sql中最好的方法是说哪个uniqueid在这个id列表中不在表等中我知道我可以用一堆ors动态构造sql,但这看起来像是一个黑客。还有其他人有什么建议吗?
2 个解决方案
#1
3
this is the best source:
这是最好的来源:
http://www.sommarskog.se/arrays-in-sql.html
create a split function, and use it like:
创建一个split函数,并使用它:
SELECT
*
FROM YourTable y
INNER JOIN dbo.splitFunction(@Parameter) s ON y.ID=s.Value
I prefer the number table approach
我更喜欢数字表方法
For this method to work, you need to do this one time table setup:
要使此方法起作用,您需要执行以下一次性表设置:
SELECT TOP 10000 IDENTITY(int,1,1) AS Number
INTO Numbers
FROM sys.objects s1
CROSS JOIN sys.objects s2
ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number)
Once the Numbers table is set up, create this function:
设置Numbers表后,创建此功能:
CREATE FUNCTION [dbo].[FN_ListToTable]
(
@SplitOn char(1) --REQUIRED, the character to split the @List string on
,@List varchar(8000)--REQUIRED, the list to split apart
)
RETURNS TABLE
AS
RETURN
(
----------------
--SINGLE QUERY-- --this will not return empty rows
----------------
SELECT
ListValue
FROM (SELECT
LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(@SplitOn, List2, number+1)-number - 1))) AS ListValue
FROM (
SELECT @SplitOn + @List + @SplitOn AS List2
) AS dt
INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
WHERE SUBSTRING(List2, number, 1) = @SplitOn
) dt2
WHERE ListValue IS NOT NULL AND ListValue!=''
);
GO
You can now easily split a CSV string into a table and join on it:
您现在可以轻松地将CSV字符串拆分为表格并加入其中:
select * from dbo.FN_ListToTable(',','1,2,3,,,4,5,6777,,,')
OUTPUT:
ListValue
-----------------------
1
2
3
4
5
6777
(6 row(s) affected)
Your can pass in a CSV string into a procedure and process only rows for the given IDs:
您可以将CSV字符串传递到过程中,并仅处理给定ID的行:
SELECT
y.*
FROM YourTable y
INNER JOIN dbo.FN_ListToTable(',',@GivenCSV) s ON y.ID=s.ListValue
#2
1
You can use the solution Joel Spolsky recently gave for this problem.
你可以使用Joel Spolsky最近解决这个问题的解决方案。
SELECT * FROM MyTable
WHERE ',' + 'comma,separated,list,of,words' + ','
LIKE '%,' + MyTable.word + ',%';
That solution is clever but slow. The better solution is to split the comma-separated string, and construct a dynamic SQL query with the IN()
predicate, adding a query parameter placeholder for each element in your list of values:
这个解决方案很聪明但很慢。更好的解决方案是拆分以逗号分隔的字符串,并使用IN()谓词构造动态SQL查询,为值列表中的每个元素添加查询参数占位符:
SELECT * FROM MyTable
WHERE word IN ( ?, ?, ?, ?, ?, ?, ?, ? );
The number of placeholders is what you have to determine when you split your comma-separated string. Then pass one value from that list per parameter.
分割逗号分隔的字符串时,必须确定占位符的数量。然后从每个参数的列表中传递一个值。
If you have too many values in the list and making a long IN()
predicate is unwieldy, then insert the values to a temporary table, and JOIN
against your main table:
如果列表中包含太多值并且使得长IN()谓词不实用,则将值插入临时表,并对主表进行JOIN:
CREATE TEMPORARY TABLE TempTableForSplitValues (word VARCHAR(20));
...split your comma-separated list and INSERT each value to a separate row...
SELECT * FROM MyTable JOIN TempTableForSplitValues USING (word);
Also see many other similar questions on SO, including:
另请参阅关于SO的许多其他类似问题,包括:
- Dynamic SQL Comma Delimited Value Query
- Passing a varchar full of comma delimited values to a SQL Server IN function
- Parameterized Queries with Like and In
- Parameterizing a SQL IN clause?
动态SQL逗号分隔值查询
将一个充满逗号分隔值的varchar传递给SQL Server IN函数
具有Like和In的参数化查询
参数化SQL IN子句?
#1
3
this is the best source:
这是最好的来源:
http://www.sommarskog.se/arrays-in-sql.html
create a split function, and use it like:
创建一个split函数,并使用它:
SELECT
*
FROM YourTable y
INNER JOIN dbo.splitFunction(@Parameter) s ON y.ID=s.Value
I prefer the number table approach
我更喜欢数字表方法
For this method to work, you need to do this one time table setup:
要使此方法起作用,您需要执行以下一次性表设置:
SELECT TOP 10000 IDENTITY(int,1,1) AS Number
INTO Numbers
FROM sys.objects s1
CROSS JOIN sys.objects s2
ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number)
Once the Numbers table is set up, create this function:
设置Numbers表后,创建此功能:
CREATE FUNCTION [dbo].[FN_ListToTable]
(
@SplitOn char(1) --REQUIRED, the character to split the @List string on
,@List varchar(8000)--REQUIRED, the list to split apart
)
RETURNS TABLE
AS
RETURN
(
----------------
--SINGLE QUERY-- --this will not return empty rows
----------------
SELECT
ListValue
FROM (SELECT
LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(@SplitOn, List2, number+1)-number - 1))) AS ListValue
FROM (
SELECT @SplitOn + @List + @SplitOn AS List2
) AS dt
INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
WHERE SUBSTRING(List2, number, 1) = @SplitOn
) dt2
WHERE ListValue IS NOT NULL AND ListValue!=''
);
GO
You can now easily split a CSV string into a table and join on it:
您现在可以轻松地将CSV字符串拆分为表格并加入其中:
select * from dbo.FN_ListToTable(',','1,2,3,,,4,5,6777,,,')
OUTPUT:
ListValue
-----------------------
1
2
3
4
5
6777
(6 row(s) affected)
Your can pass in a CSV string into a procedure and process only rows for the given IDs:
您可以将CSV字符串传递到过程中,并仅处理给定ID的行:
SELECT
y.*
FROM YourTable y
INNER JOIN dbo.FN_ListToTable(',',@GivenCSV) s ON y.ID=s.ListValue
#2
1
You can use the solution Joel Spolsky recently gave for this problem.
你可以使用Joel Spolsky最近解决这个问题的解决方案。
SELECT * FROM MyTable
WHERE ',' + 'comma,separated,list,of,words' + ','
LIKE '%,' + MyTable.word + ',%';
That solution is clever but slow. The better solution is to split the comma-separated string, and construct a dynamic SQL query with the IN()
predicate, adding a query parameter placeholder for each element in your list of values:
这个解决方案很聪明但很慢。更好的解决方案是拆分以逗号分隔的字符串,并使用IN()谓词构造动态SQL查询,为值列表中的每个元素添加查询参数占位符:
SELECT * FROM MyTable
WHERE word IN ( ?, ?, ?, ?, ?, ?, ?, ? );
The number of placeholders is what you have to determine when you split your comma-separated string. Then pass one value from that list per parameter.
分割逗号分隔的字符串时,必须确定占位符的数量。然后从每个参数的列表中传递一个值。
If you have too many values in the list and making a long IN()
predicate is unwieldy, then insert the values to a temporary table, and JOIN
against your main table:
如果列表中包含太多值并且使得长IN()谓词不实用,则将值插入临时表,并对主表进行JOIN:
CREATE TEMPORARY TABLE TempTableForSplitValues (word VARCHAR(20));
...split your comma-separated list and INSERT each value to a separate row...
SELECT * FROM MyTable JOIN TempTableForSplitValues USING (word);
Also see many other similar questions on SO, including:
另请参阅关于SO的许多其他类似问题,包括:
- Dynamic SQL Comma Delimited Value Query
- Passing a varchar full of comma delimited values to a SQL Server IN function
- Parameterized Queries with Like and In
- Parameterizing a SQL IN clause?
动态SQL逗号分隔值查询
将一个充满逗号分隔值的varchar传递给SQL Server IN函数
具有Like和In的参数化查询
参数化SQL IN子句?