How to solve the error_syntax error near '< ' - sql

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 ...

Related

Why can't bulk insert format csv?

I have this code:
BULK INSERT [custdb].[dbo].[TB_T_DISP_PARTY] FROM 'd\DB\dbo.TB_T_DISP_PARTY.csv' WITH ( FIRSTROW = 2, FORMAT = 'CSV' );
And then I get this error
'Msg 102, Level 15, State 1, Line 6
Incorrect syntax near'FORMAT'.'
Can somebody tell me how to fix this error?
It is just bcoz of Version Difference
FORMAT = 'CSV' Applies to: SQL Server 2017 + Versions

Msg 102, Level 15, State 1, Line 5 Incorrect syntax near '‌'. insert statement SQL Server

Hi i am getting this error Msg 102, Level 15, State 1, Line 5 Incorrect syntax near '‌' and i dont know from where is the problem. That is the code and also i attached a screenshot.
USE CobornSalesDB;
GO
INSERT INTO SalesActivity
VALUES ('AC00001','05-12-2016','AG16170','C000001',
'P0001','S00002'‌​,1,200000.00,NULL,‌1.2220,20,100000.00,
'12-25-2016','12-30-2016','12-31-2016','A000001','PR00001');
GO
![SCREENSHOT][1]
You have a hidden character
If I convert to ANSI in NotePad++
INSERT INTO
SalesActivity
VALUES
(
'AC00001',
'05-12-2016',
'AG16170',
'C000001',
'P0001',
'S00002'‌​, --this bad boy
1 ,
200000.00,
NULL,
1.2220,
20 ,
100000.00,
'12-25-2016',
'12-30-2016',
'12-31-2016',
'A000001',
'PR00001');
GO

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

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'

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.