Skip to content

Commit e4ad39e

Browse files
committed
improved search when SQL doesn't return any result
1 parent 7334bcf commit e4ad39e

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

db/sql/04-find_samples.sql

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,27 @@ if (@response is null) begin
3030
--print @rt
3131
--print @rq
3232

33+
/* Find the samples using T-SQL */
34+
if (@rt = 'SQL') begin
35+
declare @trq nvarchar(max) = trim(replace(replace(@rq, char(13), ' '), char(10), ' '));
36+
if (@trq like '%INSERT %' or @trq like '%UPDATE %' or @trq like '%DELETE %' or @trq like '%DROP %' or @trq like '%ALTER %' or @trq like '%CREATE %') begin
37+
--select @trq
38+
select 'NL2SQL' as [error], -1 as [error_code], 'Unauthorized SQL command requested' as [response]
39+
return -1
40+
end
41+
42+
declare @q nvarchar(max) = 'SET @s = (' + @rq + ')';
43+
exec sp_executesql @q, N'@s NVARCHAR(MAX) OUTPUT', @s = @samples output
44+
--print @samples
45+
end
46+
47+
/* If not results coming from SQL execution, try SEMANTIC anyway */
48+
if (@samples is null) begin
49+
set @rt = 'SQL+SEMANTIC'
50+
end
51+
3352
/* Find the samples most similar to the requested topic */
34-
if (@rt = 'SEMANTIC') begin
53+
if (@rt like '%SEMANTIC%') begin
3554
set @k = coalesce(@k, 50)
3655
drop table if exists #s;
3756
select top(@k)
@@ -65,19 +84,7 @@ if (@response is null) begin
6584
)
6685
end
6786

68-
/* Find the samples using T-SQL */
69-
if (@rt = 'SQL') begin
70-
declare @trq nvarchar(max) = trim(replace(replace(@rq, char(13), ' '), char(10), ' '));
71-
if (@trq like '%INSERT %' or @trq like '%UPDATE %' or @trq like '%DELETE %' or @trq like '%DROP %' or @trq like '%ALTER %' or @trq like '%CREATE %') begin
72-
--select @trq
73-
select 'NL2SQL' as [error], -1 as [error_code], 'Unauthorized SQL command requested' as [response]
74-
return -1
75-
end
76-
77-
declare @q nvarchar(max) = 'SET @s = (' + @rq + ')';
78-
exec sp_executesql @q, N'@s NVARCHAR(MAX) OUTPUT', @s = @samples output
79-
--print @samples
80-
end
87+
8188

8289
--select @samples;
8390
if (@samples is not null) begin

0 commit comments

Comments
 (0)