PL⼀pgSQL 创建表的时候,想先判断下如果表存在就不创建

2025-03-22 16:46:36
推荐回答(1个)
回答(1):

判断是否存在,如果不存在,再创建:
declare viewExist number;
begin
select count(1) into viewExist from user_views where VIEW_NAME='REPORT_APPOINTMENTSBYLOCATORS';
if viewExist=0 then
execute immediate
'CREATE VIEW REPORT_APPOINTMENTSBYLOCATORS
AS
SELECT appointmentID, startDateTime, endDateTime, grid, locator,
meetingAddress, contactNumber, company, contact, reasonForAppointment,
DtFirstCall, DtSecondCall,CustomerAnswerFirstCall, CustomerAnswerSecondCall,
IsMeetOnSite, DtLastModified, AppointmentStatus, IsLocatorArriveOnsite,
DtCompletedAt,DtLocatorArriveOnsite, RescheduledOn, RescheduledAt, IsRescheduled, city
FROM Appointment';
end if;
end;