sql >> Database teknologi >  >> RDS >> Sqlserver

Sådan masseindsættes med en dynamisk værdi for en kolonne

Dette er en af ​​de få gange, at en markør faktisk er ideel i SQL Server. Her er en måde. Når du ser PRINT-erklæringen og er tilfreds, kan du kommentere den ud og fjerne de to linjer under den. Jeg tilføjede noget logik for at tilføje filnavnet og en behandlet dato, som normalt er nødvendig, men din tabeldefinition ville have brug for disse kolonner. Det burde få ideen igennem.

---------------------------------------------------------------------------------------------------------------
--Set some variables
---------------------------------------------------------------------------------------------------------------

DECLARE @dt VARCHAR(10)                                                         --date variable but stored as VARCHAR for formatting of file name
DECLARE @fileLocation VARCHAR(128) = 'E:\DATA_TRANSFERS\'                       --production location which is \\issqlstd01 but the xp_dirtree didn't like this
DECLARE @sql NVARCHAR(4000)                                                     --dynamic sql variable
DECLARE @fileName VARCHAR(128)                                                  --full file name variable


---------------------------------------------------------------------------------------------------------------
--Get a list of all the file names in the directory
---------------------------------------------------------------------------------------------------------------

IF OBJECT_ID('tempdb..#FileNames') IS NOT NULL DROP TABLE #FileNames
CREATE TABLE #FileNames (
    id int IDENTITY(1,1)
    ,subdirectory nvarchar(512)
    ,depth int
    ,isfile bit)
INSERT #FileNames (subdirectory,depth,isfile)
EXEC xp_dirtree @fileLocation, 1, 1





---------------------------------------------------------------------------------------------------------------
--Create a cursor to fetch the file names
---------------------------------------------------------------------------------------------------------------

DECLARE c CURSOR FOR
select subdirectory from #FileNames where isfile = 1

OPEN c
FETCH NEXT FROM c INTO @fileName

---------------------------------------------------------------------------------------------------------------
--For each file, bulk insert 
---------------------------------------------------------------------------------------------------------------


WHILE @@FETCH_STATUS = 0
    BEGIN

        SET @sql = 'BULK INSERT Employee_Table FROM '''+ @fileLocation + @fileName +''' WITH (FIELDTERMINATOR = ''\t'',KEEPNULLS,ROWTERMINATOR = ''0x0a'')'


        --Try the bulk insert, if error is thrown log the error 
        --Also update the Table Columns which aren't a part of the original file (load date and original file name)
        BEGIN TRY
            PRINT(@sql)
            --EXEC(@sql)
            --UPDATE Employee_Table SET OrigFile = @fileName, LoadDate = GETDATE() WHERE OrigFile IS NULL
        END TRY
        BEGIN CATCH
            SELECT ERROR_MESSAGE()
        END CATCH

        FETCH NEXT FROM c INTO @fileName
    END

CLOSE c
DEALLOCATE c

GO



  1. MySQL dump efter forespørgsel

  2. Kan ikke oprette forbindelse til Postgres-databasen med Bitnami Django-stak

  3. Effektive ISNUMERIC()-erstatninger på SQL Server?

  4. MySQL DATE_ADD virker ikke