USING SUBQUERY IN INSERT STATEMENT

  • INSERT INTO tblProject

    (ProjectName, Description, RequestedBy, DeptID)

    VALUES (@ProjectName,@Description,@RequestedBy,(select deptID from Department where Department=@Dept) as @DeptID)

    The error I get is:

    Subqueries are not allowed in this context. Only scalar expressions are allowed

  • Do you have a question about the code?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • The error I get is :

    Subqueries are not allowed in this context. Only scalar expressions are allowed.

  • Franco_1 (1/6/2009)


    VALUES (@ProjectName,@Description,@RequestedBy,(select deptID from Department where Department=@Dept) as @DeptID)

    The source for an insert can be either VALUES or SELECT, not a combination.

    Try this:

    INSERT INTO tblProject (ProjectName, Description, RequestedBy, DeptID)

    SELECT @ProjectName,@Description,@RequestedBy, deptID

    FROM Department

    WHERE Department=@Dept

    Edit: Typo in select

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • You can't set variables within a statement like that. You could set the variable prior to the insert statement or, just do this:

    INSERT INTO tblProject

    (ProjectName, Description, RequestedBy, DeptID)

    SELECT @ProjectName,@Description,@RequestedBy,DeptID

    FROM Department where Department =@Dept

    ----------------------------------------------------The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood... Theodore RooseveltThe Scary DBAAuthor of: SQL Server 2017 Query Performance Tuning, 5th Edition and SQL Server Execution Plans, 3rd EditionProduct Evangelist for Red Gate Software

  • Thanks.

    It works when I use the INSERT and SELECT (as you suggested) in SQL Server Management Studio query pane.

    Visual Studio will not allow me to create a table adapter when I replace the VALUES with SELECT.:crying:

    Is it because @ProjectName,@Description,@RequestedBy is not part of the Department table?

    In SQL Server Management Studio query pane, I had to actually pass in string values for it to work, not variables.

  • I'm sure you can use a stored procedure. Just pass the parameters to one.

    ----------------------------------------------------The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood... Theodore RooseveltThe Scary DBAAuthor of: SQL Server 2017 Query Performance Tuning, 5th Edition and SQL Server Execution Plans, 3rd EditionProduct Evangelist for Red Gate Software

  • I guess

  • Franco_1 (1/6/2009)


    Is it because @ProjectName,@Description,@RequestedBy is not part of the Department table?

    In SQL Server Management Studio query pane, I had to actually pass in string values for it to work, not variables.

    The variables will work, as long as they've been declared before the insert.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply