MS-SQL > 저장 프로시져 - System 프로시져 - 내용보기
등록일 : 2017-07-03 19:28
조회수 : 92,924
SQL 서버에서 관리하는 모든 RULE, DEFAULT,VIEW,SP, Triger 등등,,,
이런 객체들은 모두 MASTER 데이타베이스의 syscomments라는 시스템 테이블에 저장되어 있으며,
클라이언트가 해당 오브젝트를 사용하는 쿼리를 서버에 보내면 서버에서 그 정의를 읽어서
처리하게 됩니다.
sp_helptext 시스템 프로시져는 syscomments 테이블에 저장된 오브젝트의 정의를 간편하게
보여주는 역할을 합니다.
아래는 sp_helptext의 간단한 문법을 보여주고 있습니다.
■ sp_helptext[@objname=] 'name'
|
여러분도 쿼리분석기를 읽어서 간단하게 테스트 해보시기 바랍니다.
처리결과를 보면 아시겠지만, 프로시져라는 것은 우리가 공부한 각종 Transact-SQL 문을 눈리적인
처리순서에 따라서 저장해 놓은것임을 알수 있습니다.
즉, 시스템 스토어드 프로시져의 정의를 분석해 보면 여러분이 사용자 정의 프로시져를 만들때
엄청난 도움이 될것 같다는 생각이 듭니다.
쿼리분석기를 열어서 아래처럼 실행해 보시기 바랍니다.
sp_detach_db 란 시스템 프로시져의 내용을 보는 예제 입니다.
sp_helptext sp_detach_db
create procedure sp_detach_db
@dbname sysname = null,
@skipchecks nvarchar(10) = null
as
declare @dbid int
declare @exec_stmt nvarchar(540)
if @dbname is null
begin
raiserror(15354,-1,-1)
return(1)
end
if lower(@skipchecks) <> N'true'
and lower(@skipchecks) <> N'false'
and @skipchecks is not null
begin
raiserror(15354,-1,-1)
return(1)
end
select @dbid = null
select @dbid = dbid from master.dbo.sysdatabases where name=@dbname
if @dbid is null
begin
raiserror(15010,-1,-1,@dbname)
return(1)
end
-- make sure not trying to detach within a transaction
if @@trancount > 0
begin
raiserror(226,-1,-1,'SP_DETACH_DB')
return(1)
end
-- run UPDATE STATISTICS on all tables in the database so they are current
-- when transferred to READONLY media
if lower(@skipchecks) <> N'true'
begin
print 'Running UPDATE STATISTICS on all tables'
select @exec_stmt = 'USE ' + quotename( @dbname , '[')
+ ' exec sp_updatestats '
exec (@exec_stmt)
end
select @exec_stmt = 'DBCC DETACHDB ('
+ quotename( @dbname , '[')
+ ')'
exec (@exec_stmt)
return (0) -- sp_detach_db
프로시져의 내용이 쫘~악! 나오는거 보이시져.... 딥따 신기한 함수도 많이 나오네여... 쩌~비
왜 이렇게 공부할게 많은지....
그럼, 필요할때 유용하게 사용하시길 바랍니다.