SQL - 关键字附近的语法不正确

发布于 2024-12-13 20:28:40 字数 785 浏览 1 评论 0原文

我的查询存在小问题:

CREATE VIEW rental_view
AS 
   SELECT 
       m.movie_id, movie_name, co.copy_id, 
       f.format_id, format_name, cu.customer_id, 
       (first_name + ' ' + surname) AS customer_name, 
       rental_id, rental_date, return_date, 
       ISNULL(return_date, CAST(DATEDIFF(dd, rental_date, GETDATE() ) AS rental_duration 
   FROM 
         movie AS m 
   INNER JOIN copy AS co ON m.movie_id = co.movie_id
   INNER JOIN format AS f ON co.format_id = f.format_id
   INNER JOIN rental ON co.copy_id = rental.copy_id
   INNER JOIN customer AS cu ON rental.customer_id = cu.customer_id

导致错误:

消息 156,级别 15,状态 1,
过程 Rental_view,第 3 行关键字“FROM”附近的语法不正确。

我对此进行了很长时间的尝试,但无法解决。

感谢您的帮助。

Small problem with my query here:

CREATE VIEW rental_view
AS 
   SELECT 
       m.movie_id, movie_name, co.copy_id, 
       f.format_id, format_name, cu.customer_id, 
       (first_name + ' ' + surname) AS customer_name, 
       rental_id, rental_date, return_date, 
       ISNULL(return_date, CAST(DATEDIFF(dd, rental_date, GETDATE() ) AS rental_duration 
   FROM 
         movie AS m 
   INNER JOIN copy AS co ON m.movie_id = co.movie_id
   INNER JOIN format AS f ON co.format_id = f.format_id
   INNER JOIN rental ON co.copy_id = rental.copy_id
   INNER JOIN customer AS cu ON rental.customer_id = cu.customer_id

Results in error:

Msg 156, Level 15, State 1,
Procedure rental_view, Line 3 Incorrect syntax near the keyword 'FROM'.

I had a long attempt at this one and can't solve it.

Appreciate the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

染火枫林 2024-12-20 20:28:40

您在这一行中缺少两个括号:

AS SELECT 
    m.movie_id, movie_name, co.copy_id, f.format_id, format_name, 
    cu.customer_id, (first_name + ' ' + surname) AS customer_name, 
    rental_id, rental_date, return_date, 
    ISNULL(
        return_date, 
        CAST(DATEDIFF(dd, rental_date, GETDATE()))) AS rental_duration 

无论如何,您的语法在其他部分是错误的:

  • CAST 希望转换字段、关键字 AS 和新类型(请参阅 docs)
  • DATEDIFF 需要两个参数(参见 文档) <-- 这个如果您使用的是 MySql;使用 MS-SQL 是正确的

You're missing two parenthesis on this line:

AS SELECT 
    m.movie_id, movie_name, co.copy_id, f.format_id, format_name, 
    cu.customer_id, (first_name + ' ' + surname) AS customer_name, 
    rental_id, rental_date, return_date, 
    ISNULL(
        return_date, 
        CAST(DATEDIFF(dd, rental_date, GETDATE()))) AS rental_duration 

Anyway your syntax is wrong in other parts:

  • CAST wants the field to be converted, keyword AS and new type (see docs)
  • DATEDIFF wants two params (see docs) <-- this if you're using MySql; with MS-SQL is correct
愛上了 2024-12-20 20:28:40

试试这个:

  CREATE VIEW rental_view
  AS SELECT m.movie_id, 
movie_name, 
co.copy_id, 
f.format_id, 
format_name, 
cu.customer_id, 
(first_name + ' ' + surname) AS customer_name, 
rental_id, 
rental_date, 
return_date,
ISNULL(return_date,
CAST(DATEDIFF(dd, rental_date, GETDATE()) AS *type desired here*)) AS name of column
  FROM movie as m 
  INNER JOIN copy AS co
  ON m.movie_id = co.movie_id
  INNER JOIN format AS f
  ON co.format_id = f.format_id
  INNER JOIN rental 
  ON co.copy_id = rental.copy_id
  INNER JOIN customer AS cu
  ON rental.customer_id = cu.customer_id

try this:

  CREATE VIEW rental_view
  AS SELECT m.movie_id, 
movie_name, 
co.copy_id, 
f.format_id, 
format_name, 
cu.customer_id, 
(first_name + ' ' + surname) AS customer_name, 
rental_id, 
rental_date, 
return_date,
ISNULL(return_date,
CAST(DATEDIFF(dd, rental_date, GETDATE()) AS *type desired here*)) AS name of column
  FROM movie as m 
  INNER JOIN copy AS co
  ON m.movie_id = co.movie_id
  INNER JOIN format AS f
  ON co.format_id = f.format_id
  INNER JOIN rental 
  ON co.copy_id = rental.copy_id
  INNER JOIN customer AS cu
  ON rental.customer_id = cu.customer_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文