Untitled

 avatar
unknown
plain_text
2 years ago
1.3 kB
4
Indexable
BEGIN TRANSACTION;

-- Declare table variable to hold imported data
DECLARE @ImportFileContent TABLE (
    MovieName NVARCHAR(100),
    ReleaseDate DATE,
    Genre NVARCHAR(50),
    Director NVARCHAR(100)
);

-- Sample data for @ImportFileContent (you can replace this with actual data)
INSERT INTO @ImportFileContent (MovieName, ReleaseDate, Genre, Director)
VALUES
    ('Movie 1', '2023-01-01', 'Action', 'Director 1'),
    ('Movie 2', '2023-02-01', 'Comedy', 'Director 2'),
    -- Add more rows as needed
    ('Movie N', '2023-12-31', 'Drama', 'Director N');

BEGIN TRY
    -- Insert movies from @ImportFileContent into the #Movies table
    INSERT INTO #Movies (MovieName, ReleaseDate, Genre, Director)
    SELECT MovieName, ReleaseDate, Genre, Director
    FROM @ImportFileContent;

    -- Check for errors during the insertion
    IF @@ROWCOUNT < (SELECT COUNT(*) FROM @ImportFileContent) - @ErrorThreshold
    BEGIN
        RAISERROR('Too many errors occurred during insertion. Rolling back.', 16, 1);
    END
    ELSE
    BEGIN
        -- If no error threshold is reached, commit the transaction
        COMMIT TRANSACTION;
        PRINT 'Movies inserted successfully!';
    END
END TRY
BEGIN CATCH
    -- Handle the error and rollback the transaction
    ROLLBACK TRANSACTION;
    PRINT 'Error occurred: ' + ERROR_MESSAGE();
END CATCH;
Editor is loading...