Solved

select data field from a table, if exists

Posted on 2013-12-06
2
517 Views
Last Modified: 2013-12-06
I want to select clientrefid from table1 and if it does not return anything I want to select clientrefid from table2 in one store procedure. What should I do?


create procedure select_clientrefid
@clientrefid       varchar(7)

select clientrefid from table1

if return nothing

then select clientrefid from table2
0
Comment
Question by:VBdotnet2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 39701744
try


SELECT @clientid=clientid FROM Table1

IF @clientid IS NULL
BEGIN
      SELECT @clientid=clientid FROM Table2
END
0
 
LVL 17

Expert Comment

by:dbaSQL
ID: 39701826
create procedure procName (
 @clientrefid varchar(7)
)
as
set nocount on;

if exists(select 1 from table1 where clientrefid = @clientrefid)
begin
   select  clientrefid from table1
end
else
begin
   select clientrefid from table2
end

set nocount off

go
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Removing SCCM 2016 4 53
connection to SQL 2012 error in windows 10 18 49
Creating a View from a CTE 15 50
When are cursors useful? 8 62
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question