存储过程根据ID返回不同的数据

时间:2019-01-03 23:01:56

标签: sql-server stored-procedures

如果我需要在不是更新插入或删除操作的存储过程中执行其他操作,则一直在使用此方法。通常,如果我必须根据个人角色选择显示不同的信息。有人告诉我这是非常糟糕的做法,而且性能很差,但是我不知道如何在不必为每个命令编写存储过程的情况下执行此操作,而且由于我有太多命令,因此看起来更糟。我的问题是,在SQL Server中是否有更好的方法来执行此类操作?

DROP PROCEDURE IF EXISTS spRequisicao
GO

CREATE PROCEDURE spRequisicao
    -- hRequisicao
    @Action NVARCHAR(20),
    @MotivoCriacao NVARCHAR(100) = NULL,
    @IdCodeRequest INT = NULL,
    @Projecto NVARCHAR(50) = NULL,
    @Desenho NVARCHAR(50) = NULL,
    @Indice NVARCHAR(20) = NULL,
    @CadenciaMensal INT = NULL,
    @NumCOMDEV NVARCHAR(50) = NULL,
    @RefCliente NVARCHAR(50) = NULL,
    @RefInterna NVARCHAR(7) = NULL,
    @QTDLancamentoFormas INT = NULL,
    @CapacidadeReal NVARCHAR(50) = NULL,
    @Peso FLOAT = NULL,
    @IdCaixa INT = NULL,
    @IdComp INT = NULL,
    @IdMetProd INT = NULL,
    @IdColaborador INT = NULL,
    @IdStatus INT = NULL,
    @IdInfoLogistica bit = NULL,

    -- hDataLog
    @IdData INT = NULL,
    @DataAbertura DATETIME = NULL,
    @DataAlteracao DATETIME = NULL,
    @IdReq INT = NULL,

    -- hCaixa
    @TipoCaixa NVARCHAR(50) = NULL,
    @QTDPecasCaixa INT = NULL,
    @UnidadeProducao NVARCHAR(20) = NULL,
    @CelulaProducao NVARCHAR(50) = NULL,    
    @NumKanbansProducao INT = NULL, 

    -- hMetodologiaProducao
    @TipoMetodologia NCHAR(3) = NULL,
    @QTDMetProd INT = NULL,

    -- hComponentes
    @QTDComp INT = NULL,
AS
BEGIN
    SET NOCOUNT ON;

    IF @Action = 'SELECTALL'
    BEGIN
        SELECT 
            hr.IdColaborador, hr.MotivoCriacao, hr.IdReq, 
            Nome, hcb.Email, hr.RefInterna, hi.Descricao AS InfoLog, 
            hs.Descricao AS STATUS, hd.DataAbertura AS DataCriacao 
        FROM 
            hRequisicao AS hr   
        JOIN 
            hColaborador AS hcb ON hcb.IdColaborador = hr.IdColaborador
        JOIN 
            hStatus AS hs ON hs.IdStatus = hr.IdStatus
        JOIN 
            hInfoLogistica hi ON hi.IdInfoLogistica = hr.IdInfoLogistica                
        JOIN 
            hDataLog hd ON hd.IdReq = hr.IdReq
        WHERE 
            hr.IdStatus != 6
    END

    IF @Action = 'SELECTALL_USERID'
    BEGIN
        SELECT 
            hr.IdColaborador, hr.IdReq, Nome, hcb.Email, hr.RefInterna, 
            hs.Descricao AS STATUS, hl.DataAbertura AS DataCriacao, hi.Descricao AS InfoLog  
        FROM 
            hRequisicao AS hr   
        JOIN 
            hColaborador AS hcb ON hcb.IdColaborador = hr.IdColaborador
        JOIN 
            hStatus AS hs ON hs.IdStatus = hr.IdStatus      
        JOIN 
            hDataLog AS hl ON hl.IdReq = hr.IdReq
        JOIN 
            hInfoLogistica AS hi ON hi.IdInfoLogistica = hr.IdInfoLogistica
        WHERE
            hr.IdColaborador = @IdColaborador AND hr.IdStatus != 6
    END 

    IF @Action = 'SELECT'
    BEGIN
        SELECT 
            IdReq, hr.MotivoCriacao, hr.IdCodeRequest, hr.IdColaborador, 
            Nome, hcb.Email, Projecto, Desenho, Indice, CadenciaMensal, 
            NumCOMDEV, RefCliente, RefInterna, QTDLancamentoFormas, 
            CapacidadeReal, Peso, TipoCaixa, QTDPecasCaixa, UnidadeProducao, 
            CelulaProducao, NumKanbansProducao, QTDComp, 
            TipoMetodologia, QTDMetProd, hi.Descricao AS InfoLog 
        FROM 
            hRequisicao AS hr
        JOIN 
            hCodeRequest AS hcr ON hr.IdCodeRequest = hcr.IdCodeRequest
        JOIN 
            hCaixa AS hc ON  hr.IdCaixa = hc.IdCaixa
        JOIN 
            hComponentes AS hcp ON hr.IdComp = hcp.IdComp
        JOIN 
            hMetodologiaProducao AS hmp ON hr.IdMetProd = hmp.IdMetProd
        JOIN 
            hColaborador AS hcb ON hcb.IdColaborador = hr.IdColaborador
        JOIN 
            hInfoLogistica AS hi ON hi.IdInfoLogistica = hr.IdInfoLogistica
        WHERE
            IdReq = @IdReq
    END 

    IF @Action = 'CHECKREQ'
    BEGIN
        SELECT IdReq, IdStatus 
        FROM hRequisicao
        WHERE IdColaborador = @IdColaborador AND IdStatus = 6
    END

    IF @Action = 'CHECKSTATUS'
    BEGIN
        SELECT Descricao 
        FROM hStatus hs
        JOIN hRequisicao hr ON hr.IdStatus = hs.IdStatus
        WHERE IdReq = @IdReq
    END
END

1 个答案:

答案 0 :(得分:1)

我被教导要理解,任何数据安排(收集,选择,定位等)本质上都是与行为安排无关的信息。

以上内容的相关性在于,各种查询所代表的列和关系可能对理解数据库/应用程序至关重要。

这意味着您应该对它们进行适当的评论和记录,也可能要分别对其进行注释。

我通常更喜欢以尽可能的方式设计数据库,以使将来的开发人员等所需的描述量最少。

为此,我发现基于查询(例如您的示例中使用的查询)创建视图,对其进行适当命名,以及为适合的条件(非常直观且易于维护)命名适当的结果列和列。

就性能而言,尽管我认为它比proc VS视图更受数据库设计的影响,但我不能自信地建议或告知任何一种方式。

结论:没有比更广泛的上下文设计策略和要求更独立的方法来完成此任务