Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Incorrect syntax near 'TableName'

Author  Topic 

Marcos Cruz
Starting Member

8 Posts

Posted - 2009-12-02 : 14:37:29
Hello everybody!

Please, Incorrect syntax near 'Empresas' is a SQL error that I get when I try to save this stored

procedure. I want just return all records doing the pagination on the server. Probabibly there is a stupid error, but I

can´t see where.

Someone can give me a litle help?

Thanks

Marcos Cruz, São Paulo, Brazil


ALTER PROCEDURE dbo.LstRecordsPagined
@firstRecord int = 0
,@lastRecord int = 0

AS

SELECT * FROM (
SELECT ROW_NUMBER() OVER(ORDER BY EmpresaContatos.NomeContato DESC) As row
Empresas.EmpresaId
,Empresas.RazaoSocialEmp
,Empresas.NomeFantasiaEmp
,Empresas.CnpjEmp
,Empresas.InscEstadualEmp
,Empresas.InscMunicipalEmp
,Empresas.DataCadastroEmp
,Empresas.TipoCapitalEmp
,Empresas.PaisOrigemEmp
,Empresas.TipoEmpresa
,Empresas.TipoGestaoEmp
,Empresas.SetorEconomiaEmp
,Empresas.SegmentoEconomiaEmp
,Empresas.FuncionariosEmp
,Empresas.StatusEmp
,EmpresaContatos.NomeContato
FROM
Empresas LEFT OUTER JOIN EmpresaContatos
ON
Empresas.EmpresaId = EmpresaContatos.EmpresaId
WHERE
(EmpresaContatos.NomeContato LIKE '%cruz%')
) AS tbl
WHERE
row > + CONVERT(varchar(9), @firstRecord) AND
row < + CONVERT(varchar(9), @lastRecord)


/* SET NOCOUNT ON */
RETURN

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-12-02 : 14:42:45
should that be row >= @FirstRecord and row <= @LastRecord? row is an integer, so no need to convert the paramters

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-12-02 : 14:53:43
Also..the error point to a missing comma between the first two fields...

SELECT ROW_NUMBER() OVER(ORDER BY EmpresaContatos.NomeContato DESC) As row
,Empresas.EmpresaId
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-03 : 03:14:49
Also, why are you converting the variables to varchar?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Marcos Cruz
Starting Member

8 Posts

Posted - 2009-12-03 : 05:46:55
quote:
Originally posted by madhivanan

Also, why are you converting the variables to varchar?

Madhivanan

Failing to plan is Planning to fail



Hi Madhivanan,

Thanks for your help. I´d like prefer work without converting, using int field, but if I do not convert cause a error when I try save the procedure.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-03 : 07:21:52
Have you tries this?

ALTER PROCEDURE dbo.LstRecordsPagined
@firstRecord int = 0
,@lastRecord int = 0

AS

SELECT * FROM (
SELECT ROW_NUMBER() OVER(ORDER BY EmpresaContatos.NomeContato DESC) As row
,Empresas.EmpresaId
,Empresas.RazaoSocialEmp
,Empresas.NomeFantasiaEmp
,Empresas.CnpjEmp
,Empresas.InscEstadualEmp
,Empresas.InscMunicipalEmp
,Empresas.DataCadastroEmp
,Empresas.TipoCapitalEmp
,Empresas.PaisOrigemEmp
,Empresas.TipoEmpresa
,Empresas.TipoGestaoEmp
,Empresas.SetorEconomiaEmp
,Empresas.SegmentoEconomiaEmp
,Empresas.FuncionariosEmp
,Empresas.StatusEmp
,EmpresaContatos.NomeContato
FROM
Empresas LEFT OUTER JOIN EmpresaContatos
ON
Empresas.EmpresaId = EmpresaContatos.EmpresaId
WHERE
(EmpresaContatos.NomeContato LIKE '%cruz%')
) AS tbl
WHERE
row >= @firstRecord
row <= @lastRecord



Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Marcos Cruz
Starting Member

8 Posts

Posted - 2009-12-03 : 08:03:32
I tried, and get this error when a I execute the procedure


