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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:18:01+00:00 2026-05-22T23:18:01+00:00

I have this query in my sql if (select count(*) from sys.columns where object_id

  • 0

I have this query in my sql

if (select count(*) from sys.columns where object_id = (select object_id from sys.tables where name = 'CLIENT_STATUS')) = 4
    insert into CLIENT_STATUS select 'NA', 'Inactive', 0, 0    --old version
else 
    insert into CLIENT_STATUS select 'NA', 'Inactive', 0, 0, 1 --new version

The result of select count(*) from sys.columns where object_id = (select object_id from sys.tables where name = 'CLIENT_STATUS') is 4, however it always appears to be executing the 5 parameter else version of the query.

What am I doing wrong with my if statement?

UPDATE:

It appears to be running both statments because if I do

if (select count(*) from sys.columns where object_id = (select object_id from sys.tables where name = 'CLIENT_STATUS')) = 5
    insert into CLIENT_STATUS select 'NA', 'Inactive', 0, 0, 1 --new version
else 
    insert into CLIENT_STATUS select 'NA', 'Inactive', 0, 0    --old version

I get the same error but now it says it is doing the first statement.

UPDATE2:
Mikael Eriksson had the correct answer, I changed my code to this to fix it.

if ((select count(*) from sys.columns where object_id = (select object_id from sys.tables where name = 'CLIENT_STATUS')) = 5)
    execute ('insert into CLIENT_STATUS select ''NA'', ''Inactive'', 0, 0, 1') --new version
else
    execute ('insert into CLIENT_STATUS select ''NA'', ''Inactive'', 0, 0')    --old version
  • 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-22T23:18:02+00:00Added an answer on May 22, 2026 at 11:18 pm

    You get the error when SQL Server compiles your statements.

    With this table

    create table TestTable(ID int)
    

    Try to run this statement

    if 1 = 1
      insert into TestTable values (1)
    else
      insert into TestTable values(1, 2)  
    

    Result:

    Msg 213, Level 16, State 1, Line 4
    Column name or number of supplied values does not match table definition.
    

    Clearly the second statement will never be executed but it will be compiled.

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

Sidebar

Related Questions

I have this SQL query: SELECT * FROM IMAGES WHERE IMAGENAME in ('IMG1', 'IMG2',
I seem to have a problem with this SQL query: SELECT * FROM appts
Right now, I have a SQL Query like this one: SELECT X, Y FROM
I have this query in SQL Server 2005: SELECT J.JobID, dbo.tblCustomers.Name AS CustomerName, J.CustomerJobNumber,
I have this sql query retrieving IDs from a table with 3 columns: ID,
I have this SQL query: SELECT c.id as ID, c.class_name as CLASS, COUNT(e.student_id) AS
if I have this SQL query: select substring(id for 2) as key, yw, count(*)
I have this query in sql server 2000: select pwdencrypt('AAAA') which outputs an encrypted
I have a problem with an SQL query on Postgresql. This select clause is
I have a MySQL query like this: SELECT *, SUM(...some SQL removed for brevety)

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.