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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:23:45+00:00 2026-05-26T06:23:45+00:00

I have 6 tables: LS_CLIENT_INSEE_A, B, C , etc. Each table contains only 1

  • 0

I have 6 tables: LS_CLIENT_INSEE_A, B, C , etc. Each table contains only 1 field: INSEE.

I just want to know if my input exists in one of these tables and return the corresponding letter from the table name (A,B,C etc).

Is there another why to rewrite this:

@TheInsee int
AS
BEGIN
declare @Zone char(1)

declare @CountA int
declare @CountB int
declare @CountC int
declare @CountD int
declare @CountF int
declare @CountP int

SELECT @CountA = COUNT(*) FROM LS_CLIENT_INSEE_A WHERE NO_INSEE = @TheInsee
SELECT @CountB = COUNT(*) FROM LS_CLIENT_INSEE_B WHERE NO_INSEE = @TheInsee
SELECT @CountC = COUNT(*) FROM LS_CLIENT_INSEE_C WHERE NO_INSEE = @TheInsee
SELECT @CountD = COUNT(*) FROM LS_CLIENT_INSEE_D WHERE NO_INSEE = @TheInsee
SELECT @CountF = COUNT(*) FROM LS_CLIENT_INSEE_F WHERE NO_INSEE = @TheInsee
SELECT @CountP = COUNT(*) FROM LS_CLIENT_INSEE_P WHERE NO_INSEE = @TheInsee

set @Zone = 
      CASE 
         WHEN @CountA >  0   THEN 'A'
         WHEN @CountB >  0   THEN 'B'
         WHEN @CountC >  0   THEN 'C'   
         WHEN @CountD >  0   THEN 'D' 
         WHEN @CountF >  0   THEN 'F' 
         WHEN @CountP >  0   THEN 'P'         
      END  

END

Is above query the best way of achieving this?

Thanks you in advance,
Stev

  • 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-26T06:23:46+00:00Added an answer on May 26, 2026 at 6:23 am

    This should work by stopping the select once it finds a hit, worst case will still be when it’s in none of them.

    Note: assuming SQL Server (difference being the TOP 1 compared to LIMIT 1):

     @TheInsee int
    AS
    BEGIN
    declare @Zone char(1)
    
    SELECT @Zone = 
          CASE 
             WHEN (SELECT TOP 1 1 FROM LS_CLIENT_INSEE_A WHERE NO_INSEE = @TheInsee) >  0   THEN 'A'
             WHEN (SELECT TOP 1 1 FROM LS_CLIENT_INSEE_B WHERE NO_INSEE = @TheInsee) >  0   THEN 'B'
             WHEN (SELECT TOP 1 1 FROM LS_CLIENT_INSEE_C WHERE NO_INSEE = @TheInsee) >  0   THEN 'C' 
             WHEN (SELECT TOP 1 1 FROM LS_CLIENT_INSEE_D WHERE NO_INSEE = @TheInsee) >  0   THEN 'D'
             WHEN (SELECT TOP 1 1 FROM LS_CLIENT_INSEE_F WHERE NO_INSEE = @TheInsee) >  0   THEN 'F'
             WHEN (SELECT TOP 1 1 FROM LS_CLIENT_INSEE_P WHERE NO_INSEE = @TheInsee) >  0   THEN 'P'       
          END  
    END
    

    The TOP 1 1 is a shortcut to prevent it from scanning the entire table which is important if NO_INSEE is not indexed and the table is large, it will stop when it gets the first hit and not continue to scan the entire table.

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

Sidebar

Related Questions

I have tables, seq, triggers, sp, types, oracle queues etc. I want to know
I have tables of data samples, with a timestamp and some data. Each table
I have tables linked by FK, I query on the first table using entity
In my database I have tables that define types for example Table: Publication Types
Our client's request is to have tables in PDF with rounded corner. I only
I have tables named: Posts_August_2011 Posts_September_2011 Posts_October_2011 Posts_November_2011 Posts_December_2011 Posts_January_2012 I want to create
I have the following tables created: Table #1: tbl_Connections USE [taaraf_db] GO SET ANSI_NULLS
I have tables: Teachers and Students. We have a SpecialProjects Table where Teachers and
I have tables named Events and Log. The Events table comprises of ID, Event
Greetings. I have files that contains eleven fields in the file name. Each file

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.