exec 라는 명령어를 사용하면 됩니다.
아래는 간단한 예제 입니다.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
CREATE procedure p_GetFieldValue
(
@table NVARCHAR(100),
@value1 NVARCHAR(200),
@value2 NVARCHAR(200) = null,
@value3 NVARCHAR(200) = null,
@value4 NVARCHAR(200) = null
)
as
begin
declare @fields varchar(500)
declare @sql varchar(500)
set @fields = @value1 + N' as a'
if ( @value2 is not null )
begin
set @fields = @fields + N', ' + @value2 + N' as b'
end
if ( @value3 is not null )
begin
set @fields = @fields + N', ' + @value3 + N' as c'
end
if ( @value4 is not null )
begin
set @fields = @fields + N', ' + @value4 + N' as d'
end
set @sql = N'SELECT ' + @fields + N' FROM ' + @table
exec ( @sql )
end
아래는 간단한 예제 입니다.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
CREATE procedure p_GetFieldValue
(
@table NVARCHAR(100),
@value1 NVARCHAR(200),
@value2 NVARCHAR(200) = null,
@value3 NVARCHAR(200) = null,
@value4 NVARCHAR(200) = null
)
as
begin
declare @fields varchar(500)
declare @sql varchar(500)
set @fields = @value1 + N' as a'
if ( @value2 is not null )
begin
set @fields = @fields + N', ' + @value2 + N' as b'
end
if ( @value3 is not null )
begin
set @fields = @fields + N', ' + @value3 + N' as c'
end
if ( @value4 is not null )
begin
set @fields = @fields + N', ' + @value4 + N' as d'
end
set @sql = N'SELECT ' + @fields + N' FROM ' + @table
exec ( @sql )
end
반응형
'Dev > DBMS' 카테고리의 다른 글
xml -> sql (2) | 2003.08.29 |
---|---|
MSSQL에서 동적SQL을 구현(OUTPUT파라미터 있을때) (0) | 2003.07.23 |
isql.exe를 이용한 sql문서를 실행해 보자 (0) | 2003.07.18 |
mysql은 oledb안되남^^;; (3) | 2003.01.09 |
mysql에서 테이블 자세히(?) 보기 (3) | 2003.01.09 |