-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path53_Bulk.sql
More file actions
741 lines (636 loc) · 21.8 KB
/
53_Bulk.sql
File metadata and controls
741 lines (636 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/**************************************************************
* SQL Server 2022 BULK INSERT and Batch Operations Tutorial
* Description: This script demonstrates how to use BULK INSERT and
* batch operations in SQL Server 2022. It covers:
* - Using BULK INSERT to load data from various file formats
* - Working with BCP utility for import/export
* - Table-valued parameters for batch operations
* - Implementing error handling for bulk operations
* - Optimizing performance for large data loads
* - Transaction management in batch operations
* - Using SQL Server Integration Services (SSIS) basics
* - Monitoring bulk operations and performance tuning
**************************************************************/
-------------------------------------------------
-- Region: 1. Understanding BULK INSERT Basics
-------------------------------------------------
USE master;
GO
/*
Create a test database for our examples.
*/
IF DB_ID('BulkOperationsDemo') IS NOT NULL
BEGIN
ALTER DATABASE BulkOperationsDemo SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE BulkOperationsDemo;
END
GO
CREATE DATABASE BulkOperationsDemo;
GO
USE BulkOperationsDemo;
GO
/*
The BULK INSERT statement allows for loading data from external files directly
into SQL Server tables. It's optimized for performance and can handle
large volumes of data more efficiently than row-by-row inserts.
*/
-- Create a sample table that we will use for demonstration
CREATE TABLE dbo.Products
(
ProductID INT PRIMARY KEY,
ProductName NVARCHAR(100) NOT NULL,
Category NVARCHAR(50) NOT NULL,
Price DECIMAL(10, 2) NOT NULL,
InStock BIT NOT NULL,
LastUpdated DATETIME2 DEFAULT GETDATE()
);
GO
-- Create a directory for our data files using xp_cmdshell
-- Note: xp_cmdshell requires appropriate permissions
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
GO
EXEC sp_configure 'xp_cmdshell', 1;
RECONFIGURE;
GO
-- Create a sample CSV file in temp directory
EXEC xp_cmdshell 'mkdir C:\Temp';
GO
DECLARE @csv_content NVARCHAR(MAX) =
'ProductID,ProductName,Category,Price,InStock
1,Laptop,Electronics,1200.00,1
2,Smartphone,Electronics,800.00,1
3,Desk Chair,Furniture,250.00,1
4,Coffee Maker,Appliances,65.00,0
5,Headphones,Electronics,120.00,1';
DECLARE @cmd NVARCHAR(MAX) = 'echo ' + @csv_content + ' > C:\Temp\products.csv';
EXEC xp_cmdshell @cmd;
GO
-------------------------------------------------
-- Region: 2. Basic BULK INSERT Operations
-------------------------------------------------
/*
Basic BULK INSERT with minimal options.
*/
TRUNCATE TABLE dbo.Products;
GO
-- Simple BULK INSERT from CSV file
BULK INSERT dbo.Products
FROM 'C:\Temp\products.csv'
WITH (
FIRSTROW = 2, -- Skip header row
FIELDTERMINATOR = ',', -- CSV field delimiter
ROWTERMINATOR = '\n', -- Row terminator
KEEPNULLS -- Preserve NULL values
);
GO
-- Verify the data was loaded correctly
SELECT * FROM dbo.Products;
GO
/*
Using more advanced options with BULK INSERT.
*/
-- Create a sample table for order data
CREATE TABLE dbo.Orders
(
OrderID INT PRIMARY KEY,
CustomerID INT NOT NULL,
OrderDate DATE NOT NULL,
TotalAmount DECIMAL(10, 2) NOT NULL,
Status NVARCHAR(20) NOT NULL
);
GO
-- Create a sample tab-delimited file
DECLARE @orders_content NVARCHAR(MAX) =
'OrderID CustomerID OrderDate TotalAmount Status
1001 101 2023-01-15 520.75 Completed
1002 102 2023-01-17 340.50 Processing
1003 101 2023-01-20 1250.00 Completed
1004 103 2023-01-25 89.99 Shipped
1005 104 2023-01-30 450.25 Processing';
DECLARE @cmd NVARCHAR(MAX) = 'echo ' + @orders_content + ' > C:\Temp\orders.tsv';
EXEC xp_cmdshell @cmd;
GO
-- BULK INSERT with additional options
BULK INSERT dbo.Orders
FROM 'C:\Temp\orders.tsv'
WITH (
FIRSTROW = 2, -- Skip header row
FIELDTERMINATOR = '\t', -- Tab delimiter
ROWTERMINATOR = '\n', -- Row terminator
TABLOCK, -- Table lock for better performance
CHECK_CONSTRAINTS, -- Validate constraints during import
FIRE_TRIGGERS -- Fire any triggers during import
);
GO
-- Verify the order data was loaded correctly
SELECT * FROM dbo.Orders;
GO
-------------------------------------------------
-- Region: 3. Working with Different File Formats
-------------------------------------------------
/*
BULK INSERT supports various file formats including CSV, TSV,
fixed-width, and even XML/JSON through format files.
*/
-- Create a table for fixed-width data
CREATE TABLE dbo.Employees
(
EmployeeID INT PRIMARY KEY,
FirstName NVARCHAR(50) NOT NULL,
LastName NVARCHAR(50) NOT NULL,
Department NVARCHAR(50) NOT NULL,
Salary DECIMAL(10, 2) NOT NULL
);
GO
-- Create a fixed-width format file
DECLARE @format_content NVARCHAR(MAX) =
'<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
<FIELD ID="1" xsi:type="CharFixed" LENGTH="5"/>
<FIELD ID="2" xsi:type="CharFixed" LENGTH="15"/>
<FIELD ID="3" xsi:type="CharFixed" LENGTH="15"/>
<FIELD ID="4" xsi:type="CharFixed" LENGTH="20"/>
<FIELD ID="5" xsi:type="CharFixed" LENGTH="10"/>
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="EmployeeID" xsi:type="SQLINT"/>
<COLUMN SOURCE="2" NAME="FirstName" xsi:type="SQLNVARCHAR"/>
<COLUMN SOURCE="3" NAME="LastName" xsi:type="SQLNVARCHAR"/>
<COLUMN SOURCE="4" NAME="Department" xsi:type="SQLNVARCHAR"/>
<COLUMN SOURCE="5" NAME="Salary" xsi:type="SQLDECIMAL" PRECISION="10" SCALE="2"/>
</ROW>
</BCPFORMAT>';
DECLARE @cmd NVARCHAR(MAX) = 'echo ' + REPLACE(@format_content, CHAR(10), '') + ' > C:\Temp\employees.fmt';
EXEC xp_cmdshell @cmd;
GO
-- Create a fixed-width data file
DECLARE @fixed_content NVARCHAR(MAX) =
'10001John Smith IT 75000.00
10002Mary Jones Marketing 82500.50
10003Robert Johnson Sales 65000.00
10004Sarah Williams HR 70000.00
10005Michael Brown IT 92000.00';
DECLARE @cmd NVARCHAR(MAX) = 'echo ' + @fixed_content + ' > C:\Temp\employees.dat';
EXEC xp_cmdshell @cmd;
GO
-- BULK INSERT using format file for fixed-width data
BULK INSERT dbo.Employees
FROM 'C:\Temp\employees.dat'
WITH (
FORMATFILE = 'C:\Temp\employees.fmt',
TABLOCK
);
GO
-- Verify the employee data was loaded correctly
SELECT * FROM dbo.Employees;
GO
-------------------------------------------------
-- Region: 4. Error Handling in Bulk Operations
-------------------------------------------------
/*
Handle errors that might occur during bulk operations.
*/
-- Create a table with constraints for error demonstration
CREATE TABLE dbo.Customers
(
CustomerID INT PRIMARY KEY,
CustomerName NVARCHAR(100) NOT NULL,
Email NVARCHAR(100) NOT NULL UNIQUE,
CreditLimit DECIMAL(10, 2) NOT NULL CHECK (CreditLimit >= 0),
IsActive BIT NOT NULL
);
GO
-- Create a CSV file with some invalid data
DECLARE @customers_content NVARCHAR(MAX) =
'CustomerID,CustomerName,Email,CreditLimit,IsActive
201,John Doe,john.doe@example.com,5000.00,1
202,Jane Smith,jane.smith@example.com,3000.00,1
203,Invalid Customer,invalid-email,-500.00,1
204,Duplicate Email,john.doe@example.com,2000.00,0
205,Robert Johnson,robert.johnson@example.com,4000.00,1';
DECLARE @cmd NVARCHAR(MAX) = 'echo ' + @customers_content + ' > C:\Temp\customers.csv';
EXEC xp_cmdshell @cmd;
GO
-- BULK INSERT with error handling options
-- This will fail constraints but we can capture errors
BULK INSERT dbo.Customers
FROM 'C:\Temp\customers.csv'
WITH (
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
MAXERRORS = 2, -- Allow up to 2 errors before failing
ERRORFILE = 'C:\Temp\customers_errors.log' -- Log errors to a file
);
GO
-- Alternative approach: Use a staging table without constraints
CREATE TABLE dbo.CustomersStaging
(
CustomerID INT,
CustomerName NVARCHAR(100),
Email NVARCHAR(100),
CreditLimit DECIMAL(10, 2),
IsActive BIT
);
GO
-- Bulk insert into staging table first
BULK INSERT dbo.CustomersStaging
FROM 'C:\Temp\customers.csv'
WITH (
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
);
GO
-- Validate and insert valid records into main table
INSERT INTO dbo.Customers
SELECT s.CustomerID, s.CustomerName, s.Email, s.CreditLimit, s.IsActive
FROM dbo.CustomersStaging s
WHERE s.CreditLimit >= 0
AND NOT EXISTS (SELECT 1 FROM dbo.Customers c WHERE c.Email = s.Email);
GO
-- List records that were rejected
SELECT * FROM dbo.CustomersStaging s
WHERE s.CreditLimit < 0
OR EXISTS (SELECT 1 FROM dbo.Customers c WHERE c.Email = s.Email);
GO
-------------------------------------------------
-- Region: 5. Transaction Management for Bulk Operations
-------------------------------------------------
/*
Managing transactions for bulk operations ensures data integrity.
*/
-- Create a transaction log table
CREATE TABLE dbo.ImportLog
(
LogID INT IDENTITY(1,1) PRIMARY KEY,
ImportType NVARCHAR(50) NOT NULL,
FileName NVARCHAR(255) NOT NULL,
RecordsImported INT NOT NULL,
ImportDate DATETIME2 NOT NULL DEFAULT GETDATE(),
Status NVARCHAR(20) NOT NULL
);
GO
-- Create a sample CSV file for inventory updates
DECLARE @inventory_content NVARCHAR(MAX) =
'ProductID,QuantityChange,UpdateType,UpdateDate
1,50,Restock,2023-03-01
2,-15,Sale,2023-03-01
3,30,Restock,2023-03-02
4,100,Restock,2023-03-02
5,-10,Sale,2023-03-02';
DECLARE @cmd NVARCHAR(MAX) = 'echo ' + @inventory_content + ' > C:\Temp\inventory_updates.csv';
EXEC xp_cmdshell @cmd;
GO
-- Create an inventory table
CREATE TABLE dbo.Inventory
(
ProductID INT PRIMARY KEY,
QuantityOnHand INT NOT NULL DEFAULT 0,
LastUpdated DATETIME2 NOT NULL DEFAULT GETDATE(),
CONSTRAINT FK_Inventory_Products FOREIGN KEY (ProductID) REFERENCES dbo.Products(ProductID)
);
GO
-- Initialize inventory
INSERT INTO dbo.Inventory (ProductID, QuantityOnHand)
SELECT ProductID, CASE WHEN InStock = 1 THEN 100 ELSE 0 END
FROM dbo.Products;
GO
-- Create a staging table for inventory updates
CREATE TABLE dbo.InventoryUpdatesStaging
(
ProductID INT,
QuantityChange INT,
UpdateType NVARCHAR(50),
UpdateDate DATE
);
GO
-- Import data and apply updates within a transaction
BEGIN TRY
BEGIN TRANSACTION;
-- Import data to staging
BULK INSERT dbo.InventoryUpdatesStaging
FROM 'C:\Temp\inventory_updates.csv'
WITH (
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
TABLOCK
);
-- Count records imported
DECLARE @RecordsImported INT = @@ROWCOUNT;
-- Apply updates to inventory
UPDATE i
SET i.QuantityOnHand = i.QuantityOnHand + s.QuantityChange,
i.LastUpdated = GETDATE()
FROM dbo.Inventory i
JOIN dbo.InventoryUpdatesStaging s ON i.ProductID = s.ProductID;
-- Check for negative inventory (business rule)
IF EXISTS (SELECT 1 FROM dbo.Inventory WHERE QuantityOnHand < 0)
BEGIN
RAISERROR('Inventory cannot be negative. Rolling back transaction.', 16, 1);
END
-- Log successful import
INSERT INTO dbo.ImportLog (ImportType, FileName, RecordsImported, Status)
VALUES ('Inventory Update', 'inventory_updates.csv', @RecordsImported, 'Success');
-- If everything succeeded, commit the transaction
COMMIT TRANSACTION;
PRINT 'Inventory updates applied successfully.';
END TRY
BEGIN CATCH
-- If an error occurred, roll back the transaction
IF @@TRANCOUNT > 0
ROLLBACK TRANSACTION;
-- Log the error
INSERT INTO dbo.ImportLog (ImportType, FileName, RecordsImported, Status)
VALUES ('Inventory Update', 'inventory_updates.csv', 0, 'Failed: ' + ERROR_MESSAGE());
PRINT 'Error: ' + ERROR_MESSAGE();
END CATCH;
GO
-- Verify the inventory updates
SELECT p.ProductID, p.ProductName, i.QuantityOnHand, i.LastUpdated
FROM dbo.Products p
JOIN dbo.Inventory i ON p.ProductID = i.ProductID;
GO
-------------------------------------------------
-- Region: 6. Working with BCP Utility
-------------------------------------------------
/*
BCP (Bulk Copy Program) is a command-line utility that bulk copies data
between a SQL Server instance and a data file in a specified format.
*/
-- Create a stored procedure to generate BCP commands
CREATE OR ALTER PROCEDURE dbo.GenerateBCPCommands
@TableName NVARCHAR(128),
@SchemaName NVARCHAR(128) = 'dbo',
@FilePath NVARCHAR(256),
@FormatFile NVARCHAR(256) = NULL,
@Options NVARCHAR(MAX) = NULL
AS
BEGIN
DECLARE @BCPExport NVARCHAR(MAX);
DECLARE @BCPImport NVARCHAR(MAX);
DECLARE @FullTableName NVARCHAR(256) = QUOTENAME(@SchemaName) + '.' + QUOTENAME(@TableName);
DECLARE @DatabaseName NVARCHAR(128) = DB_NAME();
-- Generate export command
SET @BCPExport = 'bcp ' + @FullTableName + ' out ' + @FilePath +
' -S "' + @@SERVERNAME + '" -d ' + @DatabaseName + ' -T';
-- Add format file option if provided
IF @FormatFile IS NOT NULL
SET @BCPExport = @BCPExport + ' -f "' + @FormatFile + '"';
-- Add any other options
IF @Options IS NOT NULL
SET @BCPExport = @BCPExport + ' ' + @Options;
-- Generate import command
SET @BCPImport = 'bcp ' + @FullTableName + ' in ' + @FilePath +
' -S "' + @@SERVERNAME + '" -d ' + @DatabaseName + ' -T';
-- Add format file option if provided
IF @FormatFile IS NOT NULL
SET @BCPImport = @BCPImport + ' -f "' + @FormatFile + '"';
-- Add any other options
IF @Options IS NOT NULL
SET @BCPImport = @BCPImport + ' ' + @Options;
-- Return the commands
SELECT
@BCPExport AS ExportCommand,
@BCPImport AS ImportCommand;
END;
GO
-- Generate BCP commands for tables
EXEC dbo.GenerateBCPCommands
@TableName = 'Products',
@FilePath = 'C:\Temp\products_export.dat',
@Options = '-c -t, -r\n';
GO
EXEC dbo.GenerateBCPCommands
@TableName = 'Employees',
@FilePath = 'C:\Temp\employees_export.dat',
@FormatFile = 'C:\Temp\employees.fmt';
GO
-- Example of what a BCP command execution would look like
-- Note: These are commented out since xp_cmdshell execution of BCP
-- requires specific server configurations
/*
DECLARE @BCPCmd NVARCHAR(MAX) = 'bcp dbo.Products out C:\Temp\products_export.dat -S ' +
@@SERVERNAME + ' -T -c -t, -r\n';
EXEC xp_cmdshell @BCPCmd;
GO
*/
-------------------------------------------------
-- Region: 7. Table-Valued Parameters for Batch Operations
-------------------------------------------------
/*
Table-Valued Parameters (TVPs) allow you to pass multiple rows of data
to a stored procedure in a structured way, ideal for batch operations.
*/
-- Create a user-defined table type
CREATE TYPE dbo.ProductUpdateTableType AS TABLE
(
ProductID INT PRIMARY KEY,
ProductName NVARCHAR(100),
Category NVARCHAR(50),
Price DECIMAL(10, 2),
InStock BIT
);
GO
-- Create a stored procedure that accepts a TVP
CREATE OR ALTER PROCEDURE dbo.BulkUpdateProducts
@ProductUpdates dbo.ProductUpdateTableType READONLY
AS
BEGIN
SET NOCOUNT ON;
-- Create a temporary table to track changes
CREATE TABLE #UpdatedProducts
(
ProductID INT PRIMARY KEY,
OldPrice DECIMAL(10, 2),
NewPrice DECIMAL(10, 2),
PriceDifference DECIMAL(10, 2)
);
-- Update products and capture changes
UPDATE p
SET
p.ProductName = ISNULL(u.ProductName, p.ProductName),
p.Category = ISNULL(u.Category, p.Category),
p.Price = ISNULL(u.Price, p.Price),
p.InStock = ISNULL(u.InStock, p.InStock),
p.LastUpdated = GETDATE()
OUTPUT
INSERTED.ProductID,
DELETED.Price AS OldPrice,
INSERTED.Price AS NewPrice,
INSERTED.Price - DELETED.Price AS PriceDifference
INTO #UpdatedProducts
FROM dbo.Products p
INNER JOIN @ProductUpdates u ON p.ProductID = u.ProductID;
-- Return summary of changes
SELECT
COUNT(*) AS TotalProductsUpdated,
SUM(CASE WHEN PriceDifference > 0 THEN 1 ELSE 0 END) AS PriceIncreases,
SUM(CASE WHEN PriceDifference < 0 THEN 1 ELSE 0 END) AS PriceDecreases,
AVG(ABS(PriceDifference)) AS AveragePriceChange
FROM #UpdatedProducts;
-- Return detailed changes
SELECT * FROM #UpdatedProducts
ORDER BY ABS(PriceDifference) DESC;
END;
GO
-- Use the TVP to update multiple products at once
DECLARE @ProductUpdates dbo.ProductUpdateTableType;
-- Populate the table variable
INSERT INTO @ProductUpdates (ProductID, ProductName, Category, Price, InStock)
VALUES
(1, 'High-End Laptop', 'Electronics', 1500.00, 1),
(2, 'Premium Smartphone', 'Electronics', 950.00, 1),
(3, NULL, NULL, 275.00, NULL),
(4, NULL, NULL, 59.99, 1),
(5, 'Wireless Headphones', NULL, 149.99, NULL);
-- Execute the stored procedure
EXEC dbo.BulkUpdateProducts @ProductUpdates;
GO
-- Verify the updates
SELECT * FROM dbo.Products;
GO
-------------------------------------------------
-- Region: 8. Bulk Copy Performance Optimization
-------------------------------------------------
/*
Optimize BULK INSERT operations for maximum performance.
*/
-- Create a large test table for performance testing
CREATE TABLE dbo.LargeDataTable
(
ID INT IDENTITY(1,1) PRIMARY KEY,
Column1 NVARCHAR(100),
Column2 NVARCHAR(100),
Column3 NVARCHAR(100),
Column4 DECIMAL(18,2),
Column5 DECIMAL(18,2),
Column6 DATE,
Column7 BIT
);
GO
-- Generate a larger test data file
CREATE OR ALTER PROCEDURE dbo.GenerateTestDataFile
@RowCount INT,
@FilePath NVARCHAR(256)
AS
BEGIN
SET NOCOUNT ON;
-- Generate CSV header
DECLARE @header NVARCHAR(MAX) = 'Column1,Column2,Column3,Column4,Column5,Column6,Column7';
DECLARE @cmd NVARCHAR(MAX) = 'echo ' + @header + ' > ' + @FilePath;
EXEC xp_cmdshell @cmd;
-- Generate CSV data in batches to avoid memory issues
DECLARE @i INT = 0;
DECLARE @batchSize INT = 1000;
DECLARE @csvData NVARCHAR(MAX);
WHILE @i < @RowCount
BEGIN
SET @csvData = '';
DECLARE @j INT = 0;
WHILE @j < @batchSize AND @i < @RowCount
BEGIN
SET @csvData = @csvData +
'Value' + CAST(@i AS NVARCHAR(10)) + ',' +
'Category' + CAST(@i % 10 AS NVARCHAR(10)) + ',' +
'Description' + CAST(@i % 20 AS NVARCHAR(10)) + ',' +
CAST((@i * 10.25) AS NVARCHAR(20)) + ',' +
CAST((@i * 5.75) AS NVARCHAR(20)) + ',' +
CONVERT(NVARCHAR(10), DATEADD(DAY, @i % 1000, '2020-01-01'), 120) + ',' +
CAST(@i % 2 AS NVARCHAR(1)) + CHAR(13) + CHAR(10);
SET @i = @i + 1;
SET @j = @j + 1;
END
-- Append to the file
SET @cmd = 'echo ' + @csvData + ' >> ' + @FilePath;
EXEC xp_cmdshell @cmd;
END
PRINT 'Generated ' + CAST(@RowCount AS NVARCHAR(10)) + ' rows of test data to ' + @FilePath;
END;
GO
-- Generate a test file with 10,000 rows (adjust as needed)
EXEC dbo.GenerateTestDataFile @RowCount = 10000, @FilePath = 'C:\Temp\large_data.csv';
GO
-- Test different BULK INSERT configurations for performance
-- 1. Basic BULK INSERT
TRUNCATE TABLE dbo.LargeDataTable;
GO
DECLARE @StartTime DATETIME2 = GETDATE();
BULK INSERT dbo.LargeDataTable
FROM 'C:\Temp\large_data.csv'
WITH (
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
);
PRINT 'Basic BULK INSERT took: ' +
CAST(DATEDIFF(MILLISECOND, @StartTime, GETDATE()) AS NVARCHAR(10)) + 'ms';
GO
-- 2. Optimized BULK INSERT with TABLOCK and batching
TRUNCATE TABLE dbo.LargeDataTable;
GO
DECLARE @StartTime DATETIME2 = GETDATE();
BULK INSERT dbo.LargeDataTable
FROM 'C:\Temp\large_data.csv'
WITH (
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
TABLOCK, -- Table lock for better performance
ROWS_PER_BATCH = 2500, -- Number of rows per batch
BATCHSIZE = 2500, -- Size of batches in rows
ORDER (Column1), -- Order for better memory usage with clustered index
DATAFILETYPE = 'char' -- Specify data type for non-Unicode data
);
PRINT 'Optimized BULK INSERT took: ' +
CAST(DATEDIFF(MILLISECOND, @StartTime, GETDATE()) AS NVARCHAR(10)) + 'ms';
GO
-- 3. Minimal logging with simple recovery model
-- Note: In a production environment, consider the implication of changing recovery models
ALTER DATABASE BulkOperationsDemo SET RECOVERY SIMPLE;
GO
TRUNCATE TABLE dbo.LargeDataTable;
GO
DECLARE @StartTime DATETIME2 = GETDATE();
BULK INSERT dbo.LargeDataTable
FROM 'C:\Temp\large_data.csv'
WITH (
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
TABLOCK,
ROWS_PER_BATCH = 2500
);
PRINT 'Minimal logging BULK INSERT took: ' +
CAST(DATEDIFF(MILLISECOND, @StartTime, GETDATE()) AS NVARCHAR(10)) + 'ms';
GO
-- Reset recovery model to FULL for normal operations
ALTER DATABASE BulkOperationsDemo SET RECOVERY FULL;
GO
-------------------------------------------------
-- Region: 9. Monitoring Bulk Operations
-------------------------------------------------
/*
Track and monitor bulk operations for troubleshooting and optimization.
*/
-- Create a monitoring table for bulk operations
CREATE TABLE dbo.BulkOperationsLog
(
LogID INT IDENTITY(1,1) PRIMARY KEY,
OperationType NVARCHAR(50) NOT NULL,
TableName NVARCHAR(128) NOT NULL,
StartTime DATETIME2 NOT NULL,
EndTime DATETIME2 NULL,
RowsAffected BIGINT NULL,
Status NVARCHAR(20) NULL,
ErrorMessage NVARCHAR(MAX) NULL
);
GO
-- Create a stored procedure to monitor and log bulk operations
CREATE OR ALTER PROCEDURE dbo.LoggedBulkInsert
@TableName NVARCHAR(128),
@FilePath NVARCHAR(256),
@Options NVARCHAR(MAX) = NULL