sqlserver存储过程检查表是否存在,存在即删除,不存在创建,家while遍历插入数据
2021/12/3 2:08:21
本文主要是介绍sqlserver存储过程检查表是否存在,存在即删除,不存在创建,家while遍历插入数据,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
use DFPVHRDB
go
--检查是否存在该表,存在即删除
if exists(select * from sysobjects where id = OBJECT_ID('DFPVHRDB..zyxATT05newPerson') and OBJECTPROPERTY(id,'IsUserTable') = 1)
print 'exists'
drop table DFPVHRDB..zyxATT05newPerson
go
--检查是否存在该表,不存在即创建新表
if not exists(select * from sysobjects where id = OBJECT_ID('DFPVHRDB..zyxATT05newPerson') and OBJECTPROPERTY(id,'IsUserTable') = 1)
print 'not exists'
select * into DFPVHRDB..zyxATT05newPerson from DFPVHRDB..ATT05 A
where A.YearInt=2021 and A.MonthInt = Month(GETDATE()) ;
--删除表数据
delete from DFPVHRDB..zyxATT05newPerson
print 'delete'
go
declare
@i int, --定义循环条件
@YearInt datetime,--定义年份
@MonthInt datetime,--定义月份
@RecDate datetime,
@PersIDList varchar(max);--定义charindex的格式
print 'declare'
set @i = 1;
print @i
set @YearInt=2021;
print @YearInt
set @MonthInt=11;
print @MonthInt
set @RecDate = '2021-11-01 00:00:00.000';
print @RecDate
print 'set'
while @i <= 30
begin
Insert zyxATT05newPerson(YearInt,MonthInt,RecDate,EMPID,PersID,Name,DeptCode,DeptName,PersType,PersClass,YCQT,L1DeptName,L2DeptName,L3DeptName,ZGQK,YCQXS,JobName2,JobCode,JobName)
Select 2021,11,@RecDate,A.EMPID,A.PersID,A.Name,A.DeptCode,A.DeptName,A.PersType,A.PersClass,0,A.L1DeptName,A.L2DeptName,
A.L3DeptName,A.ZGQK,Case datepart(weekday,@RecDate) when 1 then 0 when 7 then 0 else 8 end,A.AllJobName,A.JobCode,A.JobName
From EMP01 A Left Join EMP12 B On A.PersID=B.PersID where B.YearInt='2021' and B.MonthInt='11' and B.AppStatus='审批通过';
set @i = @i + 1 ;
print @i
set @RecDate = @RecDate + 1;
print @RecDate
end
这篇关于sqlserver存储过程检查表是否存在,存在即删除,不存在创建,家while遍历插入数据的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-08Docker下的SqlServer发布订阅启用
- 2023-06-05Docker安装MS SQL Server并使用Navicat远程连接
- 2023-05-25深入浅出 SQL Server CDC 数据同步
- 2023-05-12通过空间占用和执行计划了解SQL Server的行存储索引
- 2023-04-24以SQLserver为例的Dapper详细讲解
- 2022-11-30SQL server高级函数查询
- 2022-11-26SQL SERVER数据库服务器CPU不能全部利用原因分析
- 2022-11-21SQL Server 时间算差值/常用函数
- 2022-11-20调试Archery连接SQL Server提示驱动错误
- 2022-10-22SQL Server 完整、差异备份+完整、差异还原(详细讲解,规避错误)