Home > database >  Whether can realize according to different conditions to perform SQL stored procedures
Whether can realize according to different conditions to perform SQL stored procedures

Time:10-09

Requirements: according to whether a table is empty, decided to perform different SQL statements
Whether can use SQL to achieve without stored procedure directly?

CodePudding user response:

My train of thought is directly using if.. The else..
 BEGIN 
IF (SELECT count (*) FROM t1)=0
THEN SELECT count (*) FROM t1;
The ELSE SELECT count (*) FROM t2;
END the IF;
END;

Perform error, IF grammar mistakes, to solve

CodePudding user response:

 
SELECT the IIF (COUNT (*)=0, (SELECT COUNT (*) FROM t1), (SELECT COUNT (*) FROM t2)) FROM t1

 
The SELECT (CASE WHEN the COUNT (*)=0 THEN COUNT (*) ELSE (SELECT COUNT (*) FROM t2) END) COUNT FROM t1

CodePudding user response:

 



Declare @ a int
The set @ a=1

If @ a=1
The begin
Select the 'a'
End


If @ a=2
The begin
Select 'b'
End


Is that ok?

CodePudding user response:

Thinking no problem of the building Lord, is the problem writing some
 IF 
(
SELECT COUNT (*) FROM t1
)=0
The BEGIN
SELECT COUNT (*)
The FROM t1;
END;

The ELSE
The BEGIN
SELECT COUNT (*)
The FROM t2.
END;

CodePudding user response:

reference floor 6 February 16 response:
thinking no problem of the building Lord, is the problem writing some
 IF 
(
SELECT COUNT (*) FROM t1
)=0
The BEGIN
SELECT COUNT (*)
The FROM t1;
END;

The ELSE
The BEGIN
SELECT COUNT (*)
The FROM t2.
END;


Write grammatical errors, I used the PGSQL

CodePudding user response:

Help you move to the pg SQL section,

CodePudding user response:

You need to use function (stored procedure), not a single statement
  • Related