本人小白,沒寫過存盤程序。。請教各位大神 怎么把下面的SQL翻譯成存盤程序啊,謝謝啦!
uj5u.com熱心網友回復:
你說明一下你要干啥吧,就是實作的是什么功能,看著不難的我給你兩個例子,再寫個結構,一個是在where里加case的方法,一個是字串拼接
你的程式,第二個if里那個 + condition 我不知道condition是啥,是用戶輸入的東西?然后主條件里?也是個輸入嗎
最后else我看不懂,不怕你笑話這個語言我沒學過,不過我能看懂個大概
第一個是關于case在where里的拼接
with Core as
(select co.course_number,
(case
when termid=((substr('¶m1',1,4))-'1990') *100
then substr('¶m1',1,9)
when termid=((substr('¶m1',1,4))-'1990')*100+1
then 'S1'
when termid=((substr('¶m1',1,4))-'1990')*100+2
then 'S2'
when termid=((substr('¶m1',1,4))-'1990')*100+3
then 'Q1'
when termid=((substr('¶m1',1,4))-'1990')*100+4
then 'Q2'
when termid=((substr('¶m1',1,4))-'1990')*100+5
then 'Q3'
when termid=((substr('¶m1',1,4))-'1990')*100+6
then 'Q4'
end)
as Term,
co.course_name,cc.section_number,cc.teacherid,cc.studentid from
cc join courses co
on co.course_number=cc.course_number
and termid>=UPPER(
(select ((substr('¶m1',1,4))-'1990')*100 from dual))
group by co.course_number, co.course_name,cc.section_number,cc.teacherid,cc.studentid,
(case
when termid=((substr('¶m1',1,4))-'1990') *100
then substr('¶m1',1,9)
when termid=((substr('¶m1',1,4))-'1990')*100+1
then 'S1'
when termid=((substr('¶m1',1,4))-'1990')*100+2
then 'S2'
when termid=((substr('¶m1',1,4))-'1990')*100+3
then 'Q1'
when termid=((substr('¶m1',1,4))-'1990')*100+4
then 'Q2'
when termid=((substr('¶m1',1,4))-'1990')*100+5
then 'Q3'
when termid=((substr('¶m1',1,4))-'1990')*100+6
then 'Q4'
end)
order by course_number,section_number)
SELECT COURSE_NUMBER,TERM,COURSE_NAME,SECTION_NUMBER,TEACHERNAME,STUDENT_NUMBER,StudentName,HOME_ROOM,FATHERPHONE,FATHEREMAIL,MOTHERPHONE,MOTHEREMAIL FROM
(
select course_number,Term,course_name,section_number,t.lastfirst as TeacherName,student_number,s.lastfirst as StudentName,s.home_room,MF.CUST_FATHERMOBILEPHONE AS FATHERPHONE,MF.CUST_FATHEREMAIL AS FATHEREMAIL,MF.CUST_MOTHERMOBILEPHONE AS MOTHERPHONE,MF.CUST_MOTHEREMAIL AS MOTHEREMAIL
from students s
join core c
on s.id=c.studentid
and s.enroll_status='0'
join teachers t
on t.id=c.teacherid
join U_STUDENTSUSERFIELDS MF
on MF.studentsdcid=s.dcid
)
WHERE
course_NUMBER=UPPER('¶m2')
and
(
(case
when
'¶m3' is null
and 1=1
then 1 end )=1
or
(case
when '¶m3' is not null
and
(
section_number in (
select substr(regexp_substr(UPPER('¶m3'),
'[^,]+',
1,
level),
1
,40)
from dual
connect by level <= regexp_count(UPPER('¶m3'),
',') + 1)
)
then 1 end)=1
)
order by course_number,section_number,studentname,student_number
;
再來就是怎么拼接字串(帶輸入輸出的字串拼接):
if object_id('finaltest') is not null
drop proc finaltest
go
create proc finaltest
@student_number INT output,
@lastfirst varchar(50) output,
@input_grade int
as
declare @sql NVARCHAR(500)
set @sql=N'select @p=student_number,@q=lastfirst from students where grade_level=@i'
exec sp_executesql @sql,N'@p int output,@q varchar(50) output,@i int',
@p=@student_number output ,
@q=@lastfirst output,
@i=@input_grade
select @student_number,@lastfirst,@input_grade,@sql
go
---------------------------------------------below is out side--------------------------------------------------------
declare @student_number_out int,
@lastfirst_out varchar(50)
exec finaltest
@student_number=@student_number_out output,
@lastfirst=@lastfirst_out output,
@input_grade=9
select @student_number_out,@lastfirst_out
go
輸入輸出性我已經展示了,你說你要做存盤程序,我假設你需要進行回滾操作(rollback,撤銷修改)
那你需要在你的procedure里面加上begin transaction,這個叫開始一個存盤程序
最后運行完你要手動回顧這些修改,然后輸入rollback或者commit
begin transaction
Execute Tonytest ---這里是你自己做的程式 你想叫什么都行,上面下面分別是存盤程序的開始和結束
rollback transaction
begin transaction
Execute Tonytest
commit transaction
uj5u.com熱心網友回復:
另外你沒說運行環境,最好你能把這個問題要做什么敘述一遍,sql語言和其他語言不一樣,上學的時候C語言玩的也還可以但是這倆語言完全不一樣的思路。
C是從行列考慮,不停地fetch回圈
sql是一種集合陳述句,編程者考慮的是這一組目標資料具有的共同屬性,一切都從屬性邏輯出發,通過他們的特性進行編程。
所以在sql里面,不管是字串的拼接還是指標 都應該盡可能少的使用,sql這種程式如果你非要用游標來做的話效率非常沒有保障。
所以你可以說你的需求,讓我活著樓下的大神們用sql的邏輯給你編,不要在這里改C系的程式,完全不同的邏輯體系
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/106174.html
標籤:基礎和管理
上一篇:oracle 習題求完整的程式
