选择对动态查询无法正常运行的存储过程进行计数

时间:2019-04-01 13:26:59

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

-我正在尝试执行一个存储过程,该存储过程应该向我返回Count的结果,但是它给了我以下错误(它是葡萄牙语):

2019-04-01 10:13:49.215  WARN 7068 --- [io-8080-exec-10] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 214, SQLState: S0002
2019-04-01 10:13:49.216 ERROR 7068 --- [io-8080-exec-10] o.h.engine.jdbc.spi.SqlExceptionHelper   : O procedimento espera o parâmetro '@statement' do tipo 'ntext/nchar/nvarchar'.

-我不太明白,因为我设置的唯一参数是表名,我将其传递给存储库执行代码。

-这是我的存储过程:

ALTER PROCEDURE [dbo].[usp_verificarTabelaCadMov] (
    @NomeTabela VARCHAR(20)

)   
AS
BEGIN
    SET NOCOUNT ON;

Declare @Comando Varchar(1000)
Declare @Resultado INT;

Set @Comando = 'SELECT @Resultado = COUNT(*) FROM sysobjects WHERE nome = ' + QUOTENAME(@NomeTabela)

exec sp_executesql @Comando , N'@Resultado INT OUTPUT', @Resultado = @Resultado OUTPUT

SELECT @Resultado

END;

GO

-这是我在存储库中的执行情况:

@Query(value = "EXECUTE usp_verificarTabelaCadMov :tabela", nativeQuery = true)
public Integer verificarTabela(@Param("tabela") String tabela);

-在我的控制器中:

                    String tabela = ("M00"+(String.valueOf(Math.round(funcionario.getEmpresa().getCodigo())))+anoInicio+mesAtual);
                    if (eventoEspelhoPontoRepository.verificarTabela(tabela) > 1) {
                        registros.addAll(eventoEspelhoPontoRepository.findAllRegistrosByFuncionarioTableUnica(
                                tabela, dataInicioString, dataFimString, funcionario.getCracha()));
                    }

EDIT1-我要在这里执行的操作是选择一行表,但有时某些表不存在,因此我尝试进行验证以确认该表是否存在以及是否存在,则应执行该表中的Select *,否则应忽略。这是我发现的唯一方法,如果有人有更好的方法,请帮助我。

顺便说一句,这是我上面提到的选择,我尝试使用子句IF EXISTS验证表的存在,但它不起作用,我在主程序中始终收到错误提示“ COULD NOT EXTRACT RESULT SET”。

ALTER PROCEDURE [dbo].[usp_listarRegistrosMov]
    --PARÂMETROS
    @NomeTabela VARCHAR(20),
    @DataInicial VARCHAR(20),
    @DataFinal VARCHAR(20),
    @Cracha FLOAT

AS  

IF EXISTS(SELECT name FROM sysobjects WHERE name = @NomeTabela AND xtype = 'U')

BEGIN

Declare @Comando Varchar(1000)

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

Exec(@Comando)

END



GO

编辑2:我也尝试在同一sp中使用OBJECT_ID:

IF OBJECT_ID(@NomeTabela) IS NOT NULL

但是总是会得到错误:

2019-04-01 11:18:03.147  WARN 7068 --- [nio-8080-exec-8] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: null
2019-04-01 11:18:03.148 ERROR 7068 --- [nio-8080-exec-8] o.h.engine.jdbc.spi.SqlExceptionHelper   : The statement did not return a result set.

2 个答案:

答案 0 :(得分:1)

我会做这样的事情:

ALTER PROCEDURE dbo.usp_verificarTabelaCadMov
(
  @NomeTabela VARCHAR(20)
)
AS
  BEGIN
    SET NOCOUNT ON;

    DECLARE @Comando NVARCHAR(1000);
    DECLARE @Resultado INT;

    SET @Resultado = -1

        -- Or set to NULL depending on how you would like to handle table existence in the application code;
        --SET @Resultado = NULL

    IF OBJECT_ID(QUOTENAME(@NomeTabela), 'U') IS NOT NULL
      BEGIN
        SET @Comando = N'SELECT @Resultado = COUNT(*) FROM ' + QUOTENAME(@NomeTabela);

        EXEC sys.sp_executesql
          @Comando
          , N'@Resultado INT OUTPUT'
          , @Resultado OUTPUT;
      END;

    SELECT @Resultado AS resultado;
  END;

因此,根据您的选择,如何处理表的存在,调用

EXEC dbo.usp_verificarTabelaCadMov @NomeTabela = 'thistabledoesNOTexist';

将返回-1或NULL(请参阅存储的proc注释)和

EXEC dbo.usp_verificarTabelaCadMov @NomeTabela = 'thistableDOESexist';

将返回> -1的值。

答案 1 :(得分:0)

修复后,我不得不:

1-将我的存储过程更改为此:

ALTER PROCEDURE dbo.usp_verificarTabelaCadMov
      @NomeTabela VARCHAR(20)
AS
BEGIN
      SET NOCOUNT ON;

      DECLARE @Exists INT

      IF EXISTS(SELECT name
                        FROM sysobjects
                        WHERE name = @NomeTabela)
      BEGIN
            SET @Exists = 1
      END
      ELSE
      BEGIN
            SET @Exists = 0
      END

      RETURN @Exists
END

2-将我在存储库上的调用更改为此:

@Query(value = "Declare @Exists int "
        + "Exec @Exists = usp_verificarTabelaCadMov :tabela"
        + " Select @Exists", nativeQuery = true)

结果:正在检查表是否存在。