UPDATE SQL语句出错

时间:2013-04-28 07:52:53

标签: sql sql-server

我正在尝试制定一个具有LEFT OUTER JOIN的UPDATE sql语句。当a.count小于subjects._max_enrollment_no或a.count为NULL时,应提交UPDATE。当我尝试提交查询时,它会给出一个错误,如下所示。

UPDATE [e-request]
SET    ereq_status = N'A'
FROM   [e-request]
       LEFT OUTER JOIN (SELECT Count(em_st_id) AS count,
                               em_sj_id
                        FROM   enrollment
                        WHERE  ( em_enrolled = 1 )
                        GROUP  BY em_sj_id) AS a
                    ON [e-request.ereq_sj_id] = a.em_sj_id
       RIGHT OUTER JOIN subjects
                     ON subjects.sj_id = [e-request.ereq_sj_id]
                        AND a.count < subjects.sj_max_enrollment_no
                        AND [e-request].ereq_id = @erequestID 

如何更改查询以使其有效?架构显示在底部

enter image description here

enter image description here

USE [EnrolDB]
GO
/****** Object:  Table [dbo].[enrollment]    Script Date: 4/28/2013 6:14:58 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[enrollment](
    [em_st_id] [int] NOT NULL,
    [em_sj_id] [int] NOT NULL,
    [em_order] [int] NOT NULL,
    [em_semester] [int] NOT NULL,
    [em_year] [int] NOT NULL,
    [em_result] [float] NULL,
    [em_enrolled] [bit] NOT NULL,
 CONSTRAINT [PK__tmp_ms_x__76502B66F97E750E] PRIMARY KEY CLUSTERED 
(
    [em_st_id] ASC,
    [em_sj_id] ASC,
    [em_order] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[e-request]    Script Date: 4/28/2013 6:14:59 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[e-request](
    [ereq_id] [int] NOT NULL,
    [ereq_st_id] [int] NOT NULL,
    [ereq_sj_id] [int] NOT NULL,
    [ereq_reason] [nvarchar](max) NOT NULL,
    [ereq_status] [nvarchar](10) NOT NULL,
 CONSTRAINT [PK_e-request] PRIMARY KEY CLUSTERED 
(
    [ereq_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO
/****** Object:  Table [dbo].[prerequisites]    Script Date: 4/28/2013 6:14:59 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[prerequisites](
    [pq_sj_id] [int] NOT NULL,
    [pq_pq_sj_id] [int] NOT NULL,
 CONSTRAINT [PK_prerequisites] PRIMARY KEY CLUSTERED 
(
    [pq_sj_id] ASC,
    [pq_pq_sj_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[student]    Script Date: 4/28/2013 6:14:59 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[student](
    [st_id] [int] NOT NULL,
    [st_first_name] [nvarchar](50) NOT NULL,
    [st_mid_name] [nvarchar](50) NULL,
    [st_last_name] [nvarchar](50) NULL,
PRIMARY KEY CLUSTERED 
(
    [st_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[subjects]    Script Date: 4/28/2013 6:14:59 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[subjects](
    [sj_id] [int] NOT NULL,
    [sj_name] [nvarchar](64) NOT NULL,
    [sJ_descr] [nvarchar](1024) NULL,
    [sj_max_enrollment_no] [int] NULL,
    [sj_avail] [bit] NULL,
PRIMARY KEY CLUSTERED 
(
    [sj_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[Table]    Script Date: 4/28/2013 6:14:59 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table](
    [pq_sj_id] [int] NOT NULL,
    [pq_pq_sj_id] [int] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [pq_sj_id] ASC,
    [pq_pq_sj_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[enrollment]  WITH CHECK ADD  CONSTRAINT [FK_enrollment_student] FOREIGN KEY([em_st_id])
REFERENCES [dbo].[student] ([st_id])
GO
ALTER TABLE [dbo].[enrollment] CHECK CONSTRAINT [FK_enrollment_student]
GO
ALTER TABLE [dbo].[enrollment]  WITH CHECK ADD  CONSTRAINT [FK_enrollment_subject] FOREIGN KEY([em_sj_id])
REFERENCES [dbo].[subjects] ([sj_id])
GO
ALTER TABLE [dbo].[enrollment] CHECK CONSTRAINT [FK_enrollment_subject]
GO
ALTER TABLE [dbo].[prerequisites]  WITH CHECK ADD  CONSTRAINT [FK_prerequisites_subjects] FOREIGN KEY([pq_sj_id])
REFERENCES [dbo].[subjects] ([sj_id])
GO
ALTER TABLE [dbo].[prerequisites] CHECK CONSTRAINT [FK_prerequisites_subjects]
GO
ALTER TABLE [dbo].[prerequisites]  WITH CHECK ADD  CONSTRAINT [FK_prerequisites_subjects2] FOREIGN KEY([pq_pq_sj_id])
REFERENCES [dbo].[subjects] ([sj_id])
GO
ALTER TABLE [dbo].[prerequisites] CHECK CONSTRAINT [FK_prerequisites_subjects2]
GO

1 个答案:

答案 0 :(得分:4)

[e-request.ereq_sj_id]更改为[e-request].ereq_sj_id