|
此代码涉及一个递归存储过程调用和一种避免游标名称冲突的“不太好”的方法。最后,我不在乎它是否使用游标。只是在寻找最优雅的方法。我主要将其用作跟踪Stored. ^5 o1 Y3 l: B9 y4 S0 E
Proc层次结构的简单方法(无需购买产品)。我在“动态sql”中尝试了游标,但运气不佳。我想深入十个层次。6 n% [8 X* A/ B
所需的输出:' ]& B6 w/ }- V# K- Y) K1 L" D; v2 l
sp_Master_Proc_Name
6 c6 Y7 H, R$ Z- B-sp_Child_Proc_1_Name ! n) o$ v, a7 P, G
---- sp_Sub_Proc_1_Name
9 {5 ^3 ~8 v: u5 L' ]# q-sp_Child_Proc_2_Name
: M. c. r- u2 K0 b( l- K: x-sp_Child_Proc_3_Name
* @& ^! T4 i& n" W2 \它不是很漂亮,但是这里是代码(它没有按预期工作)* Z+ t! e9 w) D+ l. D/ \2 H
CREATE PROCEDURE SP_GET_DEPENDENCIES
' w a6 g1 m8 { (. f. v7 B2 v( R: ~$ U* N
@obj_name varchar(300),% s$ t$ g: p7 L! |/ `# O, }
@level int( B0 n0 W& \3 K
)
3 q! J+ Y9 C2 a! ? AS
! c* K& A9 |; I DECLARE @sub_obj_name varchar(300)
/ Q( t5 F8 W) `4 w IF @level = 1# I2 D* K& H2 b* G
BEGIN" o- s& F0 ?/ a4 D V% M) _
PRINT @obj_name
n/ V, P( J! `8 H7 o END8 U* p5 a8 w K4 ^& L/ r1 |
IF @level = 1* @+ P$ p6 ~; [! X! d! D
BEGIN
* e& V. s4 m( U# k" a( `/ U* M6 ^! g, d DECLARE the_cursor_1 CURSOR FOR & ^1 m" q+ i! b5 r5 u7 k$ r' Y; T
SELECT DISTINCT REPLICATE('--', @level) + ' ' + c.name FROM dbo.sysdepends a. ^# T8 ?' k) K) [8 g$ U
INNER JOIN dbo.sysobjects b ON a.id = b.id
; x7 ]# E; p% }9 ?0 n$ | INNER JOIN dbo.sysobjects c ON a.depid = c.id
) L5 v8 h- ^3 l, C WHERE b.name = @obj_name, Y5 Q; [2 y2 O1 z! T0 J
OPEN the_cursor_14 y$ D" @3 T6 D, ?' b
SET @level = @level + 1
' x, u0 y0 P1 T% n9 R9 o$ \! d FETCH NEXT FROM the_cursor_1 INTO @sub_obj_name 5 {. n# W5 d. W$ l5 ]
WHILE @@FETCH_STATUS = 0
6 w' Q3 @9 {8 V BEGIN
1 e2 [9 j) D5 f* c* U1 \ PRINT @sub_obj_name
@ q7 Q) a8 d( Z# z, Z EXEC SP_GET_DEPENDENCIES @sub_obj_name, @level
+ f; ?+ A5 Q1 ] u/ H/ j9 h FETCH NEXT FROM the_cursor_1 INTO @sub_obj_name 3 f! Y0 G# O% j
END
3 a+ ^% Z5 Q/ d$ ^ CLOSE the_cursor_1
6 M% q# ~3 }& I- s t) R DEALLOCATE the_cursor_1
5 y2 U. K7 o* A; C, _. x END S& D# H4 r" E7 |
IF @level = 2
$ p2 A$ l" r$ A# A BEGIN
6 Q+ \+ I# q+ s8 b DECLARE the_cursor_2 CURSOR FOR
: f3 ]" d/ \, f9 v# ^# E4 d SELECT DISTINCT REPLICATE('--', @level) + ' ' + c.name FROM dbo.sysdepends a/ Y! O6 K5 x# Y3 z+ |- `
INNER JOIN dbo.sysobjects b ON a.id = b.id
' J% W. \1 T) f( H+ v' T3 ^ INNER JOIN dbo.sysobjects c ON a.depid = c.id
R0 D8 w" X# q, |2 e0 v WHERE b.name = @obj_name
$ ^- F; s* v4 c1 a2 _ OPEN the_cursor_2
$ {9 ~* r% C7 k6 g) [ SET @level = @level + 13 M: X& ~; `2 { [4 M" n( t' C/ ^+ z
FETCH NEXT FROM the_cursor_2 INTO @sub_obj_name - \5 M+ w' C5 z
WHILE @@FETCH_STATUS = 0 " M2 S$ g. s, \7 J' Y) h; b$ U
BEGIN
9 r: K- L2 b& t% F5 Q8 b/ H S PRINT @sub_obj_name
) g6 i# k& \2 b& }" H9 S8 i EXEC SP_GET_DEPENDENCIES @sub_obj_name, @level
. m# @% ~1 L- ^ FETCH NEXT FROM the_cursor_2 INTO @sub_obj_name
0 H4 w4 q% ~* a! T END
9 f$ n; p5 c- ]7 Z6 e: @/ e/ ` CLOSE the_cursor_2
1 p4 m {' ]) @! E+ X DEALLOCATE the_cursor_2
1 V; @' S+ g+ A% [ M& q( ^ END
* {1 O; @/ r6 t) R" m, |! O IF @level = 3
0 ]5 i; B& i2 g+ n# }) W ]5 v BEGIN 9 [0 J2 B+ s/ w9 T X& ]
DECLARE the_cursor_3 CURSOR FOR
; L h: B3 `3 o9 A+ R) E SELECT DISTINCT REPLICATE('--', @level) + ' ' + c.name FROM dbo.sysdepends a
: u( ^* X1 \4 D( U, {; a3 t INNER JOIN dbo.sysobjects b ON a.id = b.id( p: k; {+ D' L6 K. f4 h
INNER JOIN dbo.sysobjects c ON a.depid = c.id5 r4 V, Y- |% j6 U1 R3 K
WHERE b.name = @obj_name
* t4 H5 T& l3 K' a" g$ ?+ v- _2 [, I OPEN the_cursor_3
8 E+ Y5 ~7 c$ B; Z SET @level = @level + 1
& v4 B' {1 M# L FETCH NEXT FROM the_cursor_3 INTO @sub_obj_name 9 ^, k1 f, V; o
WHILE @@FETCH_STATUS = 0
0 q3 f3 F* N1 R/ k v2 S' V# Q BEGIN
4 }5 _7 R' ]4 w5 ? PRINT @sub_obj_name4 o# Y1 S1 X: C" L5 `" |: S, J, J
EXEC SP_GET_DEPENDENCIES @sub_obj_name, @level
# y6 g- X; }6 U7 W- N FETCH NEXT FROM the_cursor_3 INTO @sub_obj_name
0 K! i( k+ F: P. |3 [ END. T& m4 n" O3 ^, m7 D6 H* V, h
CLOSE the_cursor_3
& i. a# O |2 |% T# ~2 k DEALLOCATE the_cursor_3- k. p0 Z- p S* _
END
7 n4 V( ?! Y# }& n& C2 k; |+ ^& ] ( K5 F: }! ]5 c* T$ ~+ X
解决方案:
3 B$ K& X2 w/ k- t5 E& s, O7 v & k: p; A6 {) g! ?+ N
/ H+ ?; ?: ~" ^ u$ O9 \0 H
$ K9 }! Z& R; y* A- |, a. P' G1 Q4 S
对于ms sql服务器,您可以使用CURSOR LOCAL,然后游标位于sproc调用的本地,您的代码将变得更加简单:6 l. j: N5 \8 C6 w y. F9 e
CREATE PROCEDURE uspPrintDependencies
" r& C* d( D& }2 Z(7 x3 G5 ?4 ?. k) K
@obj_name varchar(300),
# i& e- b5 I: R+ Z( z" F* G @level int8 A1 S1 R/ }0 ^% Q; G( o/ S+ P
)
2 K1 f7 Y8 N8 {# PAS
: A8 \$ f7 R+ w# o6 J$ K0 o$ zSET NOCOUNT ON
) ?; K! H) P) I( [0 BDECLARE @sub_obj_name varchar(300)1 S( ^, d3 d! d a6 W
if @level > 0 begin, f3 M E Q5 C& G' W
PRINT Replicate(' ',@level) + @obj_name
5 u( v \ s2 e" ]& G& ~end
2 P' N( z' Z, D9 Q* ?3 Felse begin+ c8 ^, M" \6 s5 j* _" l
PRINT @obj_name4 V# ~+ |3 x. e- w4 Z! y
end& t1 ?! i3 b3 T! d
DECLARE myCursor CURSOR LOCAL FOR
4 L" u y& J4 `+ s8 i5 W# R SELECT
6 p0 j+ k+ { h, B" ` DISTINCT c.name
0 y2 o* M7 Y8 A* H( L, A FROM dbo.sysdepends a
1 b4 |$ H" R) |1 [, B INNER JOIN dbo.sysobjects b ON a.id = b.id" T) E) n+ B; d6 r' K. l2 u- Y+ z
INNER JOIN dbo.sysobjects c ON a.depid = c.id$ I. R0 a0 B2 F$ {* C
WHERE b.name = @obj_name0 C8 `9 B7 \- ^" q
OPEN myCursor2 r) y8 A' R! ]3 E1 w
SET @level = @level + 14 g; H2 n/ F( i1 q6 ~* ]/ M
FETCH NEXT FROM myCursor INTO @sub_obj_name % }# M6 Y: O5 S3 F/ ?, b1 P/ D2 M* l1 D, ]
WHILE @@FETCH_STATUS = 0 BEGIN
+ p7 G* \3 s! Y% {1 e EXEC uspPrintDependencies @sub_obj_name, @level & g q1 t" F* |% X
FETCH NEXT FROM myCursor INTO @sub_obj_name
$ k& M- U, L9 f/ l; E y3 Z2 G* eEND( d: Q+ U- h- f) n
CLOSE myCursor2 s; ?7 e' N% W5 W& l
DEALLOCATE myCursor" |' R) M8 H' p, s3 g
GO |
|