Reputation: 3663
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.
Upvotes: 4
Views: 4864
Reputation: 3663
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.
Upvotes: 5
Reputation:
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.
Upvotes: 2