函数在SQL中算是比较常用的了 当中分为几种 最常用的自定义函数有两种 标量函数和表值函数
标量函数
先建个函数
CREATE FUNCTION [dbo].[ScalarSUM]
(
@int int,
@int2 int
)
RETURNS int
AS
begin
RETURN @int*@int2
end
然后用生成工具生成代码
public class ScalarSUM:BaseEntity
{
public readonly static String TABLENAME = "dbo.ScalarSUM";
private static ScalarSUM ___singleton;
public static ScalarSUM Instance()
{
if(___singleton==null)
{
___singleton = new ScalarSUM();
}
return ___singleton;
}
private ScalarSUM()
{
}
public override string ___GetTableName() { return TABLENAME; }
}
代码中
var s = RJ.DModel.Product.SelectSqlSectionFor;
s.AddColumn<ScalarSUM>((a, b) => b.Function(a.ProductID,5));
var s = RJ.DModel.Product.SelectSqlSectionFor;
s.Where<ScalarSUM>((a, b) => a.ClickDegree_int==b.Function(a.ProductID,5));
生成SQL
SELECT [dbo].ScalarSUM([Product].[ProductID],@pl6vs9d4t0jo6vd) FROM [Product]
SELECT * FROM [Product] WHERE [Product].[ClickDegree_int] = dbo.ScalarSUM([Product].[ProductID],@pk5eam29lqp6ula)
表值函数
CREATE FUNCTION [dbo].[ProductTypeFunctionName]
(
@ProductTypeID int
)
RETURNS TABLE
AS
RETURN
(
with a as
(
select ProductTypeID,ProductTypeParentID from ProductType
where ProductTypeID=@ProductTypeID
UNION ALL
select q.ProductTypeID ,q.ProductTypeParentID from ProductType q,a where q.ProductTypeParentID=a.ProductTypeID
)
select distinct productid from a ,dbo.ProductSubtype b
where a.ProductTypeID =b.ProductTypeID
)
生成代码
public sealed class ProductTypeFunctionName:RJ.Sql.BaseEntity
{
public readonly static String TABLENAME = "ProductTypeFunctionName";
private static ProductTypeFunctionName ___singleton;
public static ProductTypeFunctionName Instance()
{
if(___singleton==null)
{
___singleton = new ProductTypeFunctionName();
}
return ___singleton;
}
private ProductTypeFunctionName()
{
}
public readonly static QueryColumn _ProductId = new QueryColumn((TABLENAME + ".[ProductId]"), System.Data.DbType.Int32);
public QueryColumn ProductId { get { return _ProductId; } }
public override string ___GetTableName() { return TABLENAME; }
}
代码中
var t = new RJ.Sql.SelectSqlSection<ProductTypeFunctionName>(2).AddColumn(a => a.ProductId);
var s1 = RJ.DModel.Product.SelectSqlSectionFor;
s1.AddFormFun<ProductTypeFunctionName>((a,b) => a.ProductID==b.ProductId,2);
var s = RJ.DModel.Product.SelectSqlSectionFor;
s.Where(a=>a.ProductID.In(t));
SELECT [ProductTypeFunctionName].[ProductId] FROM ProductTypeFunctionName(@pveenih6h8o22ps)
SELECT * FROM [Product] , ProductTypeFunctionName(@pr00tk80fsgxoqb) [ProductTypeFunctionName] WHERE [Product].[ProductID] = [ProductTypeFunctionName].[ProductId]
SELECT * FROM [Product] WHERE [Product].[ProductID] IN (SELECT [ProductTypeFunctionName].[ProductId] FROM ProductTypeFunctionName(@pe2gqh5m9txbo7y))
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· 为DeepSeek添加本地知识库
· 精选4款基于.NET开源、功能强大的通讯调试工具
· DeepSeek智能编程
· 大模型工具KTransformer的安装
· [计算机/硬件/GPU] 显卡