TSqls 数据库脚本构建类型
构造方法:
declare @TSqls TSqls=''; --空对象
declare @TSqls TSqls='{"sql":"select id,name from table where id=@id",params:[1]}' --默认值构建
属性/方法 | 类型 | 说明/调用示例 |
---|---|---|
AppendSql | function | @TSqls.AppendSql(‘select * from table;’); |
AppendParams AppendParams2 |
AppendParams3
AppendParams4
AppendParams5 | function | @TSqls.AppendSql(‘select from table where id=@id’).AppendParams(1)
.AppendSql(‘update table set name=@name where code=@code’).AppendParams2(‘ted’,’code’); |
| AppendFormat
AppendFormat2
AppendFormat3
AppendFormat4
AppendFormat5 | function | @TSqls.AppendSql(‘select from table where id={0}’,1);
@TSqls.AppendSql2(‘select * from table where id={0} and name={name}’,1,’ted’); |
代码演示:
----------声明--------------------------
declare @TSqls TSqls='';
----------创建sql-----------------------
---AppendSql可以通过【@参数名】进行参数位占位,AppendParams按顺序把参数值记录上
set @TSqls=@TSqls.AppendSql('select id,name from e_Employee where name=@name').AppendParams('ted');
set @TSqls=@TSqls.AppendSql('select id,name from e_Employee where sex=@sex and age>=@start_age and age<=@end_age').AppendParams4('女',18,28);
-----字符串格式化拼接----
set @TSqls=@TSqls.AppendFormat('select id,{0} from e_Employee','name')
set @TSqls=@TSqls.AppendFormat2('select {0},{1} from e_Employee','id','name')
...
...
以此类推
---------TSqls方法---
select @TSqls.GetParams(); ---查看所有已设置的参数值,便于调试
set @TSqls=@TSqls.Clear(); ---清空所有设置
select @TSqls.ToString();--- 查询TSqls中序列化后的数据。
----------------构建完的TSqls怎么执行呢?------------------------
exec sys_TSqlHandler @TSqls
ps:sys_TSqlHandler为TCode.Sql中封装好的存储过程