into
表名
values后面括号里应该是值,而不是
字段名
;
如果是从另外一个表里插入到当前表,应该是这样写:
Insert
Into
TI_AIS_ADDRLINE
(
ADDR_ID
--地址标识
,DIST_CD
--
行政区划代码
,POST_CD
--邮政编码
,POST_CD1
--邮政编码1
,POST_CD2
--邮政编码2
,ADDR_NAME
--地址名称
,RELA_ADDR_ID
--关联地址标识
,RELA_ADDR_NAME
--关联地址名称
,DATA_SRC_CD
--数据来源代码
,LAST_UPD_DATE
--最后更新日期
)
SELECT
ADDR_ID
,DIST_CD
,POST_CD
,POST_CD1
,POST_CD2
,ADDR_NAME
,RELA_ADDR_ID
,RELA_ADDR_NAME
,DATA_SRC_CD
,to_date(LAST_UPD_DATE,
'yyyy-mm-dd
hh24:mi:ss')
FROM
表名
方案一: 存储过程
DROP PROCEDURE IF EXISTS SP_COLUMN_ADDDELIMITER $$
CREATE PROCEDURE SP_COLUMN_ADD()
BEGIN
DECLARE command VARCHAR(200)
DECLARE founded INT DEFAULT 1
DECLARE column_duplicated BOOLEAN DEFAULT FALSE
#把这个游标的语句查下,是否满足需求
DECLARE cur_sleest CURSOR FOR
SELECT CONCAT('ALTER TABLE ', TABLE_NAME, ' ADD COLUMN YOUR_COLUMN YOUR_TYPE') AS SQL_COMMAND
#eg: `ALTER TABLE TMP_SLEEST ADD COLUMN C1 INT,ADD COLUMN C2 VARCHAR(2)`
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA="yourschema"
AND TABLE_TYPE="BASE TABLE"
DECLARE CONTINUE HANDLER FOR NOT FOUND SET founded = 0
DECLARE CONTINUE HANDLER FOR 1060 SET column_duplicated = true
OPEN cur_sleest
sleet_loop : LOOP
FETCH cur_sleest INTO command
IF founded = 0 THEN LEAVE sleet_loop END IF
SET @sql := command
PREPARE stmt FROM @sql
EXECUTE stmt
DEALLOCATE PREPARE stmt
END LOOP sleet_loop
CLOSE cur_sleest
END$$
DELIMITER
方案二: 扫出指令集合到本地文件,贴到命令行执行
SELECT CONCAT("ALTER TABLE ", TABLE_NAME," ADD COLUMN YOUR_COLUMN YOUR_TYPE")FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA="yourschema"
AND TABLE_TYPE="BASE TABLE"
INTO OUTFILE 'D:\\add-column.sql'
不推荐存储过程, 因为可能要处理1060(duplicated column)等问题, 存储过程不是很好弄,中间过程也不好跟踪, 不一定能查出哪里错了
第二种方案直接在命令行中贴上,每一个句话正确错误一看就明了.
WHITE_WIN 老大的语句是对的.但是生成的表却是create table tablenamne(fieldn int,field3 int,field2 int ...field1 int)
这种形式..如果你不在意这点..那就OK啦.
如果硬要create table tablenamne(field1 int,field2 int,field3 int ...fieldn int) 这种.
就用这个试试
create procedure createtable
@i int,
@tablename varchar(10)
as
declare @str varchar(1000)
declare @j int
set @j=1
set @str='create table '+@tablename+'('
while @j<=@i
begin
set @str=@str+'field'+cast(@j as varchar)+' int,'
set @j=@j+1
end
set @str=left(@str,len(@str)-1) +')'
exec(@str)
--形成一个这样的语句并建表:
--create table tablenamne(field1 int,field2 int,field3 int ...fieldn int)
go
呵呵..借花献佛
----------------------------------------------------
一月 二月 三月 这些可不可以用1,2,3 或者1月,2月,3月替换
-----------------------------------------------------------------
create proc proc_select
@year varchar(20), --查询的年月
@i int --要查询的月份
as
declare @j int,@str varchar(2000)
set @j=0
set @str='select staName, '
while @j<@i
begin
set @str=@str+' (case (select count(1) from t_product a where a.staID=b.staID and SaleDate between '''+cast(
dateadd(month,@j,cast(@year as datetime))as varchar(50))+''' and '''+cast(dateadd(day,-1,dateadd(month,@j+1,cast(@year as datetime)))
as varchar(50))+''')when 0 then ''无'' else ''有'' end) '''+cast((@j+1) as varchar(10))+'月'','
set @j=@j+1
end
set @str=@str+' b.staID from t_staffer as b'
exec(@str)
GO
proc_select '2008-01-01','7'
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)