Usuário com melhor resposta
Criação de Store de Prodecure II

Pergunta
-
Ola bom dia,
Estou tentando fazer um COUNT() em um campo especifico de uma determinada tabela, e para isso segui algumas dicas do nosso colega de Forum o Anderson, porem estou tendo dificuldades, pois a dica sugerida esta apresentando alguns erros que eu tentei corrigir, mas nao tive sucesso, será que alguem poderia me dar uma ajuda, segue abaixo a sugestao do colega Anderson e tb os erros que encontrei ao tentar executar e fazer os testes.
O erro acontece exatamente no momento em que executo a linha "EXECUTE SP_ExecuteSql..........."
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near ')'.
Server: Msg 137, Level 15, State 1, Line 1
Must declare the variable '@Reg'.Grato.
----------------------- dica do Anderson -----------
Boa tarde Linaldo, vc pode executar uma sp_executesql tb, neste caso ficaria assim.Qualquer coisa retorne.
Declare @Comando_SQL nvarchar(max),
@Parametros
nvarchar(max),@Tabela
nvarchar(max),@Nome_Tabela
nvarchar(max),@Campo
nvarchar(max),@Registros
int,@Reg
intSet
@Tabela = N'dbo.TBResultado_Analises'Set
@Campo = N'tubidez'Set
@Parametros = N'@Reg int Output'Set
@Comando_SQL = N'SELECT @Reg = Count(' + @Campo + ') from ' + @TabelaEXECUTE
SP_ExecuteSql @Comando_Sql,@Parametros,@Reg=@Registros Outputselect
@Registros as RegistrosEspero ter ajudado
Respostas
-
Bom dia Linaldo, executei um novo teste no SQL Server 2000, testa e qualquer coisa retorne
CREATE TABLE TAB_TESTE (REGISTRO INT)
GODECLARE @I INT
SET @I = 1WHILE @I <= 100
BEGIN
INSERT INTO TAB_TESTE VALUES(@I)SET @I = @I + 1
END
GODeclare @Comando_SQL nvarchar(4000),
@Parametros nvarchar(4000),
@Tabela nvarchar(4000),
@Nome_Tabela nvarchar(1000),
@Campo nvarchar(1000),
@Registros int,
@Reg int
Set @Tabela = N'TAB_TESTE'Set @Campo = N'Registro'
Set @Parametros = N'@Reg int Output'
Set @Comando_SQL = N'SELECT @Reg = Count(' + @Campo + ') from ' + @Tabela
EXECUTE SP_ExecuteSql @Comando_Sql,@Parametros,@Reg=@Registros Output
select @Registros as Registros
Espero ter ajudado
Todas as Respostas
-
-
Ola Junior,
Na verdade a variavel esta declarada, eu ate tentei fazer algumas mudanças utilizando a mesma filosofia sugerida pelo Anderson, no entanto continuo com o erro ao executar os testes.
Declare @Comando_SQL nvarchar(max),
@Parametros
nvarchar(max),@Tabela
nvarchar(max),@Nome_Tabela
nvarchar(max),@Campo
nvarchar(max),@Registros
int,@Reg
int ************************************************************* Eis aqui a variavel que voce mencionouSet
@Tabela = N'dbo.TBResultado_Analises'Set
@Campo = N'tubidez'Set
@Parametros = N'@Reg int Output'Set
@Comando_SQL = N'SELECT @Reg = Count(' + @Campo + ') from ' + @TabelaEXECUTE
SP_ExecuteSql @Comando_Sql,@Parametros,@Reg=@Registros Outputselect
@Registros as Registros -
-
Bom dia Linaldo, executei um novo teste no SQL Server 2000, testa e qualquer coisa retorne
CREATE TABLE TAB_TESTE (REGISTRO INT)
GODECLARE @I INT
SET @I = 1WHILE @I <= 100
BEGIN
INSERT INTO TAB_TESTE VALUES(@I)SET @I = @I + 1
END
GODeclare @Comando_SQL nvarchar(4000),
@Parametros nvarchar(4000),
@Tabela nvarchar(4000),
@Nome_Tabela nvarchar(1000),
@Campo nvarchar(1000),
@Registros int,
@Reg int
Set @Tabela = N'TAB_TESTE'Set @Campo = N'Registro'
Set @Parametros = N'@Reg int Output'
Set @Comando_SQL = N'SELECT @Reg = Count(' + @Campo + ') from ' + @Tabela
EXECUTE SP_ExecuteSql @Comando_Sql,@Parametros,@Reg=@Registros Output
select @Registros as Registros
Espero ter ajudado
-
pode ser assim ?
Declare @Ret Int
Declare @Comando_SQL nVarchar(1000)
Declare @Parametros nVarchar(1000)Select @Comando_SQL = ' Select @Reg = 1 '
Set @Parametros = N'@Reg int Output'exec SP_ExecuteSql @Comando_Sql, @Parametros , @Reg = @Ret Output
Print @Ret
Abs;
-
-
-
-