存储过程不会带来结果

时间:2019-03-28 19:39:08

标签: java sql sql-server spring stored-procedures

  • 我的存储过程未如预期那样带来结果

  • 所有参数都设置为OK,就像我的表名一样(根据年/月而变化)。

  • 当我在Management Studio上手动执行查询时,结果就出现了,但是当我在Controller上调用过程时,结果却没有。

  • PS:直接执行的代码与其他日期一起使用,但是在IDE中使用已知日期执行的代码也应该带来结果,我尝试使用不同的日期,问题似乎出在存储过程查询。

CONSOLE + CODE PICTURE WITH THE VOID LIST

PICTURE OF THE QUERY WORKING ON MANAGEMENT STUDIO


存储的过程代码:

library(shiny)

ui = shinyUI(fluidPage(

  titlePanel("Defining time periods"),

  sidebarLayout(
    sidebarPanel(
      numericInput("num_periodsnr", label = "Desired number of time periods?",
                   min = 1, max = 10, value = 2),
      uiOutput("period_cutpoints"),
      actionButton("submit", "Submit")
    ),
    mainPanel(
      textOutput("nr_of_periods"),
      textOutput("cutpoints")
    )
  )
))

server = shinyServer(function(input, output, session) {

  library(lubridate)

  output$nr_of_periods <- renderPrint(input$num_periodsnr)

  dates <- seq(ymd('2016-01-02'), ymd('2017-12-31'), by = '1 week')

  output$period_cutpoints<-renderUI({
    req(input$num_periodsnr)
    lapply(1:(input$num_periodsnr-1), function(i) {
      selectInput(inputId=paste0("cutpoint",i), 
                  label=paste0("Select cutpoint for Time Period ", i, ":"),
                  choices=dates)
    })
  })

  seldates <- reactiveValues(x=NULL)
  observeEvent(input$submit, {
    seldates$x <- list()
    lapply(1:(input$num_periodsnr-1), function(i) { 
      seldates$x[[i]] <- input[[paste0("cutpoint", i)]]
    })
  })

  output$cutpoints <- renderText({as.character(seldates$x)})
})

shinyApp(ui = ui, server = server)

控制器代码:

ALTER PROCEDURE [dbo].[usp_listarRegistrosMov]
    --PARÂMETROS
    @NomeTabela VARCHAR(20),
    @DataInicial VARCHAR(20),
    @DataFinal VARCHAR(20),
    @Cracha FLOAT
AS
BEGIN
  Declare @Comando Varchar(1000)

  Set @Comando = 'SELECT * FROM ' + @NomeTabela + ' WHERE mov_data BETWEEN ' + @DataInicial + ' AND ' + @DataFinal + ' AND mov_cracha = ' + CAST(@Cracha AS VARCHAR(20))

  Exec(@Comando)
END
GO

存储库代码:

    public void consultar() {
        LocalDate dataInicio = dataInicial.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate dataFim = dataFinal.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        String mesInicio = String.valueOf(dataInicio.getMonthValue());
        String mesFim = String.valueOf(dataFim.getMonthValue());
        String anoInicio = (String.valueOf(dataInicio.getYear())).substring(2,4);
        String anoFim = (String.valueOf(dataFim.getYear())).substring(2,4);
        String empresaString = String.valueOf(Math.round(funcionario.getEmpresa().getCodigo()));
        long mesesDiferencaGlob = ChronoUnit.MONTHS.between(dataInicio, dataFim) + 1;

        if (dataInicio.isAfter(dataFim)) {
            Messages.addGlobalError("Informe uma data inicial posterior à data final");
            return;
        }

        if (dataInicio.getMonthValue() != dataFim.getMonthValue()) {
            if (dataInicio.getYear() == dataFim.getYear()) {
                do {
                    System.out.println(mesesDiferencaGlob);
                    String tabela = ("M00"+(String.valueOf(Math.round(funcionario.getEmpresa().getCodigo())))+anoInicio+"0"+mesInicio);
                    System.out.println(tabela);
                    DateTimeFormatter formatadorInicio = DateTimeFormatter.ofPattern("dd/MM/yyyy");
                    String dataInicioString = dataInicio.format(formatadorInicio);
                    String dataFimString = dataFim.format(formatadorInicio);
                    System.out.println(dataInicioString + dataFimString);
                    setRegistrosTemp(eventoEspelhoPontoRepository.findAllRegistrosByFuncionarioTableUnica(tabela, dataInicioString, dataFimString, funcionario.getCracha()));
                    for (EventoEspelhoPonto item : registrosTemp) {
                        registros.add(item);                        
                    }
                    int mesInicioInt = Integer.parseInt(mesInicio) + 1;
                    mesInicio = Integer.toString(mesInicioInt);
                    mesesDiferencaGlob--;
                } while (mesesDiferencaGlob != 0);      
            }   
        }
}

1 个答案:

答案 0 :(得分:0)

在您的存储过程中,因为您正在使用字符串作为动态sql字符串中的日期,因此需要在其周围添加2个单引号,以便执行的字符串将其括在单引号中。

换句话说,更改此内容:

Set @Comando = 'SELECT * FROM ' + @NomeTabela + 
               ' WHERE mov_data BETWEEN ' + @DataInicial + ' AND ' + @DataFinal + ' AND mov_cracha = ' + CAST(@Cracha AS VARCHAR(20))

对此:

Set @Comando = 'SELECT * FROM ' + @NomeTabela + 
               ' WHERE mov_data BETWEEN ''' + @DataInicial + ''' AND ''' + @DataFinal + ''' AND mov_cracha = ' + CAST(@Cracha AS VARCHAR(20))

我不保证这是您唯一的问题,因为我不会讲Java,但这绝对是您的存储过程中的问题。