由于工作需要根据EXCEL盘点表导入到数据库里调整库存,之前没有接触过ERP系统,刚开始不知从何入手,经过两天的摸索把过程记录下来
刚开始把EXCEL表直接通过DTS数据导入/导出向导把表导入到数据库里,生成了一个新表但不知为何其中有一个字段在EXCEL中是有记录的导到数据库中部分会出现NULL值,也许是EXCEL表里的格式类型与数据库表里的类型不符造成。后来把表转换成TXT格式再导入到数据库里一切正常。
在查询分析器里运下如下代码:
declare @TableName sysname
declare @WhereClause varchar(1024)
declare @IdentityInsert int
declare @ColName sysname
declare @ColType tinyint
declare @ColStatus tinyint
declare @DebugMode bit
declare @ColList nvarchar(4000)
declare @ValList nvarchar(4000)
declare @SQL1 nvarchar(1000)
declare @SQL2 nchar(10)
declare @SQL3 nchar(1000)
declare @WhereClause varchar(1024)
declare @IdentityInsert int
declare @ColName sysname
declare @ColType tinyint
declare @ColStatus tinyint
declare @DebugMode bit
declare @ColList nvarchar(4000)
declare @ValList nvarchar(4000)
declare @SQL1 nvarchar(1000)
declare @SQL2 nchar(10)
declare @SQL3 nchar(1000)
set @TableName = '<YourTableName>' -- '<YourTableName>'
set @WhereClause = '' -- limit scope of inserts
set @DebugMode = 0 -- set to 1 if you only want a script
set @WhereClause = '' -- limit scope of inserts
set @DebugMode = 0 -- set to 1 if you only want a script
set @IdentityInsert = 0 -- set to 1 if you want to force IDENTITY_INSERT statements
set @ColList = ''
set @ValList = ''
set @SQL1 = 'select replace(''insert into ' + @TableName + ' ('
set @SQL2 = ') values ('
set @SQL3 = ')'', ''''''null'''''', ''null'') from ' + @TableName
set @ValList = ''
set @SQL1 = 'select replace(''insert into ' + @TableName + ' ('
set @SQL2 = ') values ('
set @SQL3 = ')'', ''''''null'''''', ''null'') from ' + @TableName
if @DebugMode = 1 print '-- StmtShell: ' + @sql1 + @sql2 + @sql3
declare csrColumns cursor local fast_forward for
select c.name, c.xtype, c.status
from syscolumns c
inner join sysobjects o
on o.id = c.id
where o.name = @TableName
and o.xtype in ('U', 'S')
order by ColID
select c.name, c.xtype, c.status
from syscolumns c
inner join sysobjects o
on o.id = c.id
where o.name = @TableName
and o.xtype in ('U', 'S')
order by ColID
open csrColumns
fetch next from csrColumns into @ColName, @ColType, @ColStatus
fetch next from csrColumns into @ColName, @ColType, @ColStatus
while @@fetch_status = 0
begin
set @ColList = @ColList + ' ' + @ColName
if @ColType in (173, 104, 106, 62, 56, 60, 108, 59, 52, 122, 48, 165) -- numeric types (nulls not supported yet)
set @ValList = @ValList + ' ''+convert(varchar(200),' + @ColName + ')+'''
else if @ColType in (175, 239, 231, 231, 167) -- uid and string types
set @ValList = @ValList + ' ''''''+isnull(' + @ColName + ',''null'')+'''''''
else if @ColType in (58, 61) -- dates (nulls not supported yet)
set @ValList = @ValList + ' ''''''+convert(varchar(200),' + @ColName + ')+'''''''
else if @ColType = 36 -- uniqueidentfiers (nulls not supported yet)
set @ValList = @ValList + ' ''''{''+convert(varchar(200),' + @ColName + ')+''}'''''
if @DebugMode = 1 begin print '-- @ValList: ' + rtrim(@ValList) end
if (@ColStatus & 0x80) = 0x80 begin set @IdentityInsert = 1 end -- Check if column has Identity attribute
fetch next from csrColumns into @ColName, @ColType, @ColStatus
end
begin
set @ColList = @ColList + ' ' + @ColName
if @ColType in (173, 104, 106, 62, 56, 60, 108, 59, 52, 122, 48, 165) -- numeric types (nulls not supported yet)
set @ValList = @ValList + ' ''+convert(varchar(200),' + @ColName + ')+'''
else if @ColType in (175, 239, 231, 231, 167) -- uid and string types
set @ValList = @ValList + ' ''''''+isnull(' + @ColName + ',''null'')+'''''''
else if @ColType in (58, 61) -- dates (nulls not supported yet)
set @ValList = @ValList + ' ''''''+convert(varchar(200),' + @ColName + ')+'''''''
else if @ColType = 36 -- uniqueidentfiers (nulls not supported yet)
set @ValList = @ValList + ' ''''{''+convert(varchar(200),' + @ColName + ')+''}'''''
if @DebugMode = 1 begin print '-- @ValList: ' + rtrim(@ValList) end
if (@ColStatus & 0x80) = 0x80 begin set @IdentityInsert = 1 end -- Check if column has Identity attribute
fetch next from csrColumns into @ColName, @ColType, @ColStatus
end
close csrColumns
deallocate csrColumns
deallocate csrColumns
set @ColList = replace(ltrim(@ColList), ' ', ', ')
set @ValList = replace(ltrim(@ValList), ' ', ', ')
set @ValList = replace(ltrim(@ValList), ' ', ', ')
if @IdentityInsert = 1
print 'set identity_insert ' + @TableName + ' on'
print 'set identity_insert ' + @TableName + ' on'
if @DebugMode = 1
print @SQL1 + @ColList + @SQL2 + @ValList + @SQL3 + ' ' + @WhereClause
else
exec (@SQL1 + @ColList + @SQL2 + @ValList + @SQL3 + ' ' + @WhereClause)
print @SQL1 + @ColList + @SQL2 + @ValList + @SQL3 + ' ' + @WhereClause
else
exec (@SQL1 + @ColList + @SQL2 + @ValList + @SQL3 + ' ' + @WhereClause)
if @IdentityInsert = 1
print 'set identity_insert ' + @TableName + ' off'
print 'set identity_insert ' + @TableName + ' off'
把查询结果选定再另存为 *.sql文件即可
转载于:https://blog.51cto.com/roboboy/98871