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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:34:48+00:00 2026-05-11T13:34:48+00:00

I’m struggling with a T-SQL procedure and I am hoping you can help. I

  • 0

I’m struggling with a T-SQL procedure and I am hoping you can help.

I need to know if

  1. A row exists in a table for a given ID
  2. If one (or more) does exist then the latest one has another ID set to 5.

So the first table we need to get the row out of has two relevant ID’s: The CaseID and LocationID, these are both integers. The second table has 1 relevant ID called StateID.

Currently I can get whether the row exists in a table part but soon as I try to do anything Enterprise Manager gives a syntax error before the END statement.

CREATE PROCEDURE [dbo].[HasActiveCase]  (     @LocationID INTEGER )  AS  DECLARE @CaseID AS INTEGER SELECT @CaseID=CaseID FROM dbo.Cases WHERE @LocationID=LocationID  SELECT CASE WHEN      @CaseID IS NULL  THEN      0 ELSE       -- do something here to check CaseEvents.StateID is not 5 (closed) END GO 

There probably is a way of getting what I need in a JOIN or something but I am a complete newbie here.

What would be the easiest (to understand) way of checking the StateID is not 5 and return the result as a true/false? (I know SQLServer doesn’t have a boolean type but does have a Bit type instead.)

Also on a matter of style: The values in the ID’s have a text-field associated with them – CaseEvents.StateID has the text ‘Closed’ for instance. Should I return values as the ID’s and then replace the ID in code or return the objects with the ID’s already replaced with the text? There will never be more than 20 or 30 results returned in a set and the table will never be very large as it’s taken 5 years to get 2000 results in it.

NOTE: Can’t use linq (or anything else .NETty) because this will be called from an VB6 program.

Update:

Only 1 case can be open at a time so only the most recent item would be of relevance.

The possible situations to have is:

  1. No case ever opened. This should return 0.
  2. A case has been previously opened, but is now closed. This too should return 0.
  3. An opened case exists. This should return 1.
  • 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. 2026-05-11T13:34:49+00:00Added an answer on May 11, 2026 at 1:34 pm

    I think this query will do what you are looking for; Note that your existing query has a bug in that if more than one case exists it will only check if whatever case happened to be selected by the initial query is closed (of course that is only true if it is possible to have more than one Case assigned to a particular location).

    SELECT @CaseID = dbo.Cases.CaseID FROM dbo.Cases     JOIN dbo.CaseEvents ON dbo.Cases.CaseEventID = dbo.CaseEvents.CaseEventID WHERE @LocationID = dbo.Cases.LocationID     AND 5 != dbo.CaseEvents.StateID  SELECT CASE WHEN @CaseID IS NULL THEN CAST(0 AS BIT) ELSE CAST(1 AS BIT) END AS CaseExists 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my XML file chapters tag has more chapter tag.i need to display chapters
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I need to clean up various Word 'smart' characters in user input, including but

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.