기본 프로시저
프로시저 생성
- CREATE [or ALTER] PROCEDURE SP_SQUTY12PL
- @year nvarchar(8)
- , @season nvarchar(2)
- , @returnVal nvarchar(2000) OUTPUT
- AS
- BEGIN
- DECLARE
- @sql NVARCHAR(2000)
- set @sql = ' select * from squty02tt'
- set @returnVal = @sql
- END
프로시저 실행
- DECLARE @year nvarchar(8)
- DECLARE @season nvarchar(2)
- DECLARE @returnVal nvarchar(2000)
- set @year = '2012'
- set @season = 'F'
- exec dbo.SP_SQUTY12PL @year, @season, @returnVal OUTPUT
- select @returnVal
동적쿼리를 이용한 프로시저
- ALTER PROCEDURE SP_SQUTY12PL
- @year nvarchar(8)
- , @season nvarchar(2)
- AS
- BEGIN
- DECLARE
- @sql nvarchar(4000)
- , @param nvarchar(400)
- , @yy1 nvarchar(4)
- , @yy2 nvarchar(4)
- , @yy3 nvarchar(4)
- set @param = '@p_year nvarchar(8)
- , @p_season nvarchar(2)
- '
- set @sql = ' select top 20 * from squty02tt
- where year = @p_year and season = @p_season
- '
- --set @returnVal = @sql
- exec SP_EXECUTESQL @sql, @param, @p_year=@year, @p_season=@season
- END
execute procedure
- DECLARE @year nvarchar(8)
- DECLARE @season nvarchar(2)
- set @year = '2012'
- set @season = 'F'
- exec dbo.SP_SQUTY12PL @year, @season
댓글 없음:
댓글 쓰기