您的位置:首页 > 数据库

sql server 表值类型的使用

2016-01-25 09:48 405 查看
如果你需要向存储过程中传递一个table 那么就用到了表值类型

1.创建表值类型

CREATE TYPE [dbo].[StorePreSeparationAmount] AS TABLE(
[preSeparationId] [int] NOT NULL,
[preSeparationAmount] [decimal](18, 2) NOT NULL,
[userCode] [varchar](20) NOT NULL
)
GO


  

2.创建存储过程

CREATE PROC sp_preSeparationCommit
(
@spsa StorePreSeparationAmount READONLY
)
AS
UPDATE ps
SET ps.preSeparationAmount=spsa.preSeparationAmount,
ps.updater=spsa.userCode,
ps.updateTime=GETDATE()
FROM dbo.wms_preSeparation ps
INNER JOIN @spsa spsa ON ps.preSeparationId=spsa.preSeparationId


3.Ado.net 使用表值类型

public bool updatePreSeparationAmount(DataTable dtStorePreSeparationAmount)
{
string strSpName = @"sp_preSeparationCommit";
SqlParameter[] pars =
{
new SqlParameter("@spsa",SqlDbType.Structured),
};

pars[0].Value = dtStorePreSeparationAmount;
pars[0].TypeName = "StorePreSeparationAmount";

int num = DalBase.ExecuteNonQuery(CommandType.StoredProcedure, strSpName, pars);
if (num > 0)
{
return true;
}
else
{
return false;
}
}


  备注:table创建

DataTable dtStorePreSeparationAmount = new DataTable("dtStorePreSeparationAmount");
dtStorePreSeparationAmount.Columns.Add("preSeparationId", Type.GetType("System.Int32"));
dtStorePreSeparationAmount.Columns.Add("preSeparationAmount", Type.GetType("System.Decimal"));
dtStorePreSeparationAmount.Columns.Add("userCode", Type.GetType("System.String"));

DataRow drNew = dtStorePreSeparationAmount.NewRow();
drNew["preSeparationId"] = nPreSeparationId;
drNew["preSeparationAmount"] = dPreSeparationAmount;
drNew["userCode"] = login.UserName;
dtStorePreSeparationAmount.Rows.Add(drNew);


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: