0

I have to retrieve some data from a MSSQL 2014 server through a propriatery application which then uses an odbc data source. The only thing I can modify is the query the application uses. I cannot modify the application or how the application handles the results.

The following query is doing what I want if I execute it directly e.g. in Heidi.

USE MY_DB;

BEGIN TRANSACTION

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

DECLARE @myvar1 INT = 2;
DECLARE @myvar2 INT = 2;

PRINT @myvar1;

SELECT TOP 20 [vwPGIA].[OrNum],[vwPGIA].[DBM],[vwPGIA].[MBM],[vwPGIA].[MN],[NOMID],[Priority],SUBSTRING([Comment],0,254) AS Comment,[TLSAP],[Box],[SequenceNumber] 
INTO #tmp_tbl
FROM [MY_DB].[dbo].[vwPGIA] 
  INNER JOIN [MY_DB].[dbo].[tblDLA] ON [dbo].[tblDLA].[OrNum]=[dbo].[vwPGIA].[OrNum]
  INNER JOIN [dbo].[tblMDM] ON [vwPGIA].[MBM]=[tblMDM].[MBM]
WHERE ([TLSAP] = @myvar1) 
  AND [vwPGIA].[MBM] NOT IN (SELECT [MBM] FROM [MY_DB].[dbo].[vwDPS])
  AND [vwPGIA].[OrNum] NOT IN (SELECT [OrNum] FROM [MY_DB].[dbo].[vwDPS] WHERE  [MY_DB].[dbo].[vwDPS].[TLR] <> @myvar1)
ORDER BY [SequenceNumber];

SELECT TOP 1 [OrNum],[DBM],[MBM],[MN],[NOMID],[Priority],[Comment],[TLSAP],[Box],[WTT],[SequenceNumber]
FROM #tmp_tbl 
  INNER JOIN [dbo].[tblTBN] ON [Box]=[BoxN] 
WHERE ([WTT]=@myvar2)
ORDER BY [SequenceNumber];


INSERT INTO [dbo].[tblDPS]
    (OrNum,DBM,MBM,State,StateStartTime,Info,TLR)
        SELECT TOP 1 [OrNum],[DBM],[MBM],'1',GETDATE(),'info',@myvar1
        FROM #tmp_tbl 
        INNER JOIN [dbo].[tblTBN] ON [Box]=[BoxN] 
        WHERE ([WTT]=@myvar2)
        ORDER BY [SequenceNumber]
    ;

DROP TABLE #tmp_tbl;

COMMIT TRANSACTION

Running this through the ODBC interface results in an empty result. The problem seems to be, that I am doing a batch request which results in multiple result sets. The application probably only handles the first result set or maybe cannot handle more than one result set.

Finally the question: Is there a way or workaround to reduce the result sets to only the one returned by the SELECT TOP 1 ... part?

  • I can only see one result set being returned in the above. You have a `SELECT...INTO` and `INSERT INTO` statement; which won't return data to the presentation layer. – Thom A Nov 28 '19 at 14:37
  • 1
    Do you really retrieve more than one resultset? `SELECT TOP 1...` should be the only one. Try [suppressing messages](https://stackoverflow.com/questions/2014129/is-there-a-way-to-suppress-x-rows-affected-in-sqlcmd-from-the-command-line) with `SET NOCOUNT ON` as well as removing the `PRINT @myvar1` statement. – Steffen Bobek Nov 28 '19 at 14:38
  • I tried the other way round, starting with a simple query and adding more and more. `PRINT`, `USE`, `SELECT ... INTO` all seem to add something to the results that cannot be handled. Is there any tool that lets me debug what happens on the ODBC channel? I tried this http://www.sqledit.com/odbc/download.html#runner but it shows the same behaviour of the application. – user2790498 Nov 28 '19 at 14:51
  • Could be that whatever intermediary application you have can't sniff the metadata results. I.e. frameworks like Entity Framework can have problems reading metadata from stored procedures that combine results in a temporary table and then eventually return it all. In the case of Entity Framework binding, we needed to include an explicit `SET FMTONLY OFF` at the start of a procedure to get it to be binded correctly. – D Kramer Nov 28 '19 at 14:53
  • @Pilosa `SET NOCOUNT ON` did it! I also had to remove the `PRINT` and `USE MY_DB;` – user2790498 Nov 28 '19 at 15:09

0 Answers0