4

Right now, I am trying to get rid of some old searching code in a delphi programm, and I want to push the searching to the sql-server the program is using. For this I created this query, which is very similar to a query I use in a different C# program.

The query works just fine using the SQL Management Studio or from a C# program, but with Delphi I get a "Cursor not returned from query" error.

This is the query

DECLARE @SearchString NVARCHAR(MAX);
SET @SearchString = ':Param1';
IF @SearchString = '' SET @SearchString = '%';

--Table for splitted values
DECLARE @SearchItms TABLE
(
Item NVARCHAR(MAX)
)

--Split Operator
DECLARE @SplitOn NVARCHAR(MAX)
SET @SplitOn = ' '; --Split Keywords on space

--Splitting
While (Charindex(@SplitOn,@SearchString)>0)
Begin
    Insert Into @SearchItms (Item)
    Select ('%' + Substring(@SearchString,1,Charindex(@SplitOn,@SearchString)-1) + '%')

    Set @SearchString = Substring(@SearchString,Charindex(@SplitOn,@SearchString)+1,len(@SearchString))
End

--Add last Item
Insert Into @SearchItms (Item) 
Select ('%' + @SearchString + '%')

--Select fitting items
SELECT MyTable.*
FROM MyTable INNER JOIN SecondTable ON (MyTable.Key = SecondTable.Key)
WHERE
NOT EXISTS(
  SELECT * FROM @SearchItms WHERE NOT(
     (OneField IS NOT NULL AND OneField LIKE Item)
  OR (OneDateField IS NOT NULL AND (convert(varchar, OneDateField, 104) LIKE Item) OR (convert(varchar, OneDateField, 114) LIKE Item ) )
  OR (AnotherField IS NOT NULL AND AnotherField LIKE Item)
  OR (LastField IS NOT NULL AND LastField LIKE Item)
  )
)

One strange thing is, that when I remove

--Add last Item
Insert Into @SearchItms (Item) 
Select ('%' + @SearchString + '%')

I don't get the "Cursor not returned from query" error. But with a search string, where I get only 10 Results in the SQL Management Studio, I get all items in the database in Delphi, like the search did not do anything.

This happens also in a simple test program where I just have a Form, a TSQLConnection, TSQLQuery, TDataSetProvider and a ClientDataSet.

Can someone tell me whats the problem here? Since the query works fine in other environments, I think it should be OK.

Many thanks for any help.

2 Answers 2

5

As ldsandon mentioned in his post, the problem seems to be, that its not a simple Query, but a complete script. But since non of the other dbExpress units seems to be able to handle this I found a way to make it working.

Just add a SET NOCOUNT ON; to the beginning of the script, and close the script with SET NOCOUNT OFF;

I think this surpresses the 'xx rows effected' message from the inserts and therefore makes it possible to run without errors.

If you know a better way to run the script without errors, let me know.

Thanks.

2
  • yeah, started the same answer, but you wrote it faster :P
    – Rodrigo
    Jun 27, 2011 at 10:25
  • I have the same problem with simple "DELETE FROM WHERE id=:id" Mar 17, 2014 at 13:30
2

This is not a query - this is a script. Usually a Delphi TQuery component can handle a single SQL statement, not multiple ones (you have INSERTs first and then a SELECT). It may be the INSERT that obviously does not return a cursor. You can try to use a TSQLDataset and see if it can handle such a kind of SQL Server "anonymous block", or turn it into a stored procedure and call it from your Delphi program.

2
  • It seems like the Problem is really that it is more a script than a single query. Unfortunately the TSQLDataSet and also a stored procedure with a TStoredProc also can't handle it. But I found a Solution I will post as Answer.
    – Marks
    Jun 27, 2011 at 10:08
  • To use it as stored procedure you have to convert it to a real stored procedure. It should also be more performant that way, no need to reparse it each time.
    – user160694
    Jun 27, 2011 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.