EF 动态拼接查询语句 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Linq.Expressions; using System.Security.Cryptography; using System.Text; namespace Aliexpress.Common.CommonHelper { //public static class Pre…
2016-12-16 阅读项目代码时,在项目的xml文件中发现如下写法: SELECT student_user_id FROM tbr_student_class WHERE 1=1 <if test="@Ognl@isNotEmpty(classId)"> and class_id =#{classId} </if> <if test="@Ognl@isNotEmpty(stuId)"> and student_user_id…
在写存储过程时经常会遇到需要拼接SQL语句的情况,一般情况下仅仅是为了执行拼接后的语句使用exec(@sql)即可. 而今天的一个存储过程却需要获取动态SQL的查询结果. 需求描述:在某表中根据Id值查询Cost值(表名不确定但表结构确定,如下面的Product表) 如果不考虑获取返回值,我们这样写即可: ) ) ,) ) set @tableName='Product' ' set @sql='select Cost from '+@tableName+' where Id='+@id exe…
public T Get<T>(int id) { Type type = typeof(T); string columnStrings = string.Join(",", type.GetProperties().Select(p=>string.Format("[{0}]"))); string sql = string.Format("select {0} from [{1}] where id={2}", colum…
USE [PMS_UnifiedDB_15] GO /****** Object: StoredProcedure [dbo].[SP_GetLogInfo] Script Date: 2/11/2015 3:04:13 PM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> --…
需求场景: 需动态拼接sql语句进行执行,并将执行的结果赋值给一指定变量. 样例代码如下: SELECT @tableName = TAB_NAME FROM dbo.NMR_BLYWBDY WHERE BLID =@bldm AND TAB_TYPE='0' SELECT @sql= 'SELECT DISTINCT top 1 @a= KBLMINXH FROM '+@tableName+' WHERE PAGENO=(SELECT MAX(PAGENO) FROM '+@tableName…