sqlserver2008 中使用 表值 参数

1.声明用户自定义表类型

CREATE TYPE [dbo].[pmodelTable] AS TABLE(
    [pmodel] [varchar](60) NULL  --要传入的列
)
GO

2.存储过程中使用自定义表类型

create proc P_GetPmidByPmodel
(
@pmodel pmodelTable readonly --自定义的表类型
)
as
begin
    select pmid from V_ProductModel v
    inner join @pmodel p on v.pmodel=p.pmodel
end
go

3.测试效果

declare @table as pmodelTable   
--模拟几条数据
insert into @table values('AUD1100')
insert into @table values('AUDA100')
insert into @table values('AUD8400')
insert into @table values('AUD8450')
exec P_GetPmidByPmodel @table

4.asp.net中使用带有表值参数的存储过程

       #region 根据pmodel 获取pmid
        /// <summary>
        /// 根据pmodel 获取pmid
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public DataSet GetPmidByPmodel(DataTable dt)
        {
            DataSet ds = new DataSet();
            using (SqlConnection connection = new SqlConnection(SqlHelper.connString))
            {
                SqlCommand command = new SqlCommand();
                command.Connection = connection;
                command.CommandText = "P_GetPmidByPmodel";
                command.CommandType = CommandType.StoredProcedure;
                SqlParameter param = command.Parameters.AddWithValue("@pmodel", dt); //对应存储过程里的参数
                param.SqlDbType = SqlDbType.Structured; //说明是特殊的数据类型
                param.TypeName = "pmodelTable"; //自定义的表类型的名称
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = command;
                da.Fill(ds);
            }
            return ds;
        }
        #endregion

 

posted @ 2013-05-02 09:46  划破黑夜  阅读(269)  评论(0编辑  收藏  举报