sybase script don't understand trouble - sql

I 'm not a regular of Sybase ASE.
Tring to make a script to add line into a tempory table.
sybase version 12.5.4
BEGIN
declare and init variable
--boucle
select * into #parTemp from Parution where 1=2
WHILE #dateCourante <= #dateFin
BEGIN
select #dpart1=datepart(mm,#dateCourante)
select #dpart2=datepart(dd,#dateCourante)
select #dpart3=datepart(dw,#dateCourante)
--si on est pas le 1er mai
if #dpart1 <> 5 AND #dpart2 <> 1
begin
--id parution
select #idPar = #idPar + 1
select #idParChaine = convert(varchar(10),#idPar)
--num parution
select #numPar = #numPar + 1
select #numParChaine = convert(varchar(20),#numPar)
--prix du jour courant
if datepart(dw,#dateCourante)=6
select #prixCourant = #prixVen
else
if datepart(dw,#dateCourante)=1
select #prixCourant = #prixDim
else
select #prixCourant = #prix
end
end
insert into #parTemp values (#idParChaine,#dateCourante,#numParChaine,#nbPagination,#prixCourant,#poids,#parCompt,#parOJD,#resVal)
end
select #dateCourante = dateadd(dd,1,#dateCourante)
END
END
Errors i get :
Error code 156, SQL state ZZZZZ: Incorrect syntax near the keyword 'WHILE'.
Error code 156, SQL state ZZZZZ: Incorrect syntax near the keyword 'end.
Error code 156, SQL state ZZZZZ: Incorrect syntax near the keyword 'End'.
i really don't understand, thank for help.
Pierre

Related

Error 1064 you have error in Mysql bench syntax

Last two line show error .MY sqlwork bench show syntax error in last two line Limit 1
End
trying to run Database procedures.Error code Error 1064 you have error in Mysql bench syntax
CREATE PROCEDURE `xyz_Get`(
IN pPlaceId BIGINT,
IN pSwitchedCompanyId BIGINT,
IN pRequestedBy BIGINT
)
BEGIN
SELECT
`PlaceId`,
`CategoryCode`,
`CategoryName`,
`Description`,
`ParentCategoryId_FK`,
`ModuleId_FK`,
`LanguageId_FK`,
`CompanyId_FK`,
`OwnerCompanyId_FK`,
`IsDeleted`,
`IsArchived`,
`CreatedDate`,
`ModifiedDate`,
`CreatedBy_FK`,
`ModifiedBy_FK`
FROM `xyz_Place`
WHERE
`PlaceId` = pPlaceId AND
(
`CompanyId_FK` = pSwitchedCompanyId OR
`OwnerCompanyId_FK` = pSwitchedCompanyId
)
AND `IsDeleted` = 0
LIMIT 1;
END

SQL query working on SQL Server 2014 but not on 2008 R2

the following query works just fine on SQL Server 2014, but returns following error on SQL Server 2008:
Mens. 102, Nivel 15, Estado 1, Línea 1
Incorrect syntax near '='.
Mens. 156, Nivel 15, Estado 1, Línea 4
Incorrect syntax near the keyword 'AS'.
Mens. 156, Nivel 15, Estado 1, Línea 7
Incorrect syntax near the keyword 'AS'.
This is the query (a bit long, sorry):
SELECT per_agrup.COEMPRESA AS 'Empresa',
per_agrup.DCCENTROS AS 'Centre',
per_agrup.DCDIVISIONES AS 'Divisió',
per_agrup.DCGRUPPROF AS 'Grup professional',
per_agrup.DCUNIPROD AS 'Direcció/Àrea',
per_agrup.DCPUESTOTRABAJO AS 'Lloc de treball',
per_agrup.DCCATEGORIA AS 'Categoria',
per_agrup.DCINCIDENCIA AS 'Motiu',
Sum(IIf(per_agrup.COMPERSONAL=1
And per_agrup.COCOBERTURA Is Not Null,
IIf(per_cober_agrup.COCOBERTURA Is Not Null And per_cober_agrup.COMPERSONAL=1,Round(per_cober_agrup.SumaDeHORAS/60.00,2),
IIf(per_cober_agrup.COCOBERTURA Is Null,Round(per_agrup.SumaDeHORAS/60.00,2),0)),Round(per_agrup.SumaDeHORAS/60.00,2))) AS HORES
FROM ( SELECT TENPLANING_PER.COEMPRESA,
TENPLANING_PER.COIDENNUMERO,
TENPLANING_PER.FECHAJORNADA,
TENPLANING_PER.COPUESTOTRABAJO,
TENPLANING_PER.COTIPOMOV,
TENPLANITIPOINCIDEN.DCINCIDENCIA,
TENPLANITIPOINCIDEN.COMPERSONAL,
TENPLANING_PER.COCOBERTURA,
DCPUESTOTRABAJO,
DCDIVISIONES,
DCGRUPPROF,
DCUNIPROD,
DCCATEGORIA,
DCCENTROS,
Sum(TENPLANING_PER.HORAS) AS SumaDeHORAS
FROM TENPLANITIPOINCIDEN
INNER JOIN TENPLANING_PER ON (TENPLANITIPOINCIDEN.COINCIDENCIA = TENPLANING_PER.COTIPOMOV)
AND (TENPLANITIPOINCIDEN.COEMPRESA = TENPLANING_PER.COEMPRESA)
JOIN TPUESTOTRABAJO ON TPUESTOTRABAJO.COEMPRESA=TENPLANING_PER.COEMPRESA
AND TENPLANING_PER.COPUESTOTRABAJO=TPUESTOTRABAJO.COPUESTOTRABAJO
JOIN TUNIPROD ON TENPLANING_PER.COEMPRESA=TUNIPROD.COEMPRESA
AND TUNIPROD.COUNIPROD=TPUESTOTRABAJO.COPTUNIDPRO
JOIN TDIVISIONES ON TDIVISIONES.COEMPRESA=TUNIPROD.COEMPRESA
AND TDIVISIONES.CODIVISIONES=TUNIPROD.CODIVISION
JOIN TCATEGORIAS ON TCATEGORIAS.COEMPRESA=TPUESTOTRABAJO.COEMPRESA
AND TCATEGORIAS.COCATEGORIA=TPUESTOTRABAJO.COPTCATEGORIA
JOIN TGRUPPROF ON TENPLANING_PER.COEMPRESA=TGRUPPROF.COEMPRESA
AND TGRUPPROF.COGRUPPROF=TCATEGORIAS.COGRUPPROF
JOIN TCENTROS ON TUNIPROD.COEMPRESA=TCENTROS.COEMPRESA
AND TUNIPROD.COCENTRO=TCENTROS.COCENTROS
GROUP BY TENPLANING_PER.COEMPRESA,
TENPLANING_PER.COIDENNUMERO,
TENPLANING_PER.FECHAJORNADA,
TENPLANING_PER.COPUESTOTRABAJO,
TENPLANING_PER.COTIPOMOV,
TENPLANITIPOINCIDEN.DCINCIDENCIA,
TENPLANITIPOINCIDEN.COMPERSONAL,
TENPLANING_PER.COCOBERTURA,
DCPUESTOTRABAJO,
DCDIVISIONES,
DCGRUPPROF,
DCUNIPROD,
DCCATEGORIA,
DCCENTROS ) AS per_agrup
LEFT JOIN ( SELECT TENPLANING_PER_COBER.COEMPRESA,
TENPLANING_PER_COBER.COIDENNUMERO,
TENPLANING_PER_COBER.COCOBERTURA,
TENPLANING_PER_COBER.FECHAJORNADA,
TENPLANING_PER_COBER.COTIPOMOV,
TENPLANITIPOINCIDEN.DCINCIDENCIA,
TENPLANITIPOINCIDEN.COMPERSONAL,
TENPLANING_PER_COBER.COPUESTOTRABAJO,
Sum(TENPLANING_PER_COBER.HORAS) AS SumaDeHORAS
FROM TENPLANITIPOINCIDEN
INNER JOIN TENPLANING_PER_COBER ON (TENPLANITIPOINCIDEN.COINCIDENCIA = TENPLANING_PER_COBER.COTIPOMOV) AND (TENPLANITIPOINCIDEN.COEMPRESA = TENPLANING_PER_COBER.COEMPRESA)
GROUP BY TENPLANING_PER_COBER.COEMPRESA,
TENPLANING_PER_COBER.COIDENNUMERO,
TENPLANING_PER_COBER.COCOBERTURA,
TENPLANING_PER_COBER.FECHAJORNADA,
TENPLANING_PER_COBER.COTIPOMOV,
TENPLANITIPOINCIDEN.DCINCIDENCIA,
TENPLANITIPOINCIDEN.COMPERSONAL,
TENPLANING_PER_COBER.COPUESTOTRABAJO ) AS per_cober_agrup ON per_agrup.COEMPRESA = per_cober_agrup.COEMPRESA
AND per_agrup.COIDENNUMERO = per_cober_agrup.COIDENNUMERO
AND per_agrup.FECHAJORNADA = per_cober_agrup.FECHAJORNADA
AND per_agrup.COCOBERTURA = per_cober_agrup.COCOBERTURA
GROUP BY per_agrup.COEMPRESA,
per_agrup.DCCENTROS,
per_agrup.DCDIVISIONES,
per_agrup.DCGRUPPROF,
per_agrup.DCUNIPROD,
per_agrup.COTIPOMOV,
per_agrup.DCINCIDENCIA,
per_agrup.DCPUESTOTRABAJO,
per_agrup.DCCATEGORIA,
per_agrup.COMPERSONAL,
Year(per_agrup.FECHAJORNADA),
Month(per_agrup.FECHAJORNADA)
HAVING ((per_agrup.COEMPRESA='1')
AND ((per_agrup.COTIPOMOV)=6
OR (per_agrup.COTIPOMOV)=48
OR (per_agrup.COTIPOMOV)=85
OR (per_agrup.COTIPOMOV)=71
OR (per_agrup.COTIPOMOV)=61
OR (per_agrup.COTIPOMOV)=5
OR (per_agrup.COTIPOMOV)=9
OR (per_agrup.COTIPOMOV)=94
OR (per_agrup.COTIPOMOV)=11
OR (per_agrup.COTIPOMOV)=111
OR (per_agrup.COTIPOMOV)=22
OR (per_agrup.COTIPOMOV)=7)
AND ((Year(per_agrup.FECHAJORNADA))=2015)
AND ((Month(per_agrup.FECHAJORNADA))=1))
ORDER BY per_agrup.COEMPRESA,
per_agrup.DCCENTROS,
per_agrup.DCDIVISIONES,
per_agrup.DCGRUPPROF,
per_agrup.DCUNIPROD,
per_agrup.DCPUESTOTRABAJO,
per_agrup.DCCATEGORIA,
per_agrup.DCINCIDENCIA
I have noticed that removing this last column query works fine
Sum(IIf(per_agrup.COMPERSONAL=1 And per_agrup.COCOBERTURA Is Not Null,IIf(per_cober_agrup.COCOBERTURA Is Not Null And per_cober_agrup.COMPERSONAL=1,Round(per_cober_agrup.SumaDeHORAS/60.00,2),IIf(per_cober_agrup.COCOBERTURA Is Null,Round(per_agrup.SumaDeHORAS/60.00,2),0)),Round(per_agrup.SumaDeHORAS/60.00,2))) AS HORES
What is wrong with this last column in SQL Server 2008?
Thank you and excuse me for my bad english.
Replacing IIF's with CASE statment solved the problem.
Thanks to #Lamak and #chipmunkofdoom2

Update with if else statement

I tried using an SQL query below but got an error which says:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'IF'.
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'THEN'.
Here's the SQL query
UPDATE TBLGPS
IF speed >= 0
THEN SET REMARKS = 'Running'
ELSE SET REMARKS = 'Stopped'
WHERE PLATENO = 'ALCORAN-WIH312' AND TRXTIME = '13:16:20'
Can anyone tell me where I went wrong?
IF cannot be used in "regular" SQL Statements, use CASE instead:
UPDATE TBLGPS
set remarks = case
when speed >= 0 then 'Running'
else 'Stopped'
end
WHERE PLATENO = 'ALCORAN-WIH312'
AND TRXTIME = '13:16:20'

getting error for if/else in a stored procedure

I am relatively very new to SQL queries but I have this stored procedure where I am trying to get value of a declared variable as mentioned below but getting error,
First line is 20 here,
declare #m_ID_v int
set #m_ID_v = ( select ID_C from M_T where MName_C = #MName_parameter)
declare #g bit
if (select G_L_Column from G_L_table Where M_ID_Column = #M_ID_variable)
set #g_v = 1
else
set #g_variable = 0
Exception I get:
Msg 4145, Level 15, State 1, Procedure GetID, Line 20
An expression of non-boolean type specified in a context where a
condition is expected, near 'set'. Msg 156, Level 15, State 1,
Procedure GetID, Line 21 Incorrect syntax near the keyword 'else'.
Now if I remove declare #g... and try to parse it, no error occurs
EDIT
I want my code to check for returned value by my select statement so "if exists" is not really what am looking for, sorry.
try use if exists:
declare #g_v bit
if exists(select G_L_Column from G_L_table Where M_ID_Column = #M_ID_variable)
set #g_v = 1
else
set #g_v = 0
declare #m_ID_v int
set #m_ID_v = ( select ID_C from M_T where MName_C = #MName_parameter)
declare #g bit
if ((select G_L_Column from G_L_table Where M_ID_Column = #M_ID_variable) = value )
set #g_v = 1
else
set #g_variable = 0
You can't say
if (select ...
You have to compare something with something else in a if statement, or use a boolean function such as exists

Dynamic UPDATE statement

I want to create a dynamic update query where I need to set a certain value in a column. But the column name needs to be SELECTed from another table.
I have already the following query:
UPDATE core.TableRes
SET (
SELECT Code FROM core.TableFields
INNER JOIN core.TableXTableFields ON TableXTableFields.FieldID = TableFields.FieldID
INNER JOIN core.TableResRefLinks ON TableResRefLinks.ExtraFieldID = TableXTableFields.ExtraFieldID
WHERE TableResRefLinks.TableResRefLinksID = RefLinks.TableResRefLinksID)
= (
SELECT Value FROM core.TableResRefLinks WHERE TableResRefLinksID = RefLinks.TableResRefLinksID)
FROM core.TableRes
INNER JOIN core.TableResRefLinks RefLinks ON RefLinks.ResourceID = TableRes.ResourceID
INNER JOIN core.TableXTableFields ON TableXTableFields.ExtraFieldID = RefLinks.ExtraFieldID
INNER JOIN core.TableFields ON TableFields.FieldID = TableXTableFields.FieldID
WHERE (EndDate IS NULL OR EndDate > GETDATE()) AND
(
SELECT Code FROM core.TableFields
INNER JOIN core.TableXTableFields ON TableXTableFields.FieldID = TableFields.FieldID
INNER JOIN core.TableResRefLinks ON TableResRefLinks.ExtraFieldID = TableXTableFields.ExtraFieldID
WHERE TableResRefLinks.TableResRefLinksID = RefLinks.TableResRefLinksID)
<>
(
SELECT Value FROM core.TableResRefLinks
WHERE TableResRefLinksID = RefLinks.TableResRefLinksID)
It gives me the following errors:
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '('.
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '='.
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'FROM'.
Msg 102, Level 15, State 1, Line 14
Incorrect syntax near '<'.
Is there a way to solve this? If I change the complete UPDATE and SET statements and replace them with a SELECT *, I get results.
EDIT
Here are the datatypes
TableFields.Code => nvarchar(100)
TableResRefLinks.Value => sql_variant
And the datatypes of the columns that have as column name TableFields.Code are set as sql_variant
You can't solve this using plain SQL. You would need some kind of scripting to build your statement. For example postgresql has a scripting language called "pgpsql" which allows building dynamic SQL statements. But this clearly depends on the underlying RDBMS.
By the way: this works with SELECT as you are doing simple sub-select.