프로그램을 만들다보니
SP에 구조체로 값을 넘기고 싶은경우가 종종 생긴다.

보통 2가지의 경우를 사용하는데
파라메타를 데이터 갯수만큼 만들어서 보내는것과
텍스트로 만들어서 보낸 후 각 데이터별로 구분자를 기준으로 SP에서 잘라먹는 것이다.

하지만 MSSQL 2008에는 '사용자 정의 테이블 형식'이라는게 있다.

이걸 이용하면 SP에 구조체 형식으로 값을 넘길 수 있다.

사용자 정의 테이블 형식 생성 방법은 다음과 같이 TABLE 생성과 비슷하다.
CREATE TYPE [dbo].[FOO] AS TABLE(
[BAR01] [varchar](20) NOT NULL,
[BAR02] [numeric](3, 1) NOT NULL
)


SP에서는 다음과 같이 파라메타의 형식이 TABLE이 된다.
CREATE PROCEDURE UP_UPDATE_ FOO
@T_FOO [dbo].[FOO] READONLY  
AS
BEGIN
...
END

VB.NET에서 오출 방법은 SP호출과 비슷하다.
Dim DS_FOO As DataSet
Dim FOO_TABLE As DataTable = New DataTable("FOO")

Dim col As DataColumn
Dim row As DataRow

col = New DataColumn()
col.DataType = System.Type.GetType("System.String")
col.ColumnName = "BAR01"
col.ReadOnly = True
FOO_TABLE.Columns.Add(col)

col = New DataColumn()
col.DataType = System.Type.GetType("System.Decimal")
col.ColumnName = "BAR02"
col.ReadOnly = True
FOO_TABLE.Columns.Add(col)

DS_FOO = New DataSet()
DS_FOO.Tables.Add(FOO_TABLE)

Dim i As Integer
For i = 0 To 16
row = FOO_TABLE.NewRow()
row("BAR01") = STRINGDATA
row("BAR02") = NUMBERDATA
FOO_TABLE.Rows.Add(row)
Next i

Dim mCmd As New SqlCommand
mCmd.CommandType = CommandType.StoredProcedure
mCmd.CommandText = "UP_UPDATE_FOO"

mCmd.Parameters.AddWithValue("@T_FOO", FOO_TABLE)
mCmd.Parameters(0).SqlDbType = System.Data.SqlDbType.Structured

clsDB.Execute(mCmd)
mCmd.Dispose()

대충 이런 형식이다.

참고자료 : How to pass multiple records to a Stored Procedure


크리에이티브 커먼즈 라이센스
Creative Commons License

Posted by shiftkey

2009/10/13 10:14 2009/10/13 10:14
, ,
Response
No Trackback , No Comment
RSS :
http://shiftkey.org/rss/response/238


블로그 이미지

Shiftkey가 살아가는 이런 저런 이야기......

- shiftkey

Notices

Archives

Authors

  1. shiftkey

Calendar

«   2012/05   »
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

Site Stats

Total hits:
160856
Today:
131
Yesterday:
156