1、SQL语句选择每月的数据添加到另一张表
insert into _Data_Collect_20231108 (RECORD_ID,RTU_ID,DataType,Time,Value,INSERT_TIME,I_Flag) select RECORD_ID,RTU_ID,DataType,Time,Value,INSERT_TIME,I_Flag from Data_Collect where time <'2021-12-31 00:00' delete from Data_Collect where time <'2021-12-31 00:00'
2、sql语句把 A表的数据表备份的表(表结构与数据一起复制)
select [RECORD_ID],[RTU_ID],[DataType],[Time],[Value],[INSERT_TIME],[I_Flag] into [history].[dbo].[_Data_Collect_20231219] from _Data_Collect_20231108
3、仅仅复制表结构(A->B):
SELECT * INTO DatabaseB.dbo.TableB FROM DatabaseA.dbo.TableA WHERE 1=2
4、查询24小时的数据信息
select RECORD_ID,RTU_ID,DataType,Time,Value,INSERT_TIME,I_Flag from JSRWater where RTU_ID in ( '453040022','453030056','452110056','452110055','451110043','451090081','451060050','450100014')and DateDiff(hh,time,getDate())<=1
5、创建临时表,查询数据库的表大小
name:表名
rows:表中的行数
reserved:表占用的总空间(包括数据和索引空间)
data:表中的数据空间大小
index_size:表中的索引空间大小
unused:表中未使用的空间大小
CREATE TABLE #temp ( [name] NVARCHAR(128), [rows] CHAR(11), reserved VARCHAR(18), data VARCHAR(18), index_size VARCHAR(18), unused VARCHAR(18) ) EXEC sp_MSforeachtable ' INSERT INTO #temp EXEC sp_spaceused ''?''; ' SELECT * FROM #temp order by reserved desc DROP TABLE #temp