combining fields from one select to update a single field in a separate statement.

Posted on 2005-04-19
Last Modified: 2010-03-19
i have a stored procedure trying to do the following:

SELECT field1, field2, field3...
FROM tableA
WHERE id = @id

Field1 = 1
Field2 = 0
Field3 = 1
Field4 = 0

If the field has a value of 1 Then I want to add that field # to my input into the next table

SET combinedvalue = @values
WHERE id = @someid

in this case, @values should equal 13  (because both field1 and field3 have values of 1)

Thanks in advance.
Question by:kojak33
    LVL 28

    Accepted Solution

    Try this:

    SELECT @Field1 = Field1, @Field2 = Field2, @Field3 = Field3...
    FROM TableA
    WHERE ID = @ID

    UPDATE TableB
    SET CombinedValue = ISNULL(CAST(NULLIF(Field1 * 1, 0) AS VARCHAR(2)), '') +
                                     ISNULL(CAST(NULLIF(Field2 * 2, 0) AS VARCHAR(2)), '') +
                                     ISNULL(CAST(NULLIF(Field3 * 3, 0) AS VARCHAR(2)), '') ...
    WHERE ID = @SomeID
    LVL 5

    Expert Comment

    Good answer, however you can do this in a single statement. No need to store the variables, assuming the the IDs are common.

    UPDATE b
    SET combinedvalue = ISNULL(CAST(a.Field1*1 AS VARCHAR), '')
            + ISNULL(CAST(a.Field1*2 AS VARCHAR), '')
            + ISNULL(CAST(a.Field1*3 AS VARCHAR), '')
            + ...
    FROM TableB b
            INNER JOIN TableA a
            ON a.ID = b.ID
    WHERE a.ID = @CommonID


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
    Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
    Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
    Viewers will learn how the fundamental information of how to create a table.

    760 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

    15 Experts available now in Live!

    Get 1:1 Help Now