{"Conversion failed when converting the varchar value 'SELECT * FROM (
SELECT ROW_NUMBER() OVER(ORDER BY EmpresaContatos.NomeContato ASC) As row , Empresas.EmpresaId, Empresas.RazaoSocialEmp, Empresas.NomeFantasiaEmp, Empresas.CnpjEmp, Empresas.InscEstadualEmp, Empresas.InscMunicipalEmp, Empresas.DataCadastroEmp, Empresas.TipoCapitalEmp, Empresas.PaisOrigemEmp, Empresas.TipoEmpresa, Empresas.TipoGestaoEmp, Empresas.SetorEconomiaEmp, Empresas.SegmentoEconomiaEmp, Empresas.FuncionariosEmp, Empresas.StatusEmp, EmpresaContatos.NomeContato FROM Empresas LEFT OUTER JOIN EmpresaContatos ON Empresas.EmpresaId = EmpresaContatos.EmpresaId WHERE (EmpresaContatos.NomeContato LIKE '%cruz%')
) AS tbl
WHERE
row > ' to data type int."}
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-12-03 : 10:10:42
Why are you enclosing madhivanan's code inside a string?

Could you post exactly what you tried to run to get this error?


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Marcos Cruz
Starting Member

8 Posts

Posted - 2009-12-03 : 12:05:36
Charlie,

There is the code of my stored procedure.

ALTER PROCEDURE dbo.GetListaPaginada
@fieldList varchar(2000) = '*' -- Lista de campos
,@dataSource varchar(200) -- Nome da tabela
,@filterWhere varchar(200) = '' -- Condição de teste
,@columnOrder varchar(200) -- Nome da coluna para ordenação
,@orderBy varchar(10) -- Ordem de classificação ASC ou DESC
,@pageNum int = 1 -- Página a ser recuperada
,@pageSize int = NULL -- Quantidade de registros por página
AS
SET NOCOUNT ON
DECLARE
@sqlCom nvarchar(max) -- SQL to execute
,@totalRows int -- Total de registros

IF LTRIM(RTRIM(@filterWhere)) = '' -- Verifica se foi passado filtro de comparação
SET @filterWhere = '1 = 1'

IF @orderBy <> 'ASC' AND @orderBy <> 'DESC'
SET @orderBy = 'ASC'

-- Se não foi passado a quantidade de registros por página, então faz uma query
-- para selecionar todos os registros sem paginação.
IF @pageSize IS NULL
BEGIN
SET @sqlCom = 'SELECT ' + @fieldList + ' ' +
'FROM ' + @dataSource + ' ' +
'WHERE ' + @filterWhere + ' ' +
'ORDER BY ' + @columnOrder + ' ' + @orderBy
EXEC (@sqlCom) -- Executa o comando e retorna os registros encontrados.
END
ELSE
BEGIN

-- Conta a quantidade de registros existente no banco de dados.
SET @sqlCom = 'SELECT @totalRows = COUNT(*)
FROM ' + @dataSource + ' ' +
'WHERE ' + @filterWhere

EXEC sp_executeSQL
@sqlCom,
@params = N'@totalRows INT OUTPUT',
@totalRows = @totalRows OUTPUT

SELECT @totalRows AS totalRows -- Retorna o número de registros

-- Paginação de resultado SQL Server 2005
DECLARE
@firstRecord int,
@lastRecord int

SET @pageNum = ABS(@pageNum)
SET @pageSize = ABS(@pageSize)

IF @pageNum < 1 -- Certifica que o valor da página é positivo
SET @pageNum = 1

IF @pageSize < 1 -- Fixa a quantidade máxima de registros por página
SET @pageSize = 30 -- entre 1 e 30
IF @pageSize > 30
SET @pageSize = 30

-- Calcula qual é o primeiro e o último registro, que devem ser retornados
-- para satisfazer a quantidade de registros por página.
SET @firstRecord = ((@pageNum - 1) * @pageSize)
SET @lastRecord = @firstRecord + @pageSize + 1

-- Verifica se o primeiro registro é maior ou igual a quantidade total de
-- registros. Caso seja, então retorna a última página possível.
IF @firstRecord >= @totalRows
BEGIN
SET @lastRecord = @totalRows + 1
SET @firstRecord = @lastRecord - (@pageSize + 1)
END

-- Monta o comando para retornar somente a quantidade máxima de registros
-- permitido por página.
SET @sqlCom = 'SELECT ' + @fieldList + '
FROM (
SELECT ROW_NUMBER() OVER(ORDER BY ' + @columnOrder + ' ' + @orderBy + ') AS row, *
FROM ' + @dataSource + '
WHERE ' + @filterWhere + '
) AS tbl
WHERE
row > ' + CONVERT(varchar(9), @firstRecord) + ' AND
row < ' + CONVERT(varchar(9), @lastRecord)
EXEC (@sqlCom) -- retorna os registros.
END
RETURN

Go to Top of Page
   

- Advertisement -