Skip to content

Commit 2969910

Browse files
committed
adding some configs
1 parent 5637aae commit 2969910

File tree

1 file changed

+29
-15
lines changed

1 file changed

+29
-15
lines changed

script-generation/GetInsert_MongoShell.sql

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,19 @@
77
--======================================================
88
DROP PROCEDURE IF EXISTS [dbo].GetInsert_MongoShell
99
GO
10-
CREATE PROCEDURE [dbo].GetInsert_MongoShell @TableSchema sysname = 'dbo',
11-
@TableName sysname,
10+
CREATE PROCEDURE [dbo].GetInsert_MongoShell @TableName sysname,
11+
@TableSchema sysname = 'dbo',
12+
@Top INT = NULL,
1213
@Where nvarchar(4000) = '',
13-
@ExportPath nvarchar(255) = 'C:\Temp\'
14+
@ExportPath nvarchar(255) = NULL,
15+
@ExportOverrideServerInstance nvarchar(255) = NULL,
16+
@OverrideDestinationCollection nvarchar(255) = NULL
1417
AS
1518
BEGIN
1619
SET NOCOUNT ON;
1720

1821
DECLARE @vCommand nvarchar(4000)
22+
DECLARE @vCollectionName nvarchar(255) = COALESCE(@OverrideDestinationCollection, @TableName)
1923
DECLARE @vReturnCode INT
2024

2125
SET @ExportPath += '<table-name>-<yyyyMMdd-HHmmss>.js'
@@ -43,41 +47,51 @@ BEGIN
4347
SET @vCommand = '
4448
DECLARE @vJsonData nvarchar(max)
4549
SELECT @vJsonData = (
46-
SELECT *
50+
SELECT TOP (@Top) *
4751
FROM <schema>.<table>
4852
<where>
4953
FOR JSON AUTO
5054
)
51-
INSERT INTO ##tJsonData SELECT ''db.createCollection("<table>");db.<table>.remove({});db.<table>.insert('' + @vJsonData + '')'''
55+
INSERT INTO ##tJsonData SELECT ''db.createCollection("<collection>");db.<collection>.remove({});db.<collection>.insert('' + @vJsonData + '')'''
5256

5357
SET @vCommand = REPLACE(@vCommand, '<schema>', @TableSchema)
5458
SET @vCommand = REPLACE(@vCommand, '<table>', @TableName)
59+
SET @vCommand = REPLACE(@vCommand, '<collection>', @vCollectionName)
5560
IF COALESCE(LTRIM(RTRIM(@Where)),'') <> ''
5661
SET @Where = 'WHERE ' + @Where
5762
ELSE
5863
SET @Where = ''
5964
SET @vCommand = REPLACE(@vCommand, '<where>', @Where)
6065

61-
EXEC sp_executesql @vCommand
66+
EXEC sp_executesql @vCommand, N'@Top INT', @Top
6267

6368
--Export to js file using sqlcmd
64-
--SET @vCommand = 'sqlcmd -Q "SET NOCOUNT ON; SELECT * FROM ##tJsonData" -S ' + @@SERVERNAME + ' -E -h -1 -y 0 -o "' + @vExportPath + '"'
65-
--EXEC @vReturnCode = xp_cmdshell @vCommand, no_output
66-
67-
SET @vCommand = 'bcp "SELECT InsertScript FROM ##tJsonData" QUERYOUT "' + @vExportPath + '" -T -w -S "' + @@SERVERNAME+ '"'
68-
RAISERROR(@vCommand,0,1) WITH NOWAIT
69-
EXEC @vReturnCode = xp_cmdshell @vCommand, no_output
70-
IF @vReturnCode <> 0
69+
IF @ExportPath IS NOT NULL
7170
BEGIN
72-
RAISERROR('Exporting has been failed',16,1) WITH NOWAIT
73-
RETURN -1
71+
SET @vCommand = 'bcp "SELECT InsertScript FROM ##tJsonData" QUERYOUT "' + @vExportPath + '" -T -w -S "' + COALESCE(@ExportOverrideServerInstance, @@SERVERNAME) + '"'
72+
RAISERROR(@vCommand,0,1) WITH NOWAIT
73+
EXEC @vReturnCode = xp_cmdshell @vCommand, no_output
74+
IF @vReturnCode <> 0
75+
BEGIN
76+
RAISERROR('Exporting has been failed',16,1) WITH NOWAIT
77+
RETURN -1
78+
END
79+
END
80+
ELSE
81+
BEGIN
82+
SELECT InsertScript FROM ##tJsonData
7483
END
7584

7685
RETURN
7786
END
7887
GO
7988
/*
8089
--This sample will get results of https://github.com/datnguye/SQL-Server/blob/master/web-call/ApiCovid19.sql to generate mongo shell script
90+
91+
EXEC GetInsert_MongoShell @TableName='ApiCovid19Route'
92+
EXEC GetInsert_MongoShell @TableName='ApiCovid19CountryDayOne', @Top = 10, @Where = 'CountryCode = ''vn'''
93+
EXEC GetInsert_MongoShell @TableName='ApiCovid19Route'
94+
8195
EXEC GetInsert_MongoShell @TableName='ApiCovid19Route', @ExportPath = 'C:\Temp\'
8296
EXEC GetInsert_MongoShell @TableName='ApiCovid19Countries', @ExportPath = 'C:\Temp\'
8397
EXEC GetInsert_MongoShell @TableName='ApiCovid19CountryDayOne', @ExportPath = 'C:\Temp\'

0 commit comments

Comments
 (0)