由於專案需要,需要一次性建5000張表,且每張表的命名為target0001-target5000:
**如下:
/*下面迴圈建立有誤,第一行 '4' 附近有錯誤*/
declare @ii int
declare @length int
set @ii = 1
set @length = 4/*將數字轉換為固定長度的字串,不夠的以前導碼0填充×/
while @ii <= 6000
begin
declare @strsql varchar(500)
declare @_str varchar(10)
set @_str=replicate('0',@length-datalength(convert(varchar,@ii)))+convert(varchar,@ii)
/*set @_str = ltrim(@ii)*/
set @strsql = 'create table target'+ @_str + '(vol float,gas float,temp float,speed float,alarm int,x float,y float,z float,wtime datetime)'
print (@strsql)
set @ii = @ii + 1
execute(@strsql)
end
刪除表
declare @ii int
declare @length int
set @ii = 1
set @length = 4
while @ii <= 6000
begin
declare @strsql varchar(500)
declare @_str varchar(10)
set @_str=replicate('0',@length-datalength(convert(varchar,@ii)))+convert(varchar,@ii)
/*set @_str = ltrim(@ii)*/
set @strsql = 'drop table target'+ @_str
print (@strsql)
set @ii = @ii + 1
execute(@strsql)
end批量建帶有主鍵的表,並且主鍵id自增的sql
declare @ii int
declare @length int
set @ii = 2
set @length = 4
while @ii <= 2000
begin
declare @strsql varchar(500)
declare @_str varchar(10)
set @_str=replicate('0',@length-datalength(convert(varchar,@ii)))+convert(varchar,@ii)
/*set @_str = ltrim(@ii)*/
set @strsql = 'create table target'+@_str +'(id int identity(1,1) not null,tpsy_groupname nvarchar(100),tpsy_areaname nvarchar(100),tpsy_analyserip varchar(100),tpsy_phototype int,tpsy_photopath nvarchar(100),tpsy_creattime datetime,tpsy_storagetime datetime,tpsy_photoname nvarchar(100) primary key '+
'( '+
' [id] '+
') on [primary])'
print (@strsql)
set @ii = @ii + 1
execute(@strsql)
end
SQL語句 批量插入表 表資料插表
批量插入表 表資料插表 1.insert into select語句 語句形式為 insert into table2 field1,field2,select value1,value2,from table1 要求目標表table2必須存在,由於目標表table2已經存在,所以我們除了插入源表t...
hive SQL 建表 刪表 插入資料
1.建表 create table xx 使用create命令建立乙個新錶,帶分割槽 create dept string comment 字段注釋 partitioned by col name data type row format delimited fields terminated by...
建表和刪表 sqlalchemy框架
from sqlalchemy.ext.declarative import declarative base 拿到父類 from sqlalchemy import column 拿到字段 from sqlalchemy import create engine 建立引擎 from sqlalch...