Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Logic Error 1

Status
Not open for further replies.

jw2000

Programmer
Aug 12, 2005
105
US
I have the following logic in a SQL Stored Prodecedure. I want the stored procedure to return 'yes' if exists if false. The problem is if exists is true, select ... 2 returns no data. If this is the case (that select ... 2 returns no records), how can I return 'no' using the logic below?


if exists(select ... 1)

begin
select ... 2

end
else
begin
print 'yes'
select 'yes'
end

 
are (select ... 1) and (select ... 2) the same exact queries?

“I sense many useless updates in you... Useless updates lead to fragmentation... Fragmentation leads to downtime...Downtime leads to suffering..Fragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" --
 
No, (select...1) and (select...2) are different queries.
 
Then how do you expect it to work?
you have to add something like this
if exists(select ... 1) and exists(select ... 2)
begin
select ... 2
end
else
begin
print 'yes'
select 'yes'
end



or
if exists(select ... 1)
if exists(select ... 2)
begin
select ... 2
end
else
begin
print 'yes'
select 'yes'
end


“I sense many useless updates in you... Useless updates lead to fragmentation... Fragmentation leads to downtime...Downtime leads to suffering..Fragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" --
 
Code:
if exists(select ... 1)
   begin
       select ... 2
       if @@ROWCOUNT = 0
          begin
             print  'no'
             select 'no'
          end
   end
else
   begin
        print 'yes'
        select 'yes'
   end

Borislav Borissov
 
SQLDenis,

Your code works but unfortunately the logic doesn't apply here because (select ... 1) can exist without (select ... 2)


Borislav,

Your logic works except (select ... 2) returns an empty record and then also returns a 'no'. How can I just return a 'no' if select ... 2 returns no records?

 
I don't quite understand what you want to accomplish but this MIGHT be what you want

if exists(select ... 1)
begin
if exists(select ... 2)
begin
select ... 2
end
else
begin
print 'yes'
select 'yes'
end
end
else
begin
print 'yes'
select 'yes'
end


“I sense many useless updates in you... Useless updates lead to fragmentation... Fragmentation leads to downtime...Downtime leads to suffering..Fragmentation is the path to the darkside.. DBCC INDEXDEFRAG and DBCC DBREINDEX are the force...May the force be with you" --
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top