1

Due to a bug in SQL Server 2019 Linux, a node-mssql GET query that should be one single statement:

router.get('/getresult/:srid/:x/:y', function (req, res) {
    sql.connect(config, error => {
        new sql.Request()
            .input('srid', sql.Int, req.params.srid)
            .input('x', sql.Float, req.params.x)
            .input('y', sql.Float, req.params.y)
            .query('SELECT * from dbo.bTest(geometry::Point(@x,@y,@srid))', (error, result) => {
                         res.send(result.recordset);
                    })
            if (error) {
                console.error(`error: ${error.message}`);
            }
    })
});

Now needs to be two statements. The query that crashes SQL Server 2019 Linux is

SELECT * from dbo.bTest(geometry::Point(@x,@y,@srid))

The test table-valued function is this:

create function dbo.bTest ( @p_geom geometry )
Returns @geometries TABLE
(
  id integer,
  geom geometry
)
as
begin
  declare @g geometry;
  set @g = @p_geom.STBuffer(0.5);      
  insert into @geometries values (1, @g);
return;
end;

Until there's a fix, the workaround I've found is to use two statements in the query:

DECLARE @wkt geometry = geometry::Point(@x,@y,@srid);
SELECT * FROM dbo.bTest(@wkt);

As you can see in the following dbfiddle, the two statement query does not crash SQL Server 2019 Linux when it is executed the second time, whereas the single statement query crashes SQL Server 2019 Linux on the second execution:

https://dbfiddle.uk?rdbms=sqlserver_2019l&fiddle=4a7759a742f361fc075f35ae6fbdc186

So, is it possible in the node-mssql GET request to use two statements (DECLARE and SELECT) or will I need to put the query in a stored procedure? And if I put it in a stored procedure, can I use a node-mssql GET request to execute a stored procedure and get values back? I'm getting back a result.recordset that can consist of one or more results.

EDIT:

Big thanks to @David for what appears to be the answer. This seems to work in the node-mssql GET request (query):

SET NOCOUNT ON;
DECLARE @geom geometry = geometry::Point(@x,@y,@srid);
SELECT * FROM dbo.bTest(@geom);

It works multiple times in succession and doesn't crash SQL Server 2019 Linux. So then the resulting node-mssql router.get is:

router.get('/getresult/:srid/:x/:y', function (req, res) {
    sql.connect(config, error => {
        new sql.Request()
            .input('srid', sql.Int, req.params.srid)
            .input('x', sql.Float, req.params.x)
            .input('y', sql.Float, req.params.y)
            .query('SET NOCOUNT ON; \
                DECLARE @geom geometry = geometry::Point(@x,@y,@srid)); \
                SELECT * from dbo.bTest(@geom);', (error, result) => {
                         res.send(result.recordset);
                    })
            if (error) {
                console.error(`error: ${error.message}`);
            }
    })
});

Wasn't aware you could use multiple statements in a node-mssql query. Later on though, when I want to push this to production, I'm most likely going to want to use prepared statements. The way I understand it, semi-colons are verboten in prepared statements. Is it possible to (safely) query using more than one statement using prepared statements without having to use a stored procedure? (Sorry for the additional question, just want to make sure the workaround works for prepared statements as well, hopefully this SQL Server 2019 Linux bug gets fixed soon).

Rayner
  • 128
  • 9
  • And this fails on Windows too. Has it been repored? – David Browne - Microsoft Nov 19 '21 at 19:28
  • @DavidBrowne-Microsoft I've only seen this bug occurring in SQL Server 2019 Linux and nowhere else. I've tested it under SQL Server 2019 and 2017 Windows and it didn't crash. I wanted to report this as a bug but am not willing to pay $500 for a Microsoft support request. So, I reported it here https://feedback.azure.com/d365community/idea/afabe473-4c48-ec11-a81a-6045bd78b970 and here https://learn.microsoft.com/en-us/answers/questions/632037/bug-query-using-stgeomfromtext-twice-against-a-tab.html Not sure if these are the right places to report bugs. – Rayner Nov 20 '21 at 03:47
  • @DavidBrowne-Microsoft I reported the bug here as well https://feedback.azure.com/d365community/idea/2db851f6-b749-ec11-a81a-6045bd78b970 – Rayner Nov 20 '21 at 04:14

1 Answers1

1

Should work fine. Clients don't really know if a batch contains multiple statements. In the general case you may need to add SET NOCOUNT ON at the top of the batch to suppress sending row counts to the client for each statement.

David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67