This question already has answers here:
SQL Create View ,Show view,Incorrect syntax near the keyword 'SELECT'
(1 answer)
Creating a VIEW in SQL-Server
(2 answers)
Closed 9 months ago.
When I try to create a view, I get the error:
Msg 156, Level 15, State 1, Procedure viewFangst2021, Line 9 [Batch Start Line 445]
Incorrect syntax near the keyword 'SELECT'
I do not understand what is wrong with the code. The same thing happens if I am going to create a procedure.
Could it have something to do with settings or such in SQL? for this happens regardless of whether I create a view
CREATE VIEW viewFangst2021
AS
SELECT
FISK.FiskeNr, FISK.Fiskeslag,
COUNT(*) AS Antall,
ROUND(AVG(Vekt), 1) AS [Gjennomsnittsvekt (kg)]
FROM
FANGST, FISK
WHERE
FANGST.FiskeNr = FISK.FiskeNr
AND YEAR (Dato) = 2021
GROUP BY
FISK.FiskeNr, FISK.Fiskeslag
SELECT *
FROM viewFangst2021
Related
I can't create a function in SQL Server 2000 in my database. So, I tried to use the example in the help (F1), but I have the same error.
CREATE FUNCTION CubicVolume
-- Input dimensions in centimeters
(#CubeLength decimal(4,1),
#CubeWidth decimal(4,1),
#CubeHeight decimal(4,1)
)
RETURNS decimal(12,3) -- Cubic Centimeters.
AS
BEGIN
RETURN ( #CubeLength * #CubeWidth * #CubeHeight )
END
Errors:
Msg 170, Level 15, State 1, Line 2 Line 2:
Incorrect syntax near 'FUNCTION'.
Msg 137, Level 15, State 1, Line 9
Must declare the variable '#CubeLength'.
I run this script for Database Northwind and it didn't have problem, so I don't understand what the problem is.
thank you for your answers.
The problem was resolved.
The problem was compatibility on the level as Aaron Bertrand said.
Click Right on Database -->Properties-->Options-->Compatibility Level
(Needs 80 or high)
enter image description here
Trying to create a new table from parts of an existing one using:
CREATE TABLE Spillover_HE
AS (SELECT * FROM [dbo].[Y16_GROW_Teacher]
WHERE HEDI = 'H');
And it keeps returning the error message:
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '('.
When I hover over the code it says:
Incorrect syntax near '('. Expecting ID.
I've tried changing the table name (same error), removing the WHERE statement (that generates an addition error of "Expecting UNION or EXCEPT"). I've read some answers to similar questions but am new to SQL and am very lost.
You should use SELECT INTO syntax:
SELECT * INTO Spillover_HE
FROM [dbo].[Y16_GROW_Teacher]
WHERE HEDI = 'H'
This will work on the MSSQL
SELECT * into Spillover_HE FROM [dbo].[Y16_GROW_Teacher]
WHERE HEDI = 'H'
Probably you are trying to create view.
CREATE VIEW Spillover_HE
AS ( SELECT *
FROM [dbo].[Y16_GROW_Teacher]
WHERE HEDI = 'H'
);
I have a create view query and I want to check if it does not exist yet, then create view. I tried to create like this:
CASE WHEN IS NOT EXISTS vw_Delays
THEN
VIEW vw_Delays AS
SELECT RD_RfileID_fk_ind, SUM(DATEDIFF(day, RD_Startdate, RD_EndDate)) AS delays FROM dbo.t_RfDelay
GROUP BY RD_RfileID_fk_ind
END
but it returns these errors:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'CASE'.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'END'.
How to solve this? Please can anyone help me to fix this ?
You need to use this code to check for the view's existence:
IF NOT EXISTS (SELECT * FROM sys.views WHERE Name = N'vw_Delays')
.....
The next obstacle you'll encounter is the fact that the CREATE VIEW statement must be the first of a SQL batch - so you cannot have it right after the existence check.
What I usually do is the opposite:
check if the view does exist
and if so - drop the existing view
then create the view from scratch
and I use this code for this setup:
IF EXISTS (SELECT * FROM sys.views WHERE Name = N'vw_Delays')
DROP VIEW dbo.vw_Delays;
GO
CREATE VIEW dbo.vw_Delays
AS
SELECT
RD_RfileID_fk_ind,
SUM(DATEDIFF(day, RD_Startdate, RD_EndDate)) AS delays
FROM
dbo.t_RfDelay
GROUP BY
RD_RfileID_fk_ind
Why is this SQL getting a syntax error and how can it be fixed?
UPDATE s
SET s.modified_date = l.action_date
FROM
(SELECT l.action_date, l.user_id FROM item_audit_log) l
WHERE l.user_id = s.staff_id
Error:
Msg 4104, Level 16, State 1, Line 4
The multi-part identifier "l.action_date" could not be bound.
Msg 4104, Level 16, State 1, Line 4
The multi-part identifier "l.user_id" could not be bound.
You are getting the error because your subquery references "l" in the subquery, but it is not defined inside the subquery (it is defined in the outer scope). However, you don't need the subquery:
UPDATE s
SET modified_date = l.action_date
FROM staffs s JOIN
item_audit_log l
ON l.user_id = s.staff_id;
This assumes you have a table called s. I imagine that this really should be an alias, also defined in the FROM clause.
As your request is "WHY" this occurs, let me clarify it :
This error usually occurs when an alias is used when referencing a
column in a SELECT statement and the alias used is not defined
anywhere in the FROM clause of the SELECT statement.
Code is running fine when commenting DELETE statement and trying with SELECT statement.
Please help
DELETE FROM
--select * from
Site as s
join
(select SiteID,Code, Name, Dense_rank() over (partition by Code order by SiteID ) as Rank from Site
) as t
on s.SiteID = t.SiteID
WHERE t.Rank != 1
Getting following error message
Msg 156, Level 15, State 1, Line 5
Incorrect syntax near the keyword 'as'.
Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'as'.
You can't alias a delete table, but delete can refer to an alias. Instead of this:
delete from Site as s
...
Try:
delete from s
from Site as s
...