我有一个表DEPT
,其中包含2列 - ID, NAME
。
搜索表单显示DEPT
表中的ID,用户可以选择任意数量的ID并提交表单,以获取相关的NAMEs
。
澄清/输入:
还有其他解决方案吗?
注意:
这个例子很简单,只有1个表 - 在现实生活中,我必须处理超过6个表!
感谢您的任何建议
答案 0 :(得分:8)
CREATE TYPE dbo.DeptList
AS TABLE
(
ID INT
);
GO
CREATE PROCEDURE dbo.RetrieveDepartments
@dept_list AS dbo.DeptList READONLY
AS
BEGIN
SET NOCOUNT ON;
SELECT Name FROM dbo.table1 WHERE ID IN (SELECT ID FROM @dept)
UNION ALL
SELECT Name FROM dbo.table2 WHERE ID IN (SELECT ID FROM @dept)
-- ...
END
GO
现在在您的C#代码中,创建一个DataTable,用ID填充它,并将其传递给存储过程。假设您已经有一个名为tempList的列表,并且ID存储在id:
中DataTable tvp = new DataTable();
tvp.Columns.Add(new DataColumn("ID"));
foreach(var item in tempList)
{
tvp.Rows.Add(item.id);
}
using (connObject)
{
SqlCommand cmd = new SqlCommand("StoredProcedure", connObject);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter tvparam = cmd.Parameters.AddWithValue("@dept_list", tvp);
tvparam.SqlDbType = SqlDbType.Structured;
...
}
您也可以使用分割功能。如果你可以保证输入是安全的(没有<,>,&等),那么这就是我喜欢的那个:
CREATE FUNCTION dbo.SplitInts_XML
(
@List VARCHAR(MAX),
@Delimiter CHAR(1)
)
RETURNS TABLE
AS
RETURN
(
SELECT Item = y.i.value('(./text())[1]', 'int')
FROM
(
SELECT x = CONVERT(XML, '<i>'
+ REPLACE(@List, @Delimiter, '</i><i>') + '</i>').query('.')
) AS a
CROSS APPLY x.nodes('i') AS y(i)
);
GO
现在您的程序可以是:
CREATE PROCEDURE dbo.RetrieveDepartments
@dept_list VARCHAR(MAX)
AS
BEGIN
SET NOCOUNT ON;
;WITH d AS (SELECT ID = Item FROM dbo.SplitInts(@dept_list, ','))
SELECT Name FROM dbo.table1 WHERE ID IN (SELECT ID FROM d)
UNION ALL
SELECT Name FROM dbo.table2 WHERE ID IN (SELECT ID FROM d)
-- ...
END
GO