4

我的存储过程在 SQL Server 2008 中运行良好,但是当我尝试在 SQL Server 2005 中运行相同的过程时,它会抛出此错误说明

在这种情况下不允许子查询。只允许标量表达式。

以下是我的sp

USE dbEmployeeManagementSystem
GO
CREATE PROCEDURE [dbo].spInsertTaskAssignmentsample
(
@Username nvarchar(50),
@ProjectName nvarchar(50),
@ClientName nvarchar(50),
@Status nvarchar(50),
@StartDate nvarchar(50),
@EndDate nvarchar(50),
@ReportingManager nvarchar(50),
@Comments nvarchar(100)
 )
AS
BEGIN   
INSERT INTO tblTaskAssignment
(EID,ProjectName, ClientName, Status, StartDate, EndDate,ReportingManager,Comments) 
Values
((SELECT top 1 EID FROM tblLogin WHERE Username=@Username), @ProjectName, @ClientName, @Status, @StartDate, @EndDate,@ReportingManager,@Comments)
END

请帮助我,是否有任何解决方案或 sql 2005 不支持此类查询?

提前致谢。

4

4 回答 4

10

您可以使用代替SELECT 子句VALUES

INSERT INTO tblTaskAssignment
(EID,ProjectName, ClientName, Status, StartDate, EndDate,ReportingManager,Comments) 
SELECT (SELECT TOP 1 EID FROM tblLogin WHERE Username=@Username), @ProjectName, @ClientName, @Status, @StartDate, @EndDate,@ReportingManager,@Comments
于 2013-06-12T10:55:25.977 回答
4

SQL Server 2005 不支持此功能。它于 2008 年推出。

您可以将子查询的结果分配给变量并在VALUES子句中使用它。

于 2013-06-12T10:50:24.480 回答
2

备选方案 1:

您可以将 EID 放入这样的变量中:

    CREATE PROCEDURE [dbo].spInsertTaskAssignmentsample
    (
    @Username nvarchar(50),
    @ProjectName nvarchar(50),
    @ClientName nvarchar(50),
    @Status nvarchar(50),
    @StartDate nvarchar(50),
    @EndDate nvarchar(50),
    @ReportingManager nvarchar(50),
    @Comments nvarchar(100)
     )
    AS
    BEGIN

    DECLARE @EID INT;
    SET @EID = (SELECT top 1 EID FROM tblLogin WHERE Username=@Username);

    INSERT INTO tblTaskAssignment
    (EID,ProjectName, ClientName, Status, StartDate, EndDate,ReportingManager,Comments) 
    Values
    (@EID, @ProjectName, @ClientName, @Status, @StartDate, @EndDate,@ReportingManager,@Comments)
    END

如果 EID 不是整数类型,则必须在声明语句中指定该类型

备选方案 2:使用 SELECT 子句

    CREATE PROCEDURE [dbo].spInsertTaskAssignmentsample
    (
    @Username nvarchar(50),
    @ProjectName nvarchar(50),
    @ClientName nvarchar(50),
    @Status nvarchar(50),
    @StartDate nvarchar(50),
    @EndDate nvarchar(50),
    @ReportingManager nvarchar(50),
    @Comments nvarchar(100)
     )
    AS
    BEGIN

    INSERT INTO tblTaskAssignment
    (EID,ProjectName, ClientName, Status, StartDate, EndDate,ReportingManager,Comments) 
    SELECT TOP 1 EID, @ProjectName, @ClientName, @Status, @StartDate, @EndDate,@ReportingManager,@Comments FROM tblLogin WHERE Username=@Username 
    END
于 2013-06-12T10:51:11.423 回答
0

您可以使用代替SELECT 子句VALUES

INSERT INTO tblTaskAssignment
(EID,ProjectName, ClientName, Status, StartDate, EndDate,
    ReportingManager,Comments) 
SELECT top 1 EID, @ProjectName, @ClientName, @Status, @StartDate, @EndDate,
    @ReportingManager,@Comments
FROM tblLogin WHERE Username=@Username

tblLogin(尽管请注意,根据 Martin Smith 和 ta.speot.is 之间的讨论,这确实假设该匹配项中至少会有一行@Username

于 2013-06-12T10:54:46.460 回答