简体   繁体   English

用于选择多个记录的存储过程

[英]Stored procedure for selecting multiple records

I need to select records say 2000 from a table with a matching timestamp from c# .net code. 我需要从c#.net代码中具有匹配时间戳的表中选择记录说2000。 Eg: 例如:

SELECT * 
FROM ITEMDATA_TABLE 
WHERE ITEMNAME='Item1' and TimeStamp='2010-04-26 17:15:05.667'

The above query has to execute for 2000 items more for the same timestamp. 上面的查询必须为相同的时间戳执行2000个项目。

for this we can use 为此,我们可以使用

SELECT * 
FROM ITEMDATA_TABLE 
WHERE ITEMNAME in ('Item1','Item2','Item3','Item4',......) 
  AND TimeStamp='2010-04-26 17:15:05.667'

from SQL Server Management Studio. 从SQL Server Management Studio。

I have tried appending all Item names to one string variable and giving it as a parameter to the stored procedure,but it resulted into a wrong concatenation. 我尝试将所有项目名称附加到一个字符串变量中,并将其作为存储过程的参数,但是它导致了错误的串联。

How can I do this as a stored procedure executing from the .net code? 如何作为从.net代码执行的存储过程来做到这一点? Can anyone suggest me/correct me in doing this? 有人可以建议我/纠正我吗?

If you're on SQL Server 2008 (you didn't mention your version), you can use table-valued parameters . 如果您使用的是SQL Server 2008(未提及您的版本),则可以使用表值参数

If you're on SQL Server 2005 or below, check out Erland Sommarskog's excellent articles on how to deal with lists of parameters in stored procedures. 如果您使用的是SQL Server 2005或更低版本,请查看Erland Sommarskog的出色文章,内容涉及如何处理存储过程中的参数列表。

You cannot just pass those in as a string - that does not work. 您不能仅将它们作为字符串传递-这是行不通的。

If doing it from a stored procedure, I would create a temp table of all valid item names, insert into that (ie: just a single table), and do a query against that as the primary, joined to the master where IDs / DateTime qualified. 如果从存储过程中进行操作,我将创建一个包含所有有效项目名称的临时表,将其插入(即:仅一个表),并以该表作为主表进行查询,并连接到主表中,其中ID / DateTime合格。 Then delete the temp table. 然后删除临时表。

Following may be the code you are looking for : 以下可能是您正在寻找的代码:

SET @SQLString = N'SELECT *
    FROM table1
    WHERE timet = @time and items in (@item)';


DECLARE @SQLString nvarchar(500);
DECLARE @ParmDefinition nvarchar(500);

SET @ParmDefinition = N'@time timestamp,
    @item varchar(max) ';


EXECUTE sp_executesql
    @SQLString
    ,@ParmDefinition
    ,@time = '2010-04-26 17:15:05.667'
    ,@item = '''Item1'',''Item2'',''Item3'',''Item4'''
    ;

不会告诉您以其他方式执行此操作,但是如果您选择了要查找的时间戳的所有项目,然后在c#代码中迭代此记录集,这会更容易吗?

In SQL 2005 you can pass the list of item as XML parameter to the stored procedure and then write a query against the xml parameter. 在SQL 2005中,您可以将项目列表作为XML参数传递给存储过程,然后针对xml参数编写查询。 something like this 像这样的东西

SELECT ItemData_Table.* FROM ItemData_Table CROSS APPLY MyxmlParameter.nodes('item') data(i) WHERE ItemData_Table.ItemId = i.value('@itemid', INT) SELECT ItemData_Table。* FROM ItemData_Table交叉应用MyxmlParameter.nodes('item')data(i)WHERE ItemData_Table.ItemId = i.value('@ itemid',INT)

Here MyxmlParameter is the XML you passed from you .NET code to the stored procedure. MyxmlParameter是您从.NET代码传递到存储过程的XML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM