简体   繁体   English

使用存储过程对表进行分组

[英]Using stored procedures to group a table

UPDATE - I have figured out how to add the stored procedures using code-first migration.更新- 我已经弄清楚如何使用代码优先迁移添加存储过程。 'All' I am wondering now is how I can apply a SELECT * FROM _tempTable1_ UNION SELECT * FROM _tempTable2 , and then output this. '全部'我现在想知道的是如何应用SELECT * FROM _tempTable1_ UNION SELECT * FROM _tempTable2 ,然后是 Z78E6221F6393D1356681DB398F14CE6 I was thinking of implementing a third stored procedure which does this but I am not sure whether this is the right thing to do?我正在考虑实现第三个存储过程,但我不确定这是否是正确的做法? Much appreciated help:)非常感谢帮助:)

I have a table connected to my ASP.NET MVC application, and I have created two stored procedures in SQL Server.我有一个表连接到我的 ASP.NET MVC 应用程序,并且我在 SQL 服务器中创建了两个存储过程。 Is it possible to implement these stored procedures in a way so that when the table is output, it can be grouped into these stored procedures?是否有可能以某种方式实现这些存储过程,以便当表为 output 时,可以将其分组到这些存储过程中?

MetBasicCriteria基本标准

ALTER PROCEDURE [dbo].[MetBasicCriteria]
    -- Add the parameters for the stored procedure here
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT * 
    FROM dbo.Applications
    WHERE NorwegianCitizen=1 
    AND CurrentlyInUni=1
    AND Norwegian=1
    AND English=1
END

MetMoreCriteria满足更多标准

ALTER PROCEDURE [dbo].[MetMoreCriteria] 
    -- Add the parameters for the stored procedure here
    
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    --EXEC [dbo.MetBasicCriteria]

    SELECT * 
    FROM dbo.Applications
    WHERE (Dutch=1
    OR OtherCitizenships='Dutch'
    OR (Course_bachelor='Political Science' OR Course_bachelor='Economy' OR Course_bachelor='International Relations')
    OR (Grade_Bachelor='a' OR Grade_Bachelor='A' OR Grade_Bachelor='b' OR Grade_Bachelor='B'))
    --AND MetBasicCriteria=1
END

In other words --> can I use say 3 different stored procedures, save the outcomes of these 3 into different temporary tables, and the using UNION ALL combine them into 1 temporary table?换句话说-->我可以使用 3 个不同的存储过程,将这 3 个的结果保存到不同的临时表中,然后使用UNION ALL将它们组合成 1 个临时表吗?

Thanks in advance提前致谢

I just take your second procedure code and modify it as a simple POC.我只是将您的第二个程序代码修改为一个简单的 POC。 I have no idea how you intend to merge or alter or combine the resultsets from your first procedure with that of your second procedure.我不知道您打算如何将第一个过程的结果集与第二个过程的结果集合并或更改或组合。 So I leave that for you.所以我把它留给你。 I will also remove the meaningless comments you leave in the code since you seem to be using some sort of template.我还将删除您在代码中留下的无意义的注释,因为您似乎正在使用某种模板。

ALTER PROCEDURE [dbo].[MetMoreCriteria] 
AS
BEGIN
    SET NOCOUNT ON;

    create table #basic (
       id  int not null,
       ...
    );
    Insert #basic (id) --column list to be defined
    EXEC [dbo.MetBasicCriteria];

    SELECT appl.id, ...  
    FROM dbo.Applications as appl
    WHERE (appl.Dutch=1
       OR appl.OtherCitizenships='Dutch'
       OR (appl.Course_bachelor in ('Political Science', 'Economy', 'International Relations'))
       OR (appl.Grade_Bachelor in ('A', 'a', 'B', 'b'))
       )
    AND EXISTS (SELECT * FROM #basic as basic where basic.id = appl.id)
    ORDER BY ...;
END

Hopefully I used the parentheses correctly but I can't test it and this edit widget does not auto-count or correct typos or mistakes.希望我正确使用了括号,但我无法对其进行测试,并且此编辑小部件不会自动计数或更正拼写错误或错误。 Some better coding points:一些更好的编码点:

  • define an alias for each table and use it for every column reference.为每个表定义一个别名并将其用于每个列引用。
  • don't use "*" generally as a column list一般不要使用“*”作为列列表
  • Using IN rather than a series of logical OR comparisons makes your code more readable and less prone to error使用 IN 而不是一系列逻辑 OR 比较使您的代码更具可读性且不易出错

The column list in the SELECT and INSERT statements should be specific - don't use the lazy asterisk to select all columns since you typically don't need them all. SELECT 和 INSERT 语句中的列列表应该是特定的 - 不要对 select 所有列使用惰性星号,因为您通常不需要它们。 If you do need them all, then specify each column to avoid future problems if the schema of the tables changes.如果您确实需要它们,请指定每一列以避免将来在表的架构发生更改时出现问题。 The only place where "*" is acceptable as a column list is inside an EXISTS clause.唯一可以接受“*”作为列列表的地方是在 EXISTS 子句中。

I also added the ORDER BY clause since the consumer of a resultset usually wants those rows ordered.我还添加了 ORDER BY 子句,因为结果集的使用者通常希望这些行排序。 But perhaps your application/consumer does not care?但也许您的应用程序/消费者不在乎? If so, remove it and save some processor cycles.如果是这样,请将其删除并节省一些处理器周期。

Lastly this is just one approach.最后,这只是一种方法。 You could also make your first procedure a view or a UDF since it does nothing complicated.您还可以将您的第一个过程设为视图或 UDF,因为它不会做任何复杂的事情。 And that comment can also apply to this procedure.该评论也适用于该程序。 Perhaps you are just over-complicating a rather simple set of logic?也许您只是将一组相当简单的逻辑过于复杂化了? NB that I omit any error-handling and leave that to you.请注意,我省略了任何错误处理并将其留给您。 And let me re-iterate that this is NOT a recommendation.让我重申这不是建议。 Simply one implementation option.只是一种实现选项。

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

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