创建数据库表默认字段封装SQL

简介: declare @Table_Name varchar(500)declare @strSQL varchar(500)set @Table_Name='UserInfo' --在此处设置要创建的表if(not exists(SELECT * FROM dbo.
declare @Table_Name varchar(500)
declare @strSQL varchar(500)
set @Table_Name='UserInfo' --在此处设置要创建的表
if(not exists(SELECT  * FROM dbo.SysObjects WHERE ID = object_id(N''+@Table_Name+'') AND OBJECTPROPERTY(ID, 'IsTable') = 1))
begin
SET @strSQL='create  table '+@Table_Name+' ( id int IDENTITY(1,1) primary key )'
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='id'))
begin
SET @strSQL='alter table '+@Table_Name+' add id int IDENTITY(1,1) primary key '
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='CreateDate'))
begin
SET @strSQL=' alter table '+@Table_Name+' add CreateDate datetime'
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='CreateUserID'))
begin
SET @strSQL=' alter table '+@Table_Name+' add CreateUserID int'
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='CreateUser'))
begin
SET @strSQL=' alter table '+@Table_Name+' add CreateUser varchar(2000) '
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='LastUpdateUserID'))
begin
SET @strSQL=' alter table '+@Table_Name+' add LastUpdateUserID int '
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='LastUpdateDate'))
begin
SET @strSQL=' alter table '+@Table_Name+' add LastUpdateDate datetime'
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='LastUpdateUser'))
begin
SET @strSQL=' alter table '+@Table_Name+' add LastUpdateUser varchar(2000)'
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='LastUpTimestamp'))
begin
SET @strSQL=' alter table '+@Table_Name+' add LastUpTimestamp timestamp'
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='SortIndex'))
begin
SET @strSQL=' alter table '+@Table_Name+' add SortIndex int '
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='HotIndex'))
begin
SET @strSQL=' alter table '+@Table_Name+' add HotIndex int '
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='IsAudit'))
begin
SET @strSQL=' alter table '+@Table_Name+' add IsAudit int '
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='AuditDate'))
begin
SET @strSQL=' alter table '+@Table_Name+' add AuditDate datetime '
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='IsHot'))
begin
SET @strSQL=' alter table '+@Table_Name+' add IsHot int'
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='IsDel'))
begin
SET @strSQL=' alter table '+@Table_Name+' add IsDel int '
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='DelDate'))
begin
SET @strSQL=' alter table '+@Table_Name+' add DelDate datetime '
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='IsEnable'))
begin
SET @strSQL=' alter table '+@Table_Name+' add IsEnable int '
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='EnableDate'))
begin
SET @strSQL=' alter table '+@Table_Name+' add EnableDate datetime '
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='Status'))
begin
SET @strSQL=' alter table '+@Table_Name+' add Status int '
exec (@strSQL)
end

if(not exists(select * from syscolumns  where id=object_id(''+@Table_Name+'') and name='Pid'))
begin
SET @strSQL=' alter table '+@Table_Name+' add Pid int'
exec (@strSQL)
end
go






目录
相关文章
|
3天前
|
SQL Oracle 关系型数据库
sql语句创建数据库
在创建数据库之前,请确保你有足够的权限,并且已经考虑了数据库的安全性和性能需求。此外,不同的DBMS可能有特定的最佳实践和配置要求,因此建议查阅相关DBMS的官方文档以获取更详细和准确的信息。
|
15天前
|
SQL 人工智能 算法
【SQL server】玩转SQL server数据库:第二章 关系数据库
【SQL server】玩转SQL server数据库:第二章 关系数据库
52 10
|
15天前
|
SQL 算法 数据库
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
88 6
|
2天前
|
SQL Java 数据库连接
Java从入门到精通:2.3.2数据库编程——了解SQL语言,编写基本查询语句
Java从入门到精通:2.3.2数据库编程——了解SQL语言,编写基本查询语句
|
4天前
|
SQL XML 数据库
sql导入数据库命令
在SQL Server中,数据库导入可通过多种方式实现:1) 使用SSMS的“导入数据”向导从各种源(如Excel、CSV)导入;2) BULK INSERT语句适用于导入文本文件;3) bcp命令行工具进行批量数据交换;4) OPENROWSET函数直接从外部数据源(如Excel)插入数据。在操作前,请记得备份数据库,并可能需对数据进行预处理以符合SQL Server要求。注意不同方法可能依版本和配置而异。
|
7天前
|
SQL 关系型数据库 MySQL
mysql 数据库查询 查询字段用逗号隔开 关联另一个表并显示
mysql 数据库查询 查询字段用逗号隔开 关联另一个表并显示
17 2
|
11天前
|
SQL 数据库
数据库SQL语言实战(二)
数据库SQL语言实战(二)
|
11天前
|
SQL 关系型数据库 数据库
【后端面经】【数据库与MySQL】SQL优化:如何发现SQL中的问题?
【4月更文挑战第12天】数据库优化涉及硬件升级、操作系统调整、服务器/引擎优化和SQL优化。SQL优化目标是减少磁盘IO和内存/CPU消耗。`EXPLAIN`命令用于检查SQL执行计划,关注`type`、`possible_keys`、`key`、`rows`和`filtered`字段。设计索引时考虑外键、频繁出现在`where`、`order by`和关联查询中的列,以及区分度高的列。大数据表改结构需谨慎,可能需要停机、低峰期变更或新建表。面试中应准备SQL优化案例,如覆盖索引、优化`order by`、`count`和索引提示。优化分页查询时避免大偏移量,可利用上一批的最大ID进行限制。
38 3
|
14天前
|
SQL 监控 数据库
数据库管理与电脑监控软件:SQL代码优化与实践
本文探讨了如何优化数据库管理和使用电脑监控软件以提升效率。通过SQL代码优化,如使用索引和调整查询语句,能有效提高数据库性能。同时,合理设计数据库结构,如数据表划分和规范化,也能增强管理效率。此外,利用Python脚本自动化收集系统性能数据,并实时提交至网站,可实现对电脑监控的实时性和有效性。这些方法能提升信息系统稳定性和可靠性,满足用户需求。
48 0
|
15天前
|
SQL 存储 数据挖掘
数据库数据恢复—RAID5上层Sql Server数据库数据恢复案例
服务器数据恢复环境: 一台安装windows server操作系统的服务器。一组由8块硬盘组建的RAID5,划分LUN供这台服务器使用。 在windows服务器内装有SqlServer数据库。存储空间LUN划分了两个逻辑分区。 服务器故障&初检: 由于未知原因,Sql Server数据库文件丢失,丢失数据涉及到3个库,表的数量有3000左右。数据库文件丢失原因还没有查清楚,也不能确定数据存储位置。 数据库文件丢失后服务器仍处于开机状态,所幸没有大量数据写入。 将raid5中所有磁盘编号后取出,经过硬件工程师检测,没有发现明显的硬件故障。以只读方式将所有磁盘进行扇区级的全盘镜像,镜像完成后将所
数据库数据恢复—RAID5上层Sql Server数据库数据恢复案例