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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:44:21+00:00 2026-05-23T19:44:21+00:00

Here is a simplified version of a stored procedure I am trying to write

  • 0

Here is a simplified version of a stored procedure I am trying to write in SQL Server 2005.

My question is this:

I have declared a variable @docid to which I want to assign the results of a query as I Iterate through my cursor, if there is not an exact match, then I want to run another query and try for the next best match.

I tried putting in a test that says IF @docid IS NULL then BEGIN [second query]. This does not work since the variable @docid will not be Null it will just retain the previous value until the conditions for the first query are met and it is assigned a new value.

So how can I say “If I try to assign a docid to the variable @docid, but there is no match based on the conditions in the query then run the next query instead or set @docid=''"? Any help would be appreciated. Thanks

declare @docid bigint,
        @account varchar(30)

While @@fetch_status=0
BEGIN
   Select @Docid=Docid
   FROM printdata 
   WHERE Account=@account

   IF @docid Is Null --Docid Is never Null once assigned a value
   BEGIN
     Select @Docid=Docid
     FROM printdata 
     WHERE balance=@balance
  END
  • 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-23T19:44:21+00:00Added an answer on May 23, 2026 at 7:44 pm

    Well, I would just first initialize @docid to NULL – then it will be NULL if nothing is found:

    declare @docid bigint,
            @account varchar(30)
    
    While @@fetch_status=0
    BEGIN
       SET @docid = NULL
    
       SELECT @Docid = Docid
       FROM printdata 
       WHERE Account = @account
    
       IF @docid IS NULL  --  @Docid *WILL* be NULL now if nothing is found!
       BEGIN
         SELECT @Docid = Docid
         FROM printdata 
         WHERE balance = @balance
      END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a simplified version of what I'm trying to do : Before any other
Here is a simplified version of my database model. I have two tables: Image,
Just getting into SQL stored queries right now... anyway, here's my database schema (simplified
This is a simplified version of a class that I have in my project.
Here is a simplified version of my data. This is the result of a
I have a DataTables table. Here's a simplified version of what I have as
Simplified version of the problem: So I have this query which is inside a
Here is a simplified version of my application showing what I'm doing. /* in
Here's a simplified version of my class: public abstract class Task { private static
In my JSF/Facelets app, here's a simplified version of part of my form: <h:form

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.