自动建表 1

本文介绍了一个使用SQL存储过程自动创建数据库表结构的方法。通过动态拼接SQL语句,该过程能够根据预设的数据范围和字段名自动生成包含指定字段的表结构。

自动建立表结构
Create Procedure AutoCreateTable
@tablename varchar(40)
as
begin
declare @s1 varchar(8000),@s2 varchar(8000),@s3 varchar(8000)
select @s1='Create table '+@tablename+'  ( tagtime datetime ',@s2='',@s3=''
select @s1=@s1+',['+rtrim(tagname)+'] varchar(10) ' from bj01data where ID between 221 and 310 order by ID
select @s2=@s2+',['+rtrim(tagname)+'] varchar(10) ' from bj01data where ID between 311 and 410 order by ID
select @s3=@s3+',['+rtrim(tagname)+'] varchar(10) ' from bj01data where ID between 402 and 492 order by ID
select @s1=stuff(@s1,20,1,'')
select @s3=@s3+')'
exec (@s1+@s2+@s3 )
end

用以下语句来调用存储过程

declare @tablename varchar(100)
declare @i int
set @tablename=''
set @i=1
while @i<=365
begin
--exec('exec AutoCreateTable'+ ' GL'+''+@i'')
select @tablename='GY.dbo..GY'+right(@i+1000 ,3)
exec ('exec AutoCreateTable '+ ''''+@tablename+'''')
select @i=@i+1
end 

Flowable 提供了自动功能,可以在项目启动时根据配置自动所需的数据库。要启用自动功能,只需在 `application.yml` 或 `application.properties` 中进行相应的配置。 --- ### ✅ 自动的配置(推荐使用 `application.yml`) ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/flowable_db?useSSL=false&serverTimezone=UTC username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver flowable: database-schema-update: true history: enabled: true level: full ``` --- ### 🔧 配置说明 | 配置项 | 说明 | |--------|------| | `flowable.database-schema-update` | 是否启用自动功能。设置为 `true` 示在启动时自动或更新结构。 | | `flowable.history.enabled` | 是否启用历史记录功能(如记录流程实例、任务等历史信息)。 | | `flowable.history.level` | 历史记录级别,可选值有 `none`, `activity`, `audit`, `full`。议使用 `full` 获取完整信息。 | --- ### 💡 注意事项 1. **数据库权限**:确保数据库用户有权限创。 2. **数据库支持**:Flowable 支持多种数据库(MySQL、PostgreSQL、Oracle 等),不同数据库的驱动和连接方式略有不同。 3. **首次运行**:第一次运行项目时,如果配置正确,Flowable 会自动在指定数据库中创大约 50 多张(如 `ACT_RE_PROCDEF`, `ACT_RU_TASK` 等)。 4. **手动执行 SQL**:如果你不想使用自动功能,也可以手动执行 Flowable 提供的 SQL 脚本(位于 Flowable 官方 GitHub 或发布包中)。 --- ### ✅ 示例:完整配置(application.properties) ```properties spring.datasource.url=jdbc:mysql://localhost:3306/flowable_db?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver flowable.database-schema-update=true flowable.history.enabled=true flowable.history.level=full ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值