Solved

select data field from a table, if exists

Posted on 2013-12-06
2
501 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now