Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8563521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:58:43+00:00 2026-06-11T16:58:43+00:00

Very new SQL Server, am using SQL server 2008. I have two tables, Table

  • 0

Very new SQL Server, am using SQL server 2008. I have two tables, Table A and Table B

I want to update Table A with the count of a matching strings of Table B. Here is what I came up by declaring a static varchar. I would like it to do it in a procedure for all Records in Table A anytime a record is inserted/updated in Table B.

TABLE A: **PO**, Count, Closed         Table B: **LD**
            24A, 0,     0,                      24A-1
            25A, 0,     0,                      24A-2
            26A, 0,     0,                      25A-3
                                                26A-1
                                                26A-2

Code I tried:

 Declare @POTableA AS VARCHAR(15)
 SET @POTableA = '24A'
   Update TABLE A
   SET TableA.Count =(Select Count(*) AS 'Count_LD' FROM TABLE B 
   WHERE TableB.LD LIKE '%'+@POTableA+'-%') 
   FROM TABLE B WHERE TABLEA.PO LIKE '%'+@POTableA+'%'

Current result:

TABLE A: **PO**, Count, Closed         Table B: **LD**
            24A, 2,     0,                      24A-1
            25A, 0,     0,                      24A-2
            26A, 0,     0,                      25A-3
                                                26A-1
                                                26A-2

Desired result:

TABLE A: **PO**, Count, Closed         Table B: **LD**
            24A, 2,     0,                      24A-1
            25A, 1,     0,                      24A-2
            26A, 2,     0,                      25A-3
                                                26A-1
                                                26A-2
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:58:44+00:00Added an answer on June 11, 2026 at 4:58 pm

    While I’m not sure why you need to keep the count stored in table A – you can always determine this count at runtime, then you don’t have to use triggers etc. to maintain that information (which is redundant). But here is a demonstration using table variables about how such an update with an aggregate could be achieved (there are other ways, likely).

    DECLARE @A TABLE(PO VARCHAR(32), [count] INT);
    
    DECLARE @B TABLE(LD VARCHAR(32));
    
    INSERT @A VALUES
    ('24A',0), ('25A',0), ('26A',0);
    
    INSERT @B VALUES
    ('24A-1'), ('24A-2'), ('25A-3'),
    ('26A-1'), ('26A-2');
    
    UPDATE T
    SET T.[count] = S.[count]
    FROM @A AS T
    INNER JOIN
    (
     SELECT A.PO, [count] = COUNT(B.LD)
       FROM @A AS A INNER JOIN @B AS B
       ON B.LD LIKE A.PO + '%'
       GROUP BY A.PO
    ) AS S
    ON T.PO = S.PO;
    
    SELECT PO, [count] FROM @A;
    

    Results:

    PO     count
    ----   -----
    24A    2
    25A    1
    26A    2
    

    Now if you need to do this in a trigger, it would be something like:

    CREATE TRIGGER dbo.MaintainRedundantCount
    ON dbo.TableB
    FOR INSERT, UPDATE
    AS
    BEGIN
      SET NOCOUNT ON;
    
      UPDATE T
       SET T.[count] = S.[count]
       FROM dbo.TableA AS T
       INNER JOIN
       (
         SELECT A.PO, [count] = COUNT(B.LD)
           FROM dbo.TableA AS A 
           INNER JOIN dbo.TableB AS B
           ON B.LD LIKE A.PO + '%'
           GROUP BY A.PO
       ) AS S
       ON T.PO = S.PO;
    END
    GO
    

    You might want to modify one of those lines to:

           ON B.LD LIKE A.PO + '-%'
    

    But since you only gave three sample values in the question, and they were all the exact same format, it’s tough to tell what your true data set looks like.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very very new to sql server. I want to have a table with
Here is one very interesting problem. I am using SQL Server 2008. I have
I'm very new to SQL Server. I'm using a cursor to populate a table
I am using Microsoft SQL Server 2008. I have a very large text file
I'm using a SQL Server 2008 Database Project, and I'm finding it's very cumbersome
I have an sql server 2008 db table that holds links to articles. My
I am very new to using SQL and I haven't been able to find
I have a install script that uses DATE. I'm running SQL Server 2008 R2,
I have a database called MyDB in a Microsoft SQL Server 2008 Express instance
I'm writing a stored procedure in SQL Server 2008, interatively. When using SQL Server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.