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?
相关问题
- sql execution latency when assign to a variable
- What is the best way to cache a table from a (SQL)
- php PDO::FETCH_ASSOC doesnt detect select after ba
- Bulk update SQL Server C#
- SQL to Parse a Key-Value String
相关文章
- Entity Framework 4.3.1 failing to create (/open) a
- How to truncate seconds in TSQL?
- Code for inserting data into SQL Server database u
- Delete Every Alternate Row in SQL
- Linux based PHP install connecting to MsSQL Server
- SQL Azure Reset autoincrement
- How do we alias a Sql Server instance name used in
- Is recursion good in SQL Server?
this is the best source:
http://www.sommarskog.se/arrays-in-sql.html
create a split function, and use it like:
I prefer the number table approach
For this method to work, you need to do this one time table setup:
Once the Numbers table is set up, create this function:
You can now easily split a CSV string into a table and join on it:
OUTPUT:
Your can pass in a CSV string into a procedure and process only rows for the given IDs:
You can use the solution Joel Spolsky recently gave for this problem.
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: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, andJOIN
against your main table:Also see many other similar questions on SO, including: