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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:27:14+00:00 2026-06-17T03:27:14+00:00

I am using SQL Server 2008 R2. I have the stored procedure like this

  • 0

I am using SQL Server 2008 R2. I have the stored procedure like this :

ALTER PROCEDURE [dbo].[GetZone]
    -- Add the parameters for the stored procedure here
@TheInsee int
AS
BEGIN
declare @Zone int

declare @CountA int
declare @CountB int
declare @CountC int
declare @CountD int
declare @CountN int
declare @CountP int
declare @CountN1 int
declare @CountN2 int
declare @CountN3 int
declare @CountN4 int
declare @CountN5 int
declare @CountN6 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 @CountN = COUNT(*) FROM LS_CLIENT_INSEE_N WHERE NO_INSEE = @TheInsee
SELECT @CountP = COUNT(*) FROM LS_CLIENT_INSEE_P WHERE NO_INSEE = @TheInsee

SELECT @CountN1 = COUNT(*) FROM LS_CLIENT_INSEE_N1 WHERE NO_INSEE = @TheInsee
SELECT @CountN2 = COUNT(*) FROM LS_CLIENT_INSEE_N2 WHERE NO_INSEE = @TheInsee
SELECT @CountN3 = COUNT(*) FROM LS_CLIENT_INSEE_N3 WHERE NO_INSEE = @TheInsee
SELECT @CountN4 = COUNT(*) FROM LS_CLIENT_INSEE_N4 WHERE NO_INSEE = @TheInsee
SELECT @CountN5 = COUNT(*) FROM LS_CLIENT_INSEE_N5 WHERE NO_INSEE = @TheInsee
SELECT @CountN6 = COUNT(*) FROM LS_CLIENT_INSEE_N6 WHERE NO_INSEE = @TheInsee

set @Zone = 
      CASE 
         WHEN @CountA >  0   THEN 1
         WHEN @CountB >  0   THEN 2
         WHEN @CountC >  0   THEN 3   
         WHEN @CountD >  0   THEN 4
         WHEN @CountN >  0   THEN 5
         WHEN @CountP >  0   THEN 6  
         WHEN @CountN1 >  0   THEN 7
         WHEN @CountN2 >  0   THEN 8
         WHEN @CountN3 >  0   THEN 9
         WHEN @CountN4 >  0   THEN 10
         WHEN @CountN5 >  0   THEN 11
         WHEN @CountN6 >  0   THEN 12   
         ELSE 1    
      END

RETURN @Zone  

END

It works fine but when the data of each table is very big, then it hang out because of time out. I wonder if I can simplify this stored procedure.

This stored procedure is very simple: I just give an input @TheInsee and it search in six tables when it found within.

Any help please ?

  • 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-17T03:27:15+00:00Added an answer on June 17, 2026 at 3:27 am

    First, since you are only checking if at least one row exists in each table there is no point in counting all the rows. Second, there is no point in querying all tables if you already found rows in a previous table.

    I would do something like this:

    set @Zone = case
        when exists (select * from dbo.LS_CLIENT_INSEE_A where NO_INSEE = @TheInsee) then 1
        when exists (select * from dbo.LS_CLIENT_INSEE_B where NO_INSEE = @TheInsee) then 2
        -- etc.
        else 1
        end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2008. I have an interesting scenario where a stored procedure
I have a problem when debugging stored procedure using SQL server Management Studio 2008.
I am using SQL Server 2008 and SSMS 2012. I have a stored procedure
I am currently using MS SQL Server 2008. I have a stored procedure that
I'm using SQL Server 2008 Express, and I have a stored procedure that do
I have a stored procedure that retrieves sensitive information from an SQL Server 2008
I have following code in my SQL Server 2008 R2 stored procedure. In that
I'm going to search my database (SQL Server 2008) using a stored procedure. My
I'm writing a stored procedure in SQL Server 2008, interatively. When using SQL Server
Ok I'm using SQL Server 2008 and have a table field of type VARCHAR(MAX)

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.