[Resolved]Bug with return data from Dynamic Stored Procedure
Posted: Sun 18 Dec 2011 17:32
Hello,
I have a problem with Devart driver, where I have a stored procedure in SQL Server 2008 that returns records through instruction Exec (@ cmd) with dynamic SQL.
In Delphi, Activate the stored procedure the error occurs: Cursor not returned from Query.
Because this message occurs if the procedure in Sql Server Management Studio returns the records correctly?
I use Delphi XE2 and already tested with the components TSQLDataSet, and TSQLQuery TStoredProc.
Below I am sending the code to someone able to create the structure in the database and test with Delphi.
- Creates the schemas
CREATE SCHEMA Emp1
GO
CREATE SCHEMA Emp2
GO
CREATE SCHEMA Emp3
GO
- Creates the tables
CREATE TABLE Emp1.Atividade (IDUsuario INT Anything VARCHAR (100))
CREATE TABLE Emp2.Atividade (IDUsuario INT Anything VARCHAR (100))
CREATE TABLE Emp3.Atividade (IDUsuario INT Anything VARCHAR (100))
CREATE TABLE dbo.Usuario (INT IDUsuario, UserName VARCHAR (20))
- Insert the data
Dbo.Usuario INSERT INTO VALUES (1, 'User 1')
Dbo.Usuario INSERT INTO VALUES (2, 'User 2')
Dbo.Usuario INSERT INTO VALUES (4, 'User 4')
Emp1.Atividade INSERT INTO VALUES (1, 'user activities Company 1')
Emp2.Atividade INSERT INTO VALUES (2, 'user activities Company 2')
Emp3.Atividade INSERT INTO VALUES (4, 'User Company Activities 3')
- Create a Procedure to return the records to the Program
ALTER PROCEDURE [dbo]. [Test]
AS
BEGIN
- SET NOCOUNT ON added to Prevent extra result sets from
- Interfering with SELECT statements.
SET NOCOUNT ON;
- Build a string concatenation of commands
DECLARE @ Table VARCHAR (MAX)
SET @ Table = (
SELECT 'SELECT * FROM' + SCHEMA_NAME (SCHEMA_ID) +
. 'UNION ALL Activity' The Name FROM sys.tables
WHERE name = 'Activity'
FOR XML RAW ('Table'), ROOT ('tables'))
SET @ Table =
REPLACE (
REPLACE (
REPLACE (
REPLACE (@ Table, '','')
, 'UNION ALL ','') Tables
, '','')
DECLARE @ cmd VARCHAR (MAX)
SET @ cmd = '; Activities The WITH (' + @ Tables + ')' + CHAR (10) + CHAR (10)
SET @ cmd = @ cmd + 'SELECT * FROM User INNER JOIN The S ON S. The Activities The IDUsuario IDUsuario = A.'
PRINT @ cmd
EXEC (@ cmd)
END
I have a problem with Devart driver, where I have a stored procedure in SQL Server 2008 that returns records through instruction Exec (@ cmd) with dynamic SQL.
In Delphi, Activate the stored procedure the error occurs: Cursor not returned from Query.
Because this message occurs if the procedure in Sql Server Management Studio returns the records correctly?
I use Delphi XE2 and already tested with the components TSQLDataSet, and TSQLQuery TStoredProc.
Below I am sending the code to someone able to create the structure in the database and test with Delphi.
- Creates the schemas
CREATE SCHEMA Emp1
GO
CREATE SCHEMA Emp2
GO
CREATE SCHEMA Emp3
GO
- Creates the tables
CREATE TABLE Emp1.Atividade (IDUsuario INT Anything VARCHAR (100))
CREATE TABLE Emp2.Atividade (IDUsuario INT Anything VARCHAR (100))
CREATE TABLE Emp3.Atividade (IDUsuario INT Anything VARCHAR (100))
CREATE TABLE dbo.Usuario (INT IDUsuario, UserName VARCHAR (20))
- Insert the data
Dbo.Usuario INSERT INTO VALUES (1, 'User 1')
Dbo.Usuario INSERT INTO VALUES (2, 'User 2')
Dbo.Usuario INSERT INTO VALUES (4, 'User 4')
Emp1.Atividade INSERT INTO VALUES (1, 'user activities Company 1')
Emp2.Atividade INSERT INTO VALUES (2, 'user activities Company 2')
Emp3.Atividade INSERT INTO VALUES (4, 'User Company Activities 3')
- Create a Procedure to return the records to the Program
ALTER PROCEDURE [dbo]. [Test]
AS
BEGIN
- SET NOCOUNT ON added to Prevent extra result sets from
- Interfering with SELECT statements.
SET NOCOUNT ON;
- Build a string concatenation of commands
DECLARE @ Table VARCHAR (MAX)
SET @ Table = (
SELECT 'SELECT * FROM' + SCHEMA_NAME (SCHEMA_ID) +
. 'UNION ALL Activity' The Name FROM sys.tables
WHERE name = 'Activity'
FOR XML RAW ('Table'), ROOT ('tables'))
SET @ Table =
REPLACE (
REPLACE (
REPLACE (
REPLACE (@ Table, '','')
, 'UNION ALL ','') Tables
, '','')
DECLARE @ cmd VARCHAR (MAX)
SET @ cmd = '; Activities The WITH (' + @ Tables + ')' + CHAR (10) + CHAR (10)
SET @ cmd = @ cmd + 'SELECT * FROM User INNER JOIN The S ON S. The Activities The IDUsuario IDUsuario = A.'
PRINT @ cmd
EXEC (@ cmd)
END