Преобразование запросов T-SQL в Oracle
Я пытался преобразовать некоторые запросы SQL Server, которые я написал, в Oracle. У меня есть свои оригинальные запросы SQL на pastebin здесь: https://pastebin.com/Ru19FCzG но я работаю через него по одному блоку за раз.
Вот блок, над которым я работаю в данный момент (самый важный) Original Query:
declare @name varchar(max) = 'SPRING 1 2017 RTP';
declare @term varchar(30), @session varchar(1), @loc varchar(2), @id nvarchar(255);
set @term = '17SP1';
set @session='1';
set @loc = 'RT';
set @id = CASE WHEN @session = '2'
then ('%-%-' + @loc + '[5-9]%-' + @term)
else ('%-%-' + @loc + '[0-4]%-'+ @term)
END;
select dr.crsmain_batch_uid [course_id], (us.firstname+' '+us.lastname)[instructor],
q.qtext_body[question], qr.answer_text[answer], ac.points[points]
into #survey_results
from clp_sv_question_response qr
join deployment_response dr on qr.deployment_response_pk1=dr.pk1
join deployment d on dr.deployment_pk1=d.pk1
join clp_sv_answer_choice ac on qr.clp_sv_answer_choice_pk1=ac.pk1
join clp_sv_subquestion sq on ac.clp_sv_subquestion_pk1=sq.pk1
join clp_sv_question q on sq.clp_sv_question_pk1=q.pk1
left join (
select c.batch_uid, u.firstname, u.lastname from course_users cu
join users u on u.pk1=cu.users_pk1
join course_main c on cu.crsmain_pk1=c.pk1
where (c.course_id like @id)
and cu.role='P' and u.user_id not like '%_admin') us on dr.crsmain_batch_uid=us.batch_uid
where d.name=@name
order by dr.crsmain_batch_uid, instructor,
q.qtext_body, qr.answer_text, ac.points;
Вот модифицированный запрос, который у меня еще не работает.
Oracle:
define name = 'SPRING 1 2017 RTP';
define term = '17SP1';
define session = '1';
define loc = 'RT';
define id = IF &session = '1'
then (course_id, '[a-z]{3,}|[0-9]{3}|'loc'[0-4]{1}[0-9]{1}'term,'i')
else (course_id, '[a-z]{3,}|[0-9]{3}|'loc'[5-9]{1}[0-9]{1}'term,'i')
END IF;
create table survey_results as
select dr.crsmain_batch_uid as "course_id", (us.firstname+' '+us.lastname) as "instructor",
q.qtext_body as "question", qr.answer_text as "answer", ac.points as "points"
from clp_sv_question_response as qr
inner join deployment_response as dr on qr.deployment_response_pk1=dr.pk1
inner join deployment as d on dr.deployment_pk1=d.pk1
inner join clp_sv_answer_choice as ac on qr.clp_sv_answer_choice_pk1=ac.pk1
inner join clp_sv_subquestion as sq on ac.clp_sv_subquestion_pk1=sq.pk1
inner join clp_sv_question as q on sq.clp_sv_question_pk1=q.pk1
left join (
select c.batch_uid, u.firstname, u.lastname from course_users as cu
join users as u on u.pk1=cu.users_pk1
join course_main as c on cu.crsmain_pk1=c.pk1
where (c.course_id regexp_like &id)
and cu.role='P' and u.user_id not like '%_admin') as "us" on dr.crsmain_batch_uid=us.batch_uid
where d.name=&name
order by dr.crsmain_batch_uid, instructor,
q.qtext_body, qr.answer_text, ac.points;
Любая помощь будет оценена.
1 ответ
Попробуйте использовать блок PL/SQL
Declare
name varchar2(20);
term varchar2(10);
session varchar2(1);
loc varchar2(2);
id varchar2(100)
Begin
name = 'SPRING 1 2017 RTP';
term = '17SP1';
session = '1';
loc = 'RT';
SELECT DECODE(session , '1', 'Then value', 'Else Value')
INTO ID
FROm DUAL;
ВАШ ОТДЫХ ЗАПРОСА
END: