SQL Server 2008 是否提供任何用于表值参数的预定义表类型?
例如,如果我只想将整数列表作为表传入,并从我传入的其他参数中派生出必要的上下文,是否有适合的类型,还是必须创建它?
我有一个具有的大用户定义表类型变量129 Columns。
我将一次存储2000-3000 Records在此变量中,并将其传递给各种存储过程和函数以获取更多数据,并在相同类型的新变量中进行修改,然后将此新变量返回给源SP。(这是因为表类型参数只能以形式传递READONLY。
这是我的算法。
SP1
@tmp tableType
{
INSERT @tmp EXEC
SP2 (@tmp)
INSERT @tmp EXEC
SP3 (@tmp)
}
Run Code Online (Sandbox Code Playgroud)
哪一个我应该使用@table varible或#temp table
我已经开始在Sql Server 2k8中使用表值参数进行批处理操作.我很喜欢这个功能,感觉经过漫长的等待.
但是,为了从.Net代码传递TVP,构造SQLMetaData []然后在循环中填充值需要花费太多精力.
如何避免在同步中保留.Net代码中的Sql Server和SQLMetaData []对象中的用户定义类型?当我在SQL中更改类型定义时,没有简单的方法可以知道我在.Net的大量代码中使用该类型的所有内容.
Can .Net Reflection可以通过提供用户定义类型的名称来拯救程序员构建SQLMetadata,并通过提供对象数组来帮助填充数据.
考虑这个例子:
SqlMetaData[] tvp_TradingAllocationRule = new SqlMetaData[13];
try
{
tvp_TradingAllocationRule[0] = new SqlMetaData("ID", SqlDbType.UniqueIdentifier);
tvp_TradingAllocationRule[1] = new SqlMetaData("Name", SqlDbType.VarChar, 255);
tvp_TradingAllocationRule[2] = new SqlMetaData("Description", SqlDbType.VarChar, -1);
tvp_TradingAllocationRule[3] = new SqlMetaData("Enabled", SqlDbType.Bit);
tvp_TradingAllocationRule[4] = new SqlMetaData("Category", SqlDbType.VarChar, 255);
tvp_TradingAllocationRule[5] = new SqlMetaData("Custom1", SqlDbType.VarChar, 255);
tvp_TradingAllocationRule[6] = new SqlMetaData("Custom2", SqlDbType.VarChar, 255);
tvp_TradingAllocationRule[7] = new SqlMetaData("Custom3", SqlDbType.VarChar, 255);
tvp_TradingAllocationRule[8] = new SqlMetaData("CreatedBy", SqlDbType.VarChar, 20);
tvp_TradingAllocationRule[9] = new SqlMetaData("CreatedTS", SqlDbType.DateTime);
tvp_TradingAllocationRule[10] = new SqlMetaData("ModifiedBy", …Run Code Online (Sandbox Code Playgroud) 我有一个存储过程,它接受用户定义的表类型,并且用户定义的数据类型中所有列的默认值设置为 null。现在,我将一个包含较少列的 dataTable 从 C# 代码传递给存储过程,期望剩余列的值将设置为 null。
但我收到此错误:尝试传递具有 21 列的表值参数,而相应的用户定义表类型需要 77 列。这是代码
SqlConnection conn = new SqlConnection("server=****; database=***;integrated security=SSPI");
DataSet dataset=new DataSet();
conn.Open();
SqlCommand cmd = new SqlCommand("Insert");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
SqlParameter para = new SqlParameter();
para.ParameterName = "@TableVar";
para.SqlDbType = SqlDbType.Structured;
//SqlParameter para=cmd.Parameters.AddWithValue("@TableVar",table);
para.Value = table;
cmd.Parameters.Add(para);
cmd.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud) 如果有的话,我可以在使用Mono时将表值参数传递给查询吗?(我目前正在使用版本2.10.12)
似乎Mono中的SqlDbType枚举尚未扩展以匹配Microsoft的枚举实现,SqlParameter类也未更新为包含TypeName属性.
关于我的背景和我的问题:
非常感谢您对这个问题的任何帮助!
我写这段代码
SELECT tblprsn.prsnid, tblprsn.name
FROM tblprsn LEFT OUTER JOIN
(
SELECT tblrtm.rtmid
FROM dbo.getrtmn(tblprsn.prsnid) as getrtmn_1
) AS tblgetrtmn
ON tblprsn.prsnid = tblgetrtmn.rtmid
Run Code Online (Sandbox Code Playgroud)
dbo.getrtmn是一个表值函数,并有一个名为rtmid的uniqueidentifier字段.prsnid是uniqueidentifier
当我运行此SQL查询时导致错误:
无法绑定多部分标识符"tblprsn.prsnid".
我使用以下将Excel文件导入SQL Server.
Excel文件的所有值都是字符串.我能够导入除了文件Barcode,SalePrice和Price2.我收到错误
nvarchar值'3001822585'(条形码)的转换溢出了一个int列
码:
SqlCommand sqlcmd = new SqlCommand
(@"MERGE Inventory AS target
USING (SELECT
LocalSKU, ItemName, QOH, Price, Discontinued,Barcode, Integer2
,Integer3, SalePrice, SaleOn, Price2 FROM @source)
AS Source ON (Source.LocalSKU = target.LocalSKU)
WHEN MATCHED THEN
UPDATE
SET ItemName = source.ItemName,
Price = source.Price,
Discontinued = source.Discontinued,
Barcode = source.Barcode,
Integer2 = source.Integer2,
Integer3 = source.QOH,
SalePrice = source.SalePrice,
SaleOn = source.SaleOn,
Price2 = source.Price2;", sqlconn);
SqlParameter param;
param = sqlcmd.Parameters.AddWithValue("@source", dr);
param.SqlDbType …Run Code Online (Sandbox Code Playgroud) 我有一个表值函数,它返回一组id数据.我想把函数放在我的查询中;
SELECT *
FROM caritbl
WHERE cari_id IN (dbo.fn_AtamaliCariListe(37))
Run Code Online (Sandbox Code Playgroud)
有什么建议?
编辑:我正在使用SQL Server
sql sql-server user-defined-functions table-valued-parameters
当必须使用 Dapper 将数据表作为 TVP 传递给 DB 时DataTable.AsTableValuedParameter,返回 a的方法有什么意义SqlMapper.ICustomQueryParameter?
我可以像普通数据表一样将 TVP 发送到 DB 并执行查询就好了。我不确定AsTableValuedParameter在它上面执行什么额外购买。
例如,这有效:
int rowsAffected = await conn.ExecuteAsync(
"MyProc",
new
{
myVar = (DataTable)GetThatDataTable(),
},
commandType: CommandType.StoredProcedure);
Run Code Online (Sandbox Code Playgroud)
另外,还有一个问题,方法中的typeName可选参数需要什么AsTableValuedParameter?没有它也能正常工作。
我有一个加载、转换和计算数据的 python 脚本。在 sql-server 中有一个存储过程,它需要一个表值参数、2 个必需参数和 2 个可选参数。在 sql server 中,我可以调用这个 SP:
USE [InstName]
GO
DECLARE @return_value int
DECLARE @MergeOnColumn core.MatchColumnTable
INSERT INTO @MergeOnColumn
SELECT 'foo.ExternalInput','bar.ExternalInput'
EXEC @return_value = [core].[_TableData]
@Target = N'[dbname].[tablename1]',
@Source = N'[dbname].[table2]',
@MergeOnColumn = @MergeOnColumn,
@Opt1Param = False,
@Opt2Param = False
SELECT 'Return Value' = @return_value
GO
Run Code Online (Sandbox Code Playgroud)
经过全面搜索,我找到了以下帖子:
如何使用需要用户定义类型表参数的 SQLAlchemy 调用存储过程
它建议使用 PYTDS 和 sql-alchemy 的方言“sql alchemy pytds”来调用具有表值参数的 SP。通过这篇文章和文档,我创建了以下 Python 脚本:
import pandas as pd
import pytds
from pytds import login
import sqlalchemy as sa
from …Run Code Online (Sandbox Code Playgroud) python sql-server sqlalchemy table-valued-parameters python-db-api
sql ×5
sql-server ×4
c# ×3
.net ×1
ado.net ×1
dapper ×1
mono ×1
python ×1
reflection ×1
sql-update ×1
sqlalchemy ×1