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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:09:29+00:00 2026-06-03T17:09:29+00:00

I have a table : id | Val ____________ 1 | a 2 |

  • 0

I have a table :

id | Val
____________
1  | a
2  | s
3  | e
4  | f
5  | h

I have a query which accepts 2 params :

@id1 int
@id2 int

This query has 2 internal variables :

@val1 int
@val2 int

I need to set values into those variables : like –

select @val1=val from where id=@id1
select @val2=val from where id=@id2

I want to do it with one query…. I’ve tried:

select @val1=val , @val2=val  
from  table
where id=@id2 or  id=@id1

The problem is :

@val1 should set to val only when id=@id1

and

@val2 should set to val only when id=@id2

(what about @val2 when id=@id1 ? what value will it get ?)

How can I write those 2 queries in 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. Editorial Team
    Editorial Team
    2026-06-03T17:09:31+00:00Added an answer on June 3, 2026 at 5:09 pm
    SELECT 
          @val1=CASE WHEN id=@id1 THEN Val ELSE @val1 END
        , @val2=CASE WHEN id=@id2 THEN Val ELSE @val2 END
    FROM table
    WHERE ID IN (@id1, @id2)
    

    To test it out:

    CREATE TABLE #tmp (ID INT, VAL CHAR(1))
    INSERT INTO #tmp VALUES (1,'a')
    INSERT INTO #tmp VALUES (2,'s')
    INSERT INTO #tmp VALUES (3,'e')
    INSERT INTO #tmp VALUES (4,'f')
    INSERT INTO #tmp VALUES (5,'h')
    
    DECLARE @id1 INT=1
    DECLARE @id2 INT=4
    
    DECLARE @val1 CHAR(1)
    DECLARE @val2 CHAR(1)
    
    SELECT 
          @val1=CASE WHEN id=@id1 THEN Val ELSE @val1 END AS Val1
        , @val2=CASE WHEN id=@id2 THEN Val ELSE @val2 END AS Val2
    FROM #tmp
    WHERE ID IN (@id1, @id2)
    
    SELECT @val1, @val2  --Returns a, f
    
    DROP TABLE #tmp
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table named OT Hours which has the following column EmpId, Date,
I have a table that looks like this: Month Site Val 2009-12 Microsoft 10
Say I have a query which returns the following result: | val | type
I have table in data base name train delay, with columns train number(int), DelayTime(int),
I have table A in Oracle that has a primary key (id). I need
I have table that I insert data with following query (from c# code): INSERT
Suppose I have a MySQL table called MyTable, that looks like this: +----+------+-------+ |
I'm having trouble trying to define the SQL query for this table: There's a
I have a table that looks like this one : +------+------+------------------+ | item |
I have a MySQL table with many numeric columns (some INT, some FLOAT). I

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.