Update with Sub Query

Posted on 2012-08-29
Last Modified: 2012-08-29
I am trying to update the Org_SN field with the data from the [Serial Number] field based on the WHERE clause (MFG = N'GEM') AND (Model = N'TREK')) but I get

Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.

SET              Org_SN =
SELECT      [Serial Number]
FROM         AmcharNFA 
WHERE     (MFG = N'GEM') AND (Model = N'TREK'))

Open in new window

Question by:skull52
    LVL 65

    Expert Comment

    by:Jim Horn
    Yes, and ...

    If you highlight the below fragment of your statement, it is returning more than one row.

    SELECT      [Serial Number]
    FROM         AmcharNFA 
    WHERE     (MFG = N'GEM') AND (Model = N'TREK')

    Open in new window

    Since you have it as a subquery setting a single value to the results of that subquery, then it has to return only one row / value.

    To help, if you change the SELECT [Serial Number] to SELECT *, it'll flush out any differences that are causing more than one row.
    LVL 65

    Accepted Solution

    If you are intending to update this for all values, perhaps this is what your want...

    UPDATE    N
    SET              Org_SN =  [Serial Number]
    FROM         AmcharNFA
    WHERE     (MFG = N'GEM') AND (Model = N'TREK'))

    Author Comment

    Yes it returns more than 1 row I want it to copy all the serial numbers from the  [Serial Number] field to the Org_SN field based on the WHERE clause (MFG = N'GEM') AND (Model = N'TREK')) maybe I am using t he wrong approach
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    I think you also want to read this article:

    now, here you want to update some column from other columns, but the same row?
    or from other rows?

    if it's from the same row: no need for a subquery: see jimhorn's syntax.
    if it's from other rows: please explain how the find the correct row
    if it's from several other rows , you need to do a SUM() or MAX(), for example)

    please clarify

    Author Comment

    That's exactly what I needed I should have got that, guess I was over thinking it.


    Author Comment

    Thanks for the links

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
    Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now