Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Query the Datatypes of the SP-Resultset?

Author  Topic 

Eddy
Starting Member

4 Posts

Posted - 2004-09-01 : 10:00:28
Hi

I need to know the datatypes of the resultset of a Stored Procedure in the Microsoft SQL Server.

E.g. The SP "CustOrderHist" of the database "Northwind":

Call:
CustOrderHist 'CACTU'

Return:
Productname = nvarchar(40)
Total = smallint(2)

Does anybody know how to do this?

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-09-01 : 10:08:32
well i think u need INFORMATION_SCHEMA.Columns for that...

SELECT column_name, data_type
FROM INFORMATION_SCHEMA.Columns
WHERE table_name in ('TableName1', 'TableName2', 'TableName3')

Go with the flow & have fun! Else fight the flow :)
Go to Top of Page

kselvia
Aged Yak Warrior

526 Posts

Posted - 2004-09-01 : 13:22:30
Spirit1, INFORMATION_SCHEMA does not contain details on SP result sets.

--Get UDT for sp results
DECLARE @cursor integer

--Create server side cursor to open procedure
EXEC sp_cursoropen @cursor output, N'exec CustOrderHist @CustomerID=1', 4, 2

--Name cursor
EXEC sp_cursoroption @cursor, 2, 'myCursor'

--Cursor columns are stored in master..syscursorcolumns for server cursors. type_name() returns UDT name
SELECT column_name, type_name(data_type_sql) TypeName, column_size Size
FROM master.dbo.syscursorcolumns
WHERE cursor_handle = @cursor

--Close cursor. Retrieve no rows.
exec sp_cursorclose @cursor

column_name TypeName Size
------------- -------- -----------
ProductName nvarchar 80
Total int 4


--Ken
I want to die in my sleep like my grandfather, not screaming in terror like his passengers.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-09-01 : 13:34:08
what i meant was to use that in the sproc.
if he knows what he's returning, he probably knows from where...
that's the way i was looking at it...

but yeah i see what he meant now... my bad...

Go with the flow & have fun! Else fight the flow :)
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-09-01 : 14:45:50
Nope, I have found no way to do this. And I have tried ... extensively ...

If you have a client in c#/vb etc..
The client can issue:
SET FMTONLY ON; EXEC sproc @prm1 = NULL, @prm2 = NULL
This will return formatting info to the datadrivers.
If you put this in a DataTable for example, You can query that for the MetaData [:-)]

If anyone knows how do do this in SQL - PLEASE let me know [;-)]


rockmoose
/* Chaos is the nature of things...Order is a lesser state of chaos */
Go to Top of Page

kselvia
Aged Yak Warrior

526 Posts

Posted - 2004-09-01 : 16:00:00
You can get the resultset into a temp table and query INFORMATION_SCHEMA for standard SQL datatypes;

SELECT * into #tmp FROM
OPENROWSET('SQLOLEDB', 'Trusted_Connection=yes;Data Source=localhost', 'EXEC Northwind.dbo.CustOrderHist ALFKI')

SELECT column_name, data_type, character_maximum_length
FROM tempdb.INFORMATION_SCHEMA.Columns
WHERE table_name like '#tmp%'

column_name data_type character_maximum_length
--------------- ----------------- ------------------------
ProductName nvarchar 40
Total int NULL

The suggestion I posted earlier was would also retrieve user-defined types if applicable. Previously discussed in this thread: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=37875

--Ken
I want to die in my sleep like my grandfather, not screaming in terror like his passengers.
Go to Top of Page

Eddy
Starting Member

4 Posts

Posted - 2004-09-01 : 17:31:02
kselvia. This is very close! Thanks a lot. But it means that I have to know about the parameters the SP expects and it means that I have to call the SP. I can't call the SP, because I don't know what the SP does. It maybe changes the data on the database.


What I want to do is the following:

1. C#-Client queries "any" SP on "any" database and finds out what the returning datatypes are.

2. C#-Client creates a class on the fly, which wraps the SP. The class member uses typed arguments (I know how to do that) and returns an array of typed structures or a typed dataset (this is what I need to do).

Any other idea?

Eddy


quote:
Originally posted by kselvia

Spirit1, INFORMATION_SCHEMA does not contain details on SP result sets.

--Get UDT for sp results
DECLARE @cursor integer

--Create server side cursor to open procedure
EXEC sp_cursoropen @cursor output, N'exec CustOrderHist @CustomerID=1', 4, 2

--Name cursor
EXEC sp_cursoroption @cursor, 2, 'myCursor'

--Cursor columns are stored in master..syscursorcolumns for server cursors. type_name() returns UDT name
SELECT column_name, type_name(data_type_sql) TypeName, column_size Size
FROM master.dbo.syscursorcolumns
WHERE cursor_handle = @cursor

--Close cursor. Retrieve no rows.
exec sp_cursorclose @cursor

column_name TypeName Size
------------- -------- -----------
ProductName nvarchar 80
Total int 4


--Ken
I want to die in my sleep like my grandfather, not screaming in terror like his passengers.

Go to Top of Page

Eddy
Starting Member

4 Posts

Posted - 2004-09-01 : 17:38:16
rockmoose. Could you please explain that? What does @prm1 and @prm2 mean? Does that work with any SP?

My client is in fact in C#, but I can't see the way you are pointing at. Could you please add some code to show what you mean?

Eddy


quote:
Originally posted by rockmoose

Nope, I have found no way to do this. And I have tried ... extensively ...

If you have a client in c#/vb etc..
The client can issue:
SET FMTONLY ON; EXEC sproc @prm1 = NULL, @prm2 = NULL
This will return formatting info to the datadrivers.
If you put this in a DataTable for example, You can query that for the MetaData [:-)]

If anyone knows how do do this in SQL - PLEASE let me know [;-)]


rockmoose
/* Chaos is the nature of things...Order is a lesser state of chaos */

Go to Top of Page

kselvia
Aged Yak Warrior

526 Posts

Posted - 2004-09-01 : 17:43:11
Afraid it's not possible. Without executing the procedure, the result set is undefined. It could be 1 or many result sets. You can do as Rockmoose suggested and SET FMTONLY ON; and pass it placeholder paramaters with the correct datatypes.
See this post for exmaples of how to determine sp parameter types http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=36396

--Ken
I want to die in my sleep like my grandfather, not screaming in terror like his passengers.
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-09-02 : 04:06:53
Use INFORMATION_SCHEMA.PARAMETERS To find out the name and orders of the parameters.. ( or smthng )
C#
SqlConnection cn = new SqlConnection( ... );
SqlCommand cmd = cn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SET FMTONLY ON;EXEC Northwind.dbo.CustOrderHist @CustomerId = NULL"; // You just need placeholders, don't need to know datatype
of param
SqlDataReader dr = cmd.ExecuteReader();
dr.Open();
DataTable dt = dr.GetSchemaTable();
dr.Close();
foreach( DataColumn dc in dt.Columns ){ /* do the interesting stuff of retrieving column metadata */ }


P.S
Please post back code if You get something useful :-)





rockmoose
/* Chaos is the nature of things...Order is a lesser state of chaos */
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2004-09-02 : 04:11:16
quote:
Originally posted by kselvia

Afraid it's not possible. Without executing the procedure, the result set is undefined. It could be 1 or many result sets. You can do as Rockmoose suggested and SET FMTONLY ON; and pass it placeholder paramaters with the correct datatypes.
See this post for exmaples of how to determine sp parameter types http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=36396



When using SET FMTONLY ON, you don't need to know the datatype of the parameters, just how many there are
Effectively:

SET FMTONLY ON;EXEC procW3Params NULL, NULL, NULL




rockmoose
/* Chaos is the nature of things...Order is a lesser state of chaos */
Go to Top of Page

Eddy
Starting Member

4 Posts

Posted - 2004-09-02 : 10:51:47
This is the solution. It finally works.

Thank's a lot to everybody!!! I really appreciate it.

Eddy

quote:
Originally posted by rockmoose

quote:
Originally posted by kselvia

Afraid it's not possible. Without executing the procedure, the result set is undefined. It could be 1 or many result sets. You can do as Rockmoose suggested and SET FMTONLY ON; and pass it placeholder paramaters with the correct datatypes.
See this post for exmaples of how to determine sp parameter types http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=36396



When using SET FMTONLY ON, you don't need to know the datatype of the parameters, just how many there are
Effectively:

SET FMTONLY ON;EXEC procW3Params NULL, NULL, NULL




rockmoose
/* Chaos is the nature of things...Order is a lesser state of chaos */

Go to Top of Page
   

- Advertisement -