if exists(select * from sysobjects where name = 'CMD' and type = 'P')
drop procedure cmd
go
-- *********************************************************
-- サンプル ( 2 )
-- *********************************************************
CREATE PROCEDURE CMD
@COMMAND varchar(255),
@RET varchar(255) output
AS
DECLARE @STS int
exec @STS = master.dbo.xp_cmdshell @COMMAND
set @RET = '処理終了'
return @STS
go
' ******************************************************************************
' 検索処理
' ******************************************************************************
function fncAllSql()
Dim ErrCnt
' --------------------------------------------------------------------
' 接続文字列の取得
strConnection = GetConnectionString()
if strConnection = "" then
exit function
end if
' --------------------------------------------------------------------
' 出力エリアの初期化
Out.open()
' --------------------------------------------------------------------
' 接続オブジェクト作成
if not IsObject( cn ) then
Set cn = CreateObject("ADODB.Connection")
end if
' --------------------------------------------------------------------
' エラートラップ開始
on error resume next
' --------------------------------------------------------------------
' 接続の確立
cn.Open strConnection
if Err.Number <> 0 then
on error goto 0
exit function
end if
' --------------------------------------------------------------------
' 読み出し用のレコードセットオブジェクトを作成
if not IsObject( rs ) then
Set rs = CreateObject( "ADODB.Recordset" )
end if
if rs is nothing then
Set rs = CreateObject( "ADODB.Recordset" )
end if
' --------------------------------------------------------------------
' 入力した文字列を変数にセット
SqlQuery = Fld( "SQL" ).value
' --------------------------------------------------------------------
' SQLの実行
rs.Open SqlQuery, cn
if Err.Number <> 0 then
Out.write( "◆ " & Err.Description & "<br><br>" )
rs.Close
cn.Close
on error goto 0
exit function
end if
if rs is nothing then
Out.write( "◆ レコードセットオブジェクトが Nothing に設定されました<br><br>" )
Set rs = CreateObject( "ADODB.Recordset" )
cn.Close
on error goto 0
exit function
end if
if rs.State = 0 then
ErrCnt = 0
Do While rs.State = 0
set rs = rs.NextRecordset
if rs is nothing then
Set rs = CreateObject( "ADODB.Recordset" )
cn.Close
on error goto 0
Out.write( "◆ レコードセットオブジェクトが Nothing に設定されました<br><br>" )
exit function
end if
ErrCnt = ErrCnt + 1
if ErrCnt > 50 then
rs.Close
cn.Close
on error goto 0
Out.write( "◆ データを取り出す事ができませんでした<br><br>" )
exit function
end if
Loop
end if
' --------------------------------------------------------------------
' 表示処理
with ( Out )
Call OutTitlePrint( )
' --------------------------------------------------------------------
' テーブル開始
.write( "<TABLE border=1 cellpadding=5 bordercolordark=black bordercolorlight=silver id=SelectData>" )
' --------------------------------------------------------------------
' タイトルのセット
.write( "<th bgcolor=gray nowrap>No.</th>" )
Dim MaxColumn
MaxColumn = rs.fields.count
for i = 1 to MaxColumn
.write( "<th bgcolor=skyblue nowrap>" )
.write( Replace(rs.Fields( i-1 ).Name,"'", "'" ) )
.write( "</th>" )
Next
' --------------------------------------------------------------------
' 行を読み込む
Cnt = 0
Do while not rs.EOF
' --------------------------------------------------------------------
' 行開始
.write( "<TR>" & vbCrLf )
.write( "<TD nowrap bgcolor=gray>" & Cnt + 1 & "</TD>" )
' --------------------------------------------------------------------
' データのセット
for i = 1 to MaxColumn
select case rs.Fields( i-1 ).Type
case 128,205
.write( "<TD nowrap> </TD>" )
case else
.write( "<TD nowrap>" & rs.Fields( i-1 ).Value & " </TD>" )
end select
Next
' --------------------------------------------------------------------
' 行終了
.write( "</TR>" & vbCrLf )
Cnt = Cnt + 1
if Cnt >= eval( Fld("MaxRec").value ) then
Exit Do
end if
rs.MoveNext
Loop
' --------------------------------------------------------------------
' テーブル終了
.write( "</TABLE>" & vbCrLf )
' --------------------------------------------------------------------
' SQLの実行
set rs = rs.NextRecordset
if Err.Number <> 0 then
Out.write( "◆ " & Err.Description & "<br><br>" )
rs.Close
cn.Close
on error goto 0
.write( "</BODY><HTML>" )
if rs is nothing then
Set rs = CreateObject( "ADODB.Recordset" )
end if
exit function
end if
if rs is nothing then
Set rs = CreateObject( "ADODB.Recordset" )
cn.Close
on error goto 0
.write( "</BODY><HTML>" )
exit function
end if
if rs.State = 0 then
ErrCnt = 0
Do While rs.State = 0
set rs = rs.NextRecordset
if rs is nothing then
Set rs = CreateObject( "ADODB.Recordset" )
cn.Close
on error goto 0
.write( "</BODY><HTML>" )
exit function
end if
ErrCnt = ErrCnt + 1
if ErrCnt > 50 then
rs.Close
cn.Close
on error goto 0
Out.write( "◆ データを取り出す事ができませんでした<br><br>" )
exit function
end if
Loop
end if
Do while not rs.EOF and not rs.BOF
' --------------------------------------------------------------------
' テーブル開始
.write( "<TABLE border=1 cellpadding=5 bordercolordark=black bordercolorlight=silver id=SelectData>" )
' --------------------------------------------------------------------
' タイトルのセット
.write( "<th bgcolor=gray nowrap>No.</th>" )
MaxColumn = rs.fields.count
for i = 1 to MaxColumn
.write( "<th bgcolor=skyblue nowrap>" )
.write( Replace(rs.Fields( i-1 ).Name,"'", "'" ) )
.write( "</th>" )
Next
' --------------------------------------------------------------------
' 行を読み込む
Cnt = 0
Do while not rs.EOF
' --------------------------------------------------------------------
' 行開始
.write( "<TR>" & vbCrLf )
.write( "<TD nowrap bgcolor=gray>" & Cnt + 1 & "</TD>" )
' --------------------------------------------------------------------
' データのセット
for i = 1 to MaxColumn
select case rs.Fields( i-1 ).Type
case 128,205
.write( "<TD nowrap> </TD>" )
case else
.write( "<TD nowrap>" & rs.Fields( i-1 ).Value & " </TD>" )
end select
Next
' --------------------------------------------------------------------
' 行終了
.write( "</TR>" & vbCrLf )
Cnt = Cnt + 1
if Cnt >= eval( Fld("MaxRec").value ) then
Exit Do
end if
rs.MoveNext
Loop
' --------------------------------------------------------------------
' テーブル終了
.write( "</TABLE>" & vbCrLf )
set rs = rs.NextRecordset
if Err.Number <> 0 then
Out.write( "◆ " & Err.Description & "<br><br>" )
rs.Close
cn.Close
on error goto 0
.write( "</BODY><HTML>" )
if rs is nothing then
Set rs = CreateObject( "ADODB.Recordset" )
end if
exit function
end if
if rs is nothing then
Set rs = CreateObject( "ADODB.Recordset" )
cn.Close
on error goto 0
.write( "</BODY><HTML>" )
exit function
end if
if rs.State = 0 then
Do While rs.State = 0
set rs = rs.NextRecordset
if rs is nothing then
Set rs = CreateObject( "ADODB.Recordset" )
cn.Close
on error goto 0
.write( "</BODY><HTML>" )
exit function
end if
Loop
end if
Loop
' --------------------------------------------------------------------
' 接続を閉じる
rs.Close
cn.Close
' --------------------------------------------------------------------
' エラートラップ解除
on error goto 0
' --------------------------------------------------------------------
' 全体終了
.write( "</BODY><HTML>" )
end with
end function
DECLARE @RET1 varchar(255),@RET2 int
exec @RET2 = cmd 'dir *.bat',@RET1 output
select @RET1 as 引数の戻り値
select @RET2 as プロシージャの戻り値