Solved

select data field from a table, if exists

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

770 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