这是我从.txt文件BULK插入两个表的SQL语句:
BEGIN TRANSACTION ImportNutritionDataToDatabase;
USE [AppTest];
-------------------------------------------------------
-- Food Group Descriptions #1
-------------------------------------------------------
DECLARE @Path NVARCHAR(1000) = 'D:\CodePurehope\App\Dev\NutrientData\FD_GROUP.txt';
CREATE TABLE #TempTable
(
[FoodGroupCode] VARCHAR(666) NOT NULL,
[FoodGroupDescription] VARCHAR(60) NOT NULL
)
DECLARE @sql NVARCHAR(4000) = 'BULK INSERT #TempTable FROM ''' + @Path + ''' WITH ( FIELDTERMINATOR =''^'', ROWTERMINATOR =''\n'' )';
EXEC(@sql);
UPDATE #TempTable
SET [FoodGroupCode] = REPLACE([FoodGroupCode], '~', ''),
[FoodGroupDescription] = REPLACE([FoodGroupDescription], '~', '')
GO
INSERT INTO [dbo].[FoodGroupDescriptions]
(
[FoodGroupCode],
[FoodGroupDescription]
)
SELECT
[FoodGroupCode],
[FoodGroupDescription]
FROM
#TempTable
GO
DROP TABLE #TempTable
-------------------------------------------------------
-- Food Description #2
-------------------------------------------------------
DECLARE @Path NVARCHAR(1000) = 'D:\CodePurehope\App\Dev\NutrientData\FOOD_DES.txt';
CREATE TABLE #TempTable2
(
[NutrientDatabankNumber] [varchar](10) NOT NULL,
[FoodGroupCode] [varchar](10) NOT NULL,
[LongDescription] [varchar](200) NOT NULL,
[ShortDescription] [varchar](200) NOT NULL,
[CommonName] [varchar](100) NULL,
[ManufacturerName] [varchar](165) NULL,
[Survey] [varchar](10) NULL,
[ReferenceDescription] [varchar](235) NULL,
[RefusePercentage] [tinyint] NULL,
[ScientificName] [varchar](65) NULL,
[NitrogenFactor] [decimal](18, 2) NULL,
[ProteinFactor] [decimal](18, 2) NULL,
[FatFactor] [decimal](18, 2) NULL,
[CarbohydratesFactor] [decimal](18, 2) NULL
)
DECLARE @sql NVARCHAR(4000) = 'BULK INSERT #TempTable2 FROM ''' + @Path + ''' WITH ( FIELDTERMINATOR =''^'', ROWTERMINATOR =''\n'' )';
EXEC(@sql);
UPDATE #TempTable2
SET [NutrientDatabankNumber] = REPLACE([NutrientDatabankNumber], '~', ''),
[FoodGroupCode] = REPLACE([FoodGroupCode], '~', ''),
[LongDescription] = REPLACE([LongDescription], '~', ''),
[ShortDescription] = REPLACE([ShortDescription], '~', ''),
[CommonName] = REPLACE([CommonName], '~', ''),
[ManufacturerName] = REPLACE([ManufacturerName], '~', ''),
[Survey] = REPLACE([Survey], '~', ''),
[ReferenceDescription] = REPLACE([ReferenceDescription], '~', ''),
[RefusePercentage] = REPLACE([RefusePercentage], '~', ''),
[ScientificName] = REPLACE([ScientificName], '~', ''),
[NitrogenFactor] = REPLACE([NitrogenFactor], '~', ''),
[ProteinFactor] = REPLACE([ProteinFactor], '~', ''),
[FatFactor] = REPLACE([FatFactor], '~', ''),
[CarbohydratesFactor] = REPLACE([CarbohydratesFactor], '~', '')
GO
INSERT INTO [dbo].[FoodDescriptions]
(
[NutrientDatabankNumber],
[FoodGroupCode],
[LongDescription],
[ShortDescription],
[CommonName],
[ManufacturerName],
[Survey],
[ReferenceDescription],
[RefusePercentage],
[ScientificName],
[NitrogenFactor],
[ProteinFactor],
[FatFactor],
[CarbohydratesFactor]
)
SELECT
[NutrientDatabankNumber],
[FoodGroupCode],
[LongDescription],
[ShortDescription],
[CommonName],
[ManufacturerName],
[Survey],
[ReferenceDescription],
[RefusePercentage],
[ScientificName],
[NitrogenFactor],
[ProteinFactor],
[FatFactor],
[CarbohydratesFactor]
FROM
#TempTable2
GO
DROP TABLE #TempTable2
COMMIT TRANSACTION ImportNutritionDataToDatabase;
GO
如何在不重复路径的情况下使用一个temptable等来简化此操作?
答案 0 :(得分:0)
这正是SSIS的意思。我建议将其用于文件中的ETL。它将在开发过程中提高效率,并且可以配置为更容易处理无效数据元素。更改路径也更容易,因为您可以在每个环境中的SQL代理作业中覆盖它们。