关键字“SELECT”附近的语法不正确。创建视图错误
我正在为作业创建数据库视图,
但我在代码中不断收到此错误。
CREATE VIEW Rental_View
SELECT Mo.Movie_ID, copy_id, mo.Movie_Name, format_name
FROM Movies AS mo JOIN copies AS co
ON mo.Movie_ID = co.movie_id
JOIN format AS fo
ON co.Format_id = fo.format_id
SELECT c.customer_id, rental_ID, DATEDIFF (day, rental_date, return_date) AS rental_duration' , c.first_name + ' ' + c.last_name AS customer_name
FROM Customers AS c INNER JOIN rentals AS r
ON c.customer_ID = r.customer_ID
在第一个 SELECT 语句中,它带有红色下划线,并且出现错误
“关键字“SELECT”附近的语法不正确。”
我真的不知道如何纠正这个问题,因为我对 SQL 还很陌生,任何帮助将不胜感激。
提前致谢。
I am creating a view for a database for an assignment,
i keep getting this error in my code though.
CREATE VIEW Rental_View
SELECT Mo.Movie_ID, copy_id, mo.Movie_Name, format_name
FROM Movies AS mo JOIN copies AS co
ON mo.Movie_ID = co.movie_id
JOIN format AS fo
ON co.Format_id = fo.format_id
SELECT c.customer_id, rental_ID, DATEDIFF (day, rental_date, return_date) AS rental_duration' , c.first_name + ' ' + c.last_name AS customer_name
FROM Customers AS c INNER JOIN rentals AS r
ON c.customer_ID = r.customer_ID
In the first SELECT statement it is underlined red and i get the error
"Incorrect syntax near the keyword 'SELECT'."
i literally have no idea how to correct this as i am fairly new to SQL, any help would be much appreciated.
thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要
AS
关键字:如果您想将两个
SELECT
合并为一个,则需要JOIN
两者的表,如下所示:You need the
AS
keyword:If you want to combine the two
SELECT
in one, you need toJOIN
the tables from both, with something like this:您缺少“AS”关键字
you are missing the "AS" keyword
您看起来在这段代码中的
rental_duration
之后有一个错误的单引号 -尝试
You look top have an erroneous single quote after
rental_duration
in this code -try
您可以尝试将第一个选择从
SELECT Mo.Movie_ID, copy_id, mo.Movie_Name, format_name
更改为
SELECT mo.Movie_ID, copy_id, mo.Movie_Name, format_name
you can try changing the first select from
SELECT Mo.Movie_ID, copy_id, mo.Movie_Name, format_name
to
SELECT mo.Movie_ID, copy_id, mo.Movie_Name, format_name
尝试:
通常情况下,您不会创建一个返回多个结果集的视图。如果这确实是您想要的,那么 BEGIN/END 块应该可以解决问题(未经测试 - 现在不在任何 RDMS 前面):
Try:
Normally you don't create a view that will return more than one result set. If this is truly what you desire then a BEGIN/END block should solve the problem (Untested - not infront of any RDMS right now):
您发布的代码中有多个错误。
CREATE VIEW 语句在 SELECE 语句之前需要 AS 关键字。你可以这样做:
我可以看到另一个 SELECT 语句,这在视图中没有任何意义。添加第二个 SELECT 语句将导致错误“关键字‘SELECT’附近的语法不正确。”对于第二个 SELECT 语句。
There are multiple errors in the code that you have posted.
The CREATE VIEW statement needs AS keyword befor the SELECE statement. You might do it like:
And I could see another SELECT statement, that is making no meaning in a view. Adding the second SELECT statement will cause an error "Incorrect syntax near the keyword 'SELECT'." for the second SELECT statement.