Update with if else statement - sql

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'

Related

How to solve the error_syntax error near '< '

Msg 102, Level 15, State 1, Line 3 Incorrect syntax near '<'.
I got the above error message every time I tried to execute the below query.
UPDATE [dbo].[FM1]
SET [Datum] = <Datum, smalldatetime,>
,[Gesamtzeit] = <Gesamtzeit, nvarchar(5),>
You can try this no need to specify datatype here.
UPDATE [dbo].[FM1]
SET [Datum] = 'YourValue'
,[Gesamtzeit] = 'YourValue'
where ...

sybase script don't understand trouble

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

The multi-part identifier could not be bound error with update statement and where exists clause

My query is as shown below . I am getting error as
Msg 4104, Level 16, State 1, Procedure USP_Group11HtmlFileDetails,
Line 71
The multi-part identifier "t_FI.Fullimage" could not be bound.
Msg 4104, Level 16, State 1, Procedure USP_Group11HtmlFileDetails,
Line 71
The multi-part identifier "t_FI.Caption" could not be bound.
#tblGroup11HtmlFileImages is table variable which is of type exactly similar to ta
Query
UPDATE FI
SET FI.Fullimage = t_FI.Fullimage,
FI.Caption = t_FI.Caption
FROM tblGroup11HtmlFileImages FI
WHERE EXISTS (SELECT *
FROM #tblGroup11HtmlFileImages t_FI
WHERE t_FI.[FileName] = FI.[FileName]
AND t_FI.Thumbnail = FI.Thumbnail)
The exists operator only checks for existence of a value, no value is actually retrieved from the Exists operator.
You need to join these two tables something like....
UPDATE FI
SET FI.Fullimage = t_FI.Fullimage
, FI.Caption = t_FI.Caption
FROM tblGroup11HtmlFileImages FI
INNER JOIN #tblGroup11HtmlFileImages t_FI
ON t_FI.[FileName] = FI.[FileName]
AND t_FI.Thumbnail = FI.Thumbnail

SQL Help - If Then Else

I need the help of someone who understands SQL better than I do...
I am trying to make a single SP call that uses the IF THEN ELSE but keep getting three "the multi-part identifier could not be bound" messages.
Pages can be sponsored by a Business or a Group.
In the advertEntityType field I have a "B" or a "G" for business or group.
In the advertEntityID field I have the businessID or groupID.
SELECT
Advertisements.advertEntityType
,Advertisements.advertEntityID
FROM Advertisements
WHERE Advertisements.advertPageName = #pageName
IF (Advertisements.advertEntityType = 'G')
SELECT
Group.groupID
,Group.groupName
,Group.groupPhone
,Group.groupWebsite
,Group.groupLogoName
FROM Group
WHERE Group.groupID = Advertisements.advertEntityID
ELSE
SELECT
Business.businessID
,Business.businessName
,Business.businessWorkPhone
,Business.businessWebsite
,Business.businessLogoName
FROM Business
WHERE Business.businessID = Advertisements.advertEntityID
When I try to execute I get these messages which I cannot seem to get my head around...
Msg 4104, Level 16, State 1, Procedure Sponsor_s01, Line 36
The multi-part identifier "Advertisements.advertEntityType" could not be bound.
Msg 4104, Level 16, State 1, Procedure Sponsor_s01, Line 46
The multi-part identifier "Advertisements.advertEntityID" could not be bound.
Msg 4104, Level 16, State 1, Procedure Sponsor_s01, Line 58
The multi-part identifier "Advertisements.advertEntityID" could not be bound.
Advertisements.advertEntityType is a table name and column name. You need to assign these values to local variables if you want to use them in if statements. If I understand correctly you want to evaluate based on the condition in the first select statement. In this case you should use such query:
DECLARE #entityType int --assuming the type of Advertisements.advertEntityType is int
DECLARE #advertEntityID int --assuming the type of Advertisements.advertEntityID is int
SELECT
#entityType = Advertisements.advertEntityType,
#advertEntityID = Advertisements.advertEntityID
FROM Advertisements
WHERE Advertisements.advertPageName = #pageName
IF (#entityType = 'G')
SELECT
Group.groupID
,Group.groupName
,Group.groupPhone
,Group.groupWebsite
,Group.groupLogoName
FROM Group
WHERE Group.groupID = #advertEntityID
ELSE
SELECT
Business.businessID
,Business.businessName
,Business.businessWorkPhone
,Business.businessWebsite
,Business.businessLogoName
FROM Business
WHERE Business.businessID = #advertEntityID

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.