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

  • SEARCH
  • Home
  • 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 4547878
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:09:05+00:00 2026-05-21T16:09:05+00:00

SELECT COUNT(Field1_) FROM ( SELECT Field1_ FROM Table1_ WHERE Field1_= @Field1 UNION ALL SELECT

  • 0
SELECT COUNT(Field1_) 
           FROM 
           (
               SELECT Field1_
               FROM Table1_
               WHERE Field1_= @Field1
           UNION ALL
               SELECT Field1_
               FROM Table2_
               WHERE DeliveryPoint_ = @Field1
           UNION ALL
               SELECT Table3_
               FROM GasSupplied_
               WHERE DeliveryPoint_ = @Field1
           UNION ALL
               SELECT Table4_
               FROM Gnsnominations_
               WHERE DeliveryPoint_ = @Field1
           UNION ALL
               SELECT Table5_
               FROM HourlyProfileReports_
               WHERE DeliveryPoint_ = @Field1
           )

And I have problems with this code. Please help me.

DECLARE
              CountedRows NUMBER;
            BEGIN
              SELECT COUNT(*) INTO CountedRows 
              FROM Profiles_ 
              WHERE Field1_ = @Param1 AND RowNum < 2;  
                IF(CountedRows > 0)
                THEN
                    UPDATE Profiles_ 
                    SET 
                        Field2_ = @Param2,
                        Field3_ = @Param3,
            Field4_ = @Param4 
                        WHERE Field1_ = @Param1;
                ELSE
                INSERT INTO Profiles_
                (
                    Field1_, 
                    Field2_,
                    Field3_,
                    Field4_
                ) 
                VALUES
                (
                    @Param1, 
                    @Param2,
                    @Param3,
                    @Param4                    );
                END IF;
              END;

Please help me. I read many books and tutorials but I can’t understand this.

  • 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-05-21T16:09:05+00:00Added an answer on May 21, 2026 at 4:09 pm

    Your first one needs a table alias

    SELECT COUNT(Field1_) 
               FROM 
               (
                   SELECT Field1_
                   <snip>
                   WHERE DeliveryPoint_ = @Field1
               ) AS foo
    

    Your second one should begin

                DECLARE CountedRows int;
    
    
                  SELECT CountedRows = COUNT(*) 
                  FROM Profiles_ 
                  WHERE Field1_ = @Param1 AND RowNum < 2;  
    

    To be syntactically correct. Also use END; not END IF; However it gives you a potential race condition. As you are on SQL Server 2008 you should look into MERGE for doing this kind of UPSERT

    As far as I can see the whole of the second one can be replaced with.

    MERGE INTO Profiles_
    USING (VALUES(@Param1_) )T(P1)
    ON Field1_ = P1
    WHEN MATCHED THEN
      UPDATE SET Field2_ = @Param2_,
                 Field3_ = @Param3_,
                 Field4_ = @Param4_
    WHEN NOT MATCHED BY TARGET THEN
      INSERT
      VALUES (P1, @Param2_, @Param3_, @Param4_);  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm executing the following query SELECT COUNT(*) FROM table WHERE field1='value' AND (field2 >=
Why sql bellow don't work? select a.field1, a.field2, a.field3, (select count(*) from table2 b
Here is a sample query: UPDATE `table1` SET `table1`.`field1` = (SELECT COUNT(*) FROM `table2`
It's easy to find duplicates with one field: SELECT email, COUNT(email) FROM users GROUP
SELECT COUNT(`t`.id) FROM `product` `t` INNER JOIN `category` `c1` ON ( `c1`.id ='15' )
SELECT * FROM( (SELECT count(DISTINCT RECEPIENT_ID) as NoOfUsers, TO_CHAR(ACTN_TAKE_DATA_TM,'YYYY-MM-DD') as accDate FROM ALRT_PLATFORM_ALRT_HSTRY where
TOPIC_COUNT_SQL = SELECT COUNT(*) FROM topics_topic WHERE topics_topic.object_id = maps_map.id AND topics_topic.content_type_id = %s
SQL : SELECT COUNT(usr.id) as `total_results` FROM users as usr LEFT JOIN profile as
Example: select count(*) from my table where column1 is not null and (column1 =
i develop this code: SELECT COUNT(NewEmployee.EmployeeID), NewEmployee.EmployeeId,EmployeeName FROM NewEmployee INNER JOIN NewTimeAttendance ON NewEmployee.EmployeeID

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.