views:

180

answers:

3

I have some complex stored procedures pulling data from other databases using linked servers. This data is put into temp tables which are joined in a select query for output. The procedures work fine but in visual studio if I try to add the stored procedure to a dataset using the designer I get the error invalid object name #tmp or whatever the first temp table is called. It is unable to retrieve the database schema. It's the same for using and sqldatasource in ASP.NET.

The procedure is still usable but I have to manually add all the columns it should output to the datatable. This is going to be a pain to do manually and I assume it is to do with the way visual studio gathers the output fields from the stored procedure, it doesn't seem to run it in the normal way. Is there a way to correct this as I have quite a lot of these to do and don't want to have to add all the columns manually, which is time consuming and error prone.

A: 

The reason it cannot read metadata is because you have a #Temp table and it cannot find the temp table. You can try to get around it by defining the metadata using a TOP 0 query

SELECT TOP 0
    CONVERT (NULL, VarChar (30)) AS Column1,
    CONVERT (NULL, INTEGER) AS Column2,
    CONVERT (NULL, DECIMAL (9, 2)) AS Column3,
    CONVERT (NULL, VarChar (55)) AS Column4

UNION

SELECT *
FROM #MyTempTable
Raj More
Semes like that will take nearly as long, I don't see why visual studio can't obtain the meta data by running the query as normal since for instance Crystal report can do this without hassle.
PeteT
A: 

The reason you cannot obtain meta data here is because it could depend on data. For example.

If Exists(Select 1 From Table Where Column1 = 7)
  Select Col1, Col2 From Table2
Else
  Select Col3, Col4, Col5 From Table2

I would never write a stored procedure that has a different number of columns depending on data, BUT it is possible to do. Just for a moment, suppose you had a stored procedure like this. What column list should be returned?

G Mastros
Yeah I understand that and I would also never write one where it would return potentially a different number of columns. But why doesn't it just retrieve the columns for the variables I specify.
PeteT
Because it still may not be deterministic. Also consider that a stored procedure could return no recordset, one recordset, or even multiple recordsets.You can also use random number functions so that each call returns different columns.
G Mastros
Yeah but it's only being used for the designer, the developer knows what columns need to exist in the end it's just quicker for it to put them in for you based on the values you give it for the stored proc. An example is crystal report it takes the values you give it and pulls out the last datatable a stored procedure gives you. It doesn't matter the fact that they may not exist at runtime as this is always the case anyway as you could easily change any sql query.
PeteT
A: 

After googling for a while I found a bizarre solution putting:

IF 1=0 BEGIN
SET FMTONLY OFF
END

At the start of your stored procedure allows the designer to correctly get the schema information. This code obviously never runs but it solves the issue. FMTONLY is related to only returning meta data about a stored procedure. I am using sql server 2005.

PeteT