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 5844309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:16:28+00:00 2026-05-22T12:16:28+00:00

CREATE PROCEDURE [dbo].[SCD1] AS — SLOWLY CHANGING DIMENSION’S 1 (SCD1) — DROP PROCEDURE SCD1

  • 0
CREATE PROCEDURE [dbo].[SCD1] AS
-- SLOWLY CHANGING DIMENSION'S 1 (SCD1)
-- DROP PROCEDURE SCD1
-- EXEC SCD1
SET NOCOUNT ON
BEGIN
    --INSERT OF NEW SOURCE VALUES INTO TEMP TABLE

    SELECT SRC.* INTO #TEMP
    FROM SRC_CUST SRC
    LEFT OUTER JOIN DIM_CUST TGT ON SRC.CUSTOMERID = TGT.CUSTOMERID
    WHERE TGT.CUSTOMERID IS NULL

    --INSERT RECORDS THAT NEEDS TO BE UPDATED INTO #TEMP1 TABLE
    SELECT SRC.* INTO #TEMP1
    FROM SRC_CUST SRC
    INNER JOIN DIM_CUST TGT ON SRC.CUSTOMERID = TGT.CUSTOMERID
    WHERE (TGT.COMPANYNAME <> SRC.COMPANYNAME
       OR TGT.CONTACTNAME <> SRC.CONTACTNAME
       OR TGT.CONTACTTITLE <> SRC.CONTACTTITLE
       OR TGT.ADDRESS <> SRC.ADDRESS
       OR ISNULL(TGT.CITY,'UNK') <> SRC.CITY
       OR TGT.REGION <> SRC.REGION
       OR TGT.POSTALCODE <> SRC.POSTALCODE
       OR TGT.COUNTRY <> SRC.COUNTRY
       OR TGT.PHONE <> SRC.PHONE
       OR TGT.FAX <> SRC.FAX)

    --CHECK FOR THE EXISTENCE OF VALUES IN THE #TEMP TABLE
    IF EXISTS(SELECT COUNT(1) FROM #TEMP)
    BEGIN
       --INSERT NEW RECORDS INTO THE TARGET TABLE
       INSERT INTO DIM_CUST
          SELECT SRC.* FROM #TEMP SRC

       DROP TABLE #TEMP
       PRINT 'NEW RECORDS INSERTED'
    END
    ELSE
       DROP TABLE #TEMP 

    PRINT 'NO NEW RECORDS TO INSERT';

    IF EXISTS(SELECT COUNT(1) FROM #TEMP1)
    BEGIN
       --CHECK FOR THE EXISTENCE OF VALUES IN THE #TEMP1 TABLE  
       -- UPDATES THE RECORDS INTO THE TARGET TABLE
       UPDATE TGT
       SET  TGT.COMPANYNAME = SRC.COMPANYNAME
           ,TGT.CONTACTNAME = SRC.CONTACTNAME
           ,TGT.CONTACTTITLE = SRC.CONTACTTITLE
           ,TGT.ADDRESS = SRC.ADDRESS
           ,TGT.CITY = SRC.CITY
           ,TGT.REGION = SRC.REGION
           ,TGT.POSTALCODE = SRC.POSTALCODE
           ,TGT.COUNTRY = SRC.COUNTRY
           ,TGT.PHONE = SRC.PHONE
           ,TGT.FAX = SRC.FAX
      FROM DIM_CUST TGT
      INNER JOIN #TEMP1 SRC ON TGT.CUSTOMERID = SRC.CUSTOMERID

      DROP TABLE #TEMP1
      PRINT 'UPDATED RECORDS'
    END
    ELSE
        DROP TABLE #TEMP1

    PRINT 'NO RECORDS THERE TO UPDATE'
END

The problem that I get when execute this stored procedure is that it goes into the else part as well even though the if condition is satisfied. Can any one help me debug this stored procedure.

The source table that I have taken is the Customer table in the Northwind database.

Thanks.

  • 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-22T12:16:28+00:00Added an answer on May 22, 2026 at 12:16 pm

    There are 2 problems

    You need to BEGIN/END the ELSE clauses too. Only the DROP is being excecuted in the ELSE : the PRINT *always” is which makes it run like you’ve reported

    ...
    ELSE
    BEGIN
        DROP TABLE #TEMP 
        PRINT 'NO NEW RECORDS TO INSERT';
    END
    
    ...
    ELSE
    BEGIN
        DROP TABLE #TEMP1
        PRINT 'NO RECORDS THERE TO UPDATE'
    END
    

    Secondly, these are always true

    IF EXISTS(SELECT COUNT(1) FROM #TEMP)
    ...
    IF EXISTS(SELECT COUNT(1) FROM #TEMP1)
    

    All you need is.

    IF EXISTS(SELECT * FROM #TEMP)
    

    See these answers from me to explain why:

    • Does COUNT(*) always return a result?
    • What's the best to check if item exist or not: Select Count(ID)OR Exist(…)?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a stored procedure: CREATE PROCEDURE [dbo].[TestProc] AS BEGIN SET NOCOUNT ON; create
I have the following SP: CREATE PROCEDURE [dbo].[sp_LockReader] AS BEGIN SET NOCOUNT ON; begin
you have this procedure CREATE PROCEDURE dbo.test1 AS BEGIN begin transaction begin try exec
I have this proc: Create PROCEDURE [dbo].[myProc] @TableName nvarchar(100), @RowID int AS BEGIN SET
I have this SQL now: CREATE PROCEDURE dbo.sel_Track_HitsLast30Days( @projectID int ) AS BEGIN DECLARE
CREATE PROCEDURE dbo.InsertInboxMessage @UserID uniqueIdentifier @Message nvarchar(Max) AS INSERT INTO Messages(UsersID, Messages) VALUES(@UserID, @Message)
I tried to write such kind of code as CREATE PROCEDURE DBO.GENCODE AS BEGIN
a stored procedure that return multiple results: CREATE PROCEDURE [dbo].[GetMultipleTable] AS BEGIN if exists
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create PROCEDURE [dbo].[SD_Sproc_Insurance_Insert] -- Add the
CREATE PROCEDURE [dbo].[PL_GEN_PROVN_NO1] @GAD_COMP_CODE VARCHAR(2) =NULL, @@voucher_no numeric =null output AS BEGIN DECLARE @NUM

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.