SQL Server中的数据自动导出到Access中,这个问题您是怎么解决的
2024-08-23 广告
我们用的SSIS包,
要让SQL2008及以上能显示OFFICE2007的文件选项,先在SQL服务器上装上office2007的组件,
AccessDatabaseEngine_2010_x64.exe AccessDatabaseEngine_2007_x86.exe在SQL中创建一个SSIS包,将Access文件名,SQL语句(新增表,删除表),设为一个变量,可传参数。(SQL与access一起工作时,主要就是不能判断表是否存在),所以,直接删除(不存在会报错,直接忽略就是),然后再创建表。(这个包,附件上了)
SQL导数据时,直接调用 SSIS包导数据就好了,这时就可以自己写存储过程了。
具体,可能较麻烦,特别如果你没做过SSIS包。
下面是部分存储过程,因是客户的资料,只能共享部分给你了,
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N''[dbo].[vi_Columns]''))
BEGIN
SELECT @SQL=''CREATE VIEW [dbo].[vi_Columns] AS
SELECT distinct obj.name AS TableName, col.colorder, col.name AS ColumnName, typ.name AS TypeName, col.prec
FROM dbo.syscolumns AS col LEFT OUTER JOIN
dbo.systypes AS typ ON col.xtype = typ.xusertype INNER JOIN
dbo.sysobjects AS obj ON col.id = obj.id AND obj.xtype = ''''U'''' AND obj.status >= 0 INNER join
dbo.RS_EXPORT_SETTING as [exp] ON [exp].IMPORT_TABLE_NAME=obj.name and ([exp].IMPORT_COLUMN_NAME=''''-'''' or
([exp].IMPORT_COLUMN_NAME !=''''-'''' and patindex(''''%''''+ col.Name+''''%'''',[exp].IMPORT_COLUMN_NAME)>0))''
EXEC(@SQL)
END以上是创建一个视图,用来获得表中的字段的,因,我们有部分表只要导出部分字段
以下是导数据的存储过程中的部分代码
--Get joint columns name, which looks like: `fileID` VarChar(2),`aod` DateTime,`branch` VarChar(4),`sbrch` VarChar(3),
SELECT @SQLCreate=(SELECT CHAR(96) + ColumnName + CHAR(96) + '' '' +
CASE WHEN TypeName in(''varchar'',''nvarchar'',''char'',''nchar'',''varbinary'',''binary'') THEN
CASE WHEN Prec<256 THEN ''VarChar('' + CONVERT(NVARCHAR(3), Prec) + '')'' ELSE ''LongText'' END
WHEN TypeName in (''numeric'', ''decimal'', ''float'', ''currency'') THEN ''Double''
WHEN TypeName in (''int'') THEN ''Long''
WHEN TypeName in (''datetime'', ''date'') THEN ''DateTime''
ELSE ''LongText''
END + '',''
FROM vi_Columns as b WHERE b.TableName = a.TableName FOR XML PATH(''''))
FROM vi_Columns as a
WHERE TableName=@SourceTableName
GROUP BY TableName
SELECT @SQLCreate=''Create Table '' + @DestTableName + '' ('' + SUBSTRING(@SQLCreate, 1, LEN(@SQLCreate)-1) + '')''
SELECT @SQLDrop=''DROP TABLE '' + @DestTableName
SELECT @SQL = ''dtexec /F "'' + @SSISFile + ''"''
--Set variables''s value in SSIS file
SELECT @SQL = @SQL + '' /SET \Package.Variables[User::FilePath].Properties[Value];"'' + @EUCFileName + ''"''
SELECT @SQL = @SQL + '' /SET \Package.Variables[User::SQLDrop].Properties[Value];"'' + @SQLDrop + ''"''
SELECT @SQL = @SQL + '' /SET \Package.Variables[User::SQLCreate].Properties[Value];"'' + @SQLCreate + ''"''
--Drop table and create it again according to the "DestTableName" by calling the SSIS file
EXEC @returncode=master..xp_cmdshell @SQL, no_output
--error happend when drop or create table BEGINIF @returncode <> 0
--sava fail message
SET @createMessage = @createMessage + @DestTableName + '',''
ELSE
BEGIN
-- Insert the data into the destination table in EUC file from the source table in SQL Server
SET @columenSQL = N''SELECT @importColumenName=[IMPORT_COLUMN_NAME] FROM [dbo].[RS_EXPORT_SETTING] WHERE [IMPORT_TABLE_NAME] = ''''''+ @SourceTableName + ''''''''
EXEC sp_executesql @columenSQL,N''@importColumenName nvarchar(4000) output'',@importColumenName=@importColumenName output
IF @importColumenName = ''-''
BEGIN
SELECT @SQL=''INSERT INTO OPENROWSET(''''Microsoft.ACE.OLEDB.12.0'''', '''''' + @EUCFileName +
''''''; ''''admin'''';'''''''','' + @DestTableName +
'') SELECT * FROM '' + @SourceTableName
END
ELSE
BEGIN
SELECT @SQL=''INSERT INTO OPENROWSET(''''Microsoft.ACE.OLEDB.12.0'''', '''''' + @EUCFileName +
''''''; ''''admin'''';'''''''','' + @DestTableName +
'') SELECT ''+@importColumenName+'' FROM '' + @SourceTableName
END
export