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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:45:36+00:00 2026-06-07T05:45:36+00:00

I am using a SQL Server 2005 database, and I have an If statement

  • 0

I am using a SQL Server 2005 database, and I have an If statement that is behaving in two different manners against the same data set, depending on the actions to be taken in the begin…end block.

First, if I merely want to print to the console, the following code prints nothing, as expected:

if (not exists(select null from tblControls where Name = 'SOME_CONTROL_NAME'))
begin
  print 'control not found'
end

Whereas this code prints ‘control found’ as expected:

if (exists(select null from tblControls where Name = 'SOME_CONTROL_NAME'))
begin
  print 'control found'
end

However, if I change the code to this:

if (not exists(select null from tblControls where Name = 'SOME_CONTROL_NAME'))
begin
  insert into tblControls values (632, 'NEW_CONTROL_NAME', 'New Control', 1, 1, NULL, 1, 'DataControls.CheckBox', NULL, NULL, 1) 
end

The insert statement ALWAYS fires, even though a matching record exists in tblControls. Is there something special about insert statements in T-SQL 2005 that could be causing this behavior, or am I missing something obvious? I would understand if the logic of the code is wrong, but when I tested using the print statement it works as expected.

EDIT: Its generating an “Insert Error” message.

Any help with this frustrating problem is greatly appreciated.

  • 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-07T05:45:37+00:00Added an answer on June 7, 2026 at 5:45 am

    It’s the NULL in the Exists..Select that’s confusing it. Try:

    if (not exists(select * from tblControls where Name = 'SOME_CONTROL_NAME'))
    begin
      insert into tblControls values (632, 'NEW_CONTROL_NAME', 'New Control', 1, 1, NULL, 1, 'DataControls.CheckBox', NULL, NULL, 1) 
    end
    

    Also, it can be rewritten without the parentheses and Begin/End:

    IF NOT EXISTS (SELECT * FROM tblControls WHERE Name = 'SOME_CONTROL_NAME') 
      INSERT INTO tblControls VALUES (632, 'NEW_CONTROL_NAME', 'New Control', 1, 1, NULL, 1, 'DataControls.CheckBox', NULL, NULL, 1) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using SQL-Server 2005. I have a dev and prod database that have
I have a SQL Server 2005 database that stores data for multiple users. Each
I have a web application, which is using a SQL Server 2005 database. My
Using SQL Server 2005 I have a query that gets child records from bundles
I have a Windows .net application using SQL Server 2005 Express as database, which
I am using SQL Server Express 2005. I have a single database myDB I
Using SSRS 2008R2 with a SQL Server 2005 database for the Data Source. I
I have an existing SQL Server 2005 database that runs our accounting/inventory application. We
I have a large (100+ tables) SQL Server 2005 database that I would like
We have a system that uses a SQL server 2005 database with a number

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.