Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 534
  • Last Modified:

select data field from a table, if exists

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
VBdotnet2005
Asked:
VBdotnet2005
1 Solution
 
YZlatCommented:
try


SELECT @clientid=clientid FROM Table1

IF @clientid IS NULL
BEGIN
      SELECT @clientid=clientid FROM Table2
END
0
 
dbaSQLCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now