如何向一张表中插入多条记录?
假设a和b的结构一样
insert into a
select * from b
这样可以把记录导入到a表中,但是如果a表中有自动增加的字段就不以啊。
------解决方案--------------------insert into a(字段1,字段2...) select * from b
------解决方案--------------------不要用*,显示地把字段名称都写出来
insert into a(col1,col2,...coln)
select col1,col2,...coln from b
------解决方案-----------------------下面的是查看表结构的语句,它可以看出那个列是自增列,IsIDENTITY=1是自增列,IsIDENTITY=0 不是
SELECT TableName=o.name,OWNER=USER_NAME(o.uid),TableDescription=ISNULL(ptb.value,N ' '),
FieldId=c.colid,FieldName=c.name,
FieldType=QUOTENAME(t.name)
+CASE
WHEN t.name IN (N 'decimal ',N 'numeric ')
THEN N '( '+CAST(c.prec as varchar)+N ', '+CAST(c.scale as varchar)+N ') '
WHEN t.name=N 'float '
OR t.name like N '%char '
OR t.name like N '%binary '
THEN N '( '+CAST(c.prec as varchar)+N ') '
ELSE N ' ' END
+CASE WHEN c.isnullable=1 THEN N ' ' ELSE N ' NOT ' END+N ' NULL ',
FieldDescription=ISNULL(pfd.value, ' '),
DefileLength=c.length,
FieldDefault=ISNULL(df.text,N ' '),
IsIDENTITY=COLUMNPROPERTY(o.id,c.name,N 'IsIdentity '),
IsComputed=COLUMNPROPERTY(o.id,c.name,N 'IsComputed '),
IsROWGUID=COLUMNPROPERTY(o.id,c.name,N 'IsRowGuidCol '),
IsPrimaryKey=CASE WHEN opk.xtype IS NULL THEN 0 ELSE 1 END
FROM sysobjects o
JOIN syscolumns c
ON c.id=o.id
AND OBJECTPROPERTY(o.id,N 'IsUserTable ')=1
JOIN systypes t
ON t.xusertype=c.xusertype
LEFT JOIN syscomments df
ON df.id=c.cdefault
LEFT JOIN sysproperties ptb
ON ptb.id=o.id and ptb.smallid=0
LEFT JOIN sysproperties pfd
ON pfd.id=o.id and pfd.smallid=c.colid
LEFT JOIN sysindexkeys idxk
ON idxk.id=o.id
AND idxk.colid=c.colid
LEFT JOIN sysindexes idx
ON idx.indid=idxk.indid
AND idx.id=idxk.id
AND idx.indid NOT IN(0,255)
LEFT JOIN sysobjects opk
ON opk.parent_obj=o.id
AND opk.name=idx.name
AND OBJECTPROPERTY(opk.id,N 'IsPrimaryKey ')=1
where object_name(o.id)= 'b ' ---这个 b 是你的表名
ORDER BY o.name,c.colid