473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How To Write a Case Statment to check two tables

103 100+
I have a Invoice Number that can appear in either two tables (history = table A and current= table B) which have all the same fields, but not both tables at one time. I want to check If the invoice is in table A and if so then grab other fields, if not check table B. How would I write a Case statement of If Statement for this?
Jul 16 '12 #1
4 2205
Rabbit
12,516 Expert Mod 8TB
Outer join the two tables and use coalesce on the two fields to return the one with data.
Jul 16 '12 #2
Brian Connelly
103 100+
Do you have a small sample of how to do that? Im kind of new to advance SQL.
Jul 31 '12 #3
Rabbit
12,516 Expert Mod 8TB
It would be something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. tableA FULL OUTER JOIN tableB
  3. ON tableA.ID = tableB.ID
  4. WHERE COALESCE(tableA.ID, tableB.ID) = 12345
Jul 31 '12 #4
ariful alam
185 100+
You can check the following:
Expand|Select|Wrap|Line Numbers
  1. if exists (select * from tableA where id = 1234)
  2. begin
  3.    select * from tableA where id = 1234
  4. end
  5. else if exists (select * from tableB where id = 1234)
  6. begin
  7.    select * from tableB where id = 1234
  8. end
  9. else
  10. begin
  11.    select 'answer'=0
  12. end
Aug 4 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tom | last post by:
I would like to know if an .asp case statement can contain HTML elements. I am building an application that I would like to have dynamic choices. The dynamic part would be built in the a case...
6
by: deanfamily11 | last post by:
I've set up a case statement to have my program determine where on the Cartesian plane a point the user enters is located. I keep getting the C2051 error when I compile. Any help? #include...
2
by: Pierre | last post by:
Hello, I would like to write a regexp that can be either case sensitive or that can ignore the case based on a variable (value would be either 'i' or ''). For instance in the below code the...
16
by: Brian Tkatch | last post by:
Is there a way to check the order in which SET INTEGRITY needs to be applied? This would be for a script with a dynamic list of TABLEs. B.
5
by: vsteshenko | last post by:
Hello, This is my second post to the any usernet group and the first one was posted to the wrong one. I am currently working on creating an order form for sales associates at my work to be used...
2
by: rajakamaraj | last post by:
Hi I have a question in using case statement in Oracle.Like is it possible can we use queries in when clause and then clause in case statement. Like select AMOUNT1, (CASE WHEN (select amount1 from...
4
by: huzzaa | last post by:
I am using a switch statement that will decide from a random number what message to display. I want the random number to be between 0 and 100 and then if the number is say between 1 and 10 to...
1
by: buddi26 | last post by:
hi i need a code for subtables in drop down boxes with database. Exactly what i want is, if we select one value in drop down box, some sub values r coming in another drop down box with on change...
10
by: allik7 | last post by:
I am have a problem with this section of code. When I place the select statemnet in a string variable i get an syntax error at RTRIM. Can the Case section be used in a string variable i am wondering...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.