使用不存在的数据更新SQL Server表

时间:2017-12-03 21:51:02

标签: sql-server-2012 sql-server-2014

我有下面的脚本供应它的时间,我需要做一些修改。该脚本每天运行2到3次,以从表中导入数据。有些数据可能是重复的,我想要的是修改后的脚本只会在数据不存在的情况下导入和写入表。

有人提到我可以尝试合并数据或使用Where Not Exist。

不幸的是,我不确定我将如何实施,因此我向大家求助。任何帮助将不胜感激。

BEGIN TRY
BEGIN TRANSACTION 
Declare @Date As DATE

IF OBJECT_ID('Tempdb..#1','U') IS NOT NULL
DROP TABLE #1

SELECT [EMP_ID],[NAME],[DEPT],[DATE],[Start Time],[LOGIN],[LATE/AWOL],[Team Manager],[Senior Manager],[SITE]
INTO #1
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
                 'Excel 12.0;Database=\\VF_RSOSDCNASUDA\xxx\SQL\ESO\ALLAttendance.xlsm;HDR=YES',
                 'SELECT * FROM [ForEmail$]');

SELECT TOP 1 @Date=[DATE] FROM #1

DELETE [dbo].[ALLAttendance$] WHERE CONVERT(char(10),[Date],102)=@Date;

INSERT INTO [dbo].[ALLAttendance$]
SELECT [EMP_ID],[NAME],[DEPT],[DATE],[Start Time],[LOGIN],[LATE/AWOL],[Team Manager],[Senior Manager],[SITE]
FROM #1
DROP TABLE #1
COMMIT
END TRY
BEGIN CATCH

IF @@TRANCOUNT > 0
    ROLLBACK
END CATCH

***** UPDATE ***** 我提出了下面的内容,似乎有效,但不确定这是否是正确的做法

with CTE
AS
(
SELECT [EMP_ID]
   ,[NAME]
   ,[DEPT]
   ,[DATE]
   ,[Start Time]
   ,[LOGIN]
   ,[LATE/AWOL]
   ,[Team Manager]
   ,[Senior Manager]
   ,[SITE]
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
                 'Excel 12.0;Database=\\VF_RSOSDCNASUDA\xxx\SQL\ESO\ALLAttendance.xlsm;HDR=YES',
                 'SELECT * FROM [ForEmail$]')
)
merge [dbo].[ALLAttendance$] t
using CTE s
on s.[EMP_ID] = t.[EMP_ID]
and s.[NAME] = t.[NAME]
and s.[DEPT] = t.[DEPT]
and s.[DATE] = t.[DATE]
and s.[Start Time] = t.[Start Time]
and s.[LOGIN] = t.[LOGIN]
and s.[LATE/AWOL] = t.[LATE/AWOL]
and s.[Team Manager] = t.[Team Manager]
and s.[Senior Manager] = t.[Senior Manager]
and s.[SITE] = t.[SITE]
when not matched by target
then insert ([EMP_ID],[NAME],[DEPT],[DATE],[Start Time],[LOGIN],[LATE/AWOL],[Team Manager],[Senior Manager],[SITE]) 
values (s.[EMP_ID],s.[NAME],s.[DEPT],s.[DATE],s.[Start Time],s.[LOGIN],s.[LATE/AWOL],s.[Team Manager],s.[Senior Manager],s.[SITE]);

1 个答案:

答案 0 :(得分:0)

我对此问题的最终解决方案如下。

app.get('/getContents', function(req,res){
    var params = {Bucket: req.query.Bucket};

    return new Promise(function(resolve, reject) {
        s3.listObjects(params, function(err, data) {
           if (err) {
                var error = {};
                error['isError'] = true,
                error['status'] = 400,
                error['message'] = "Data Not Found",
                error['data'] = err
                resolve(error);
            }
            else {
                var result1 = [];   
                var result = data.Contents;
                result.forEach(function(final){
                    var urlParams = {Bucket: req.query.Bucket, Key: final.Key};
                    s3.getSignedUrl('getObject', urlParams, function(err, url){
                        request.get(url, function (err, result, body) {
                            var path = result.request.uri.href;
                            new ExifImage(body, function (error, exifData) {
                                if (error) {
                                    result1.push(error.message);
                                }
                                else {
                                    result1.push(exifData);
                                    res.send(result1);
                                }
                            });
                        });
                    });
                });     
                //res.send(result1);
            }   
        }); 
    });
});