Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my typed dataset not like temporary tables?

I am attempting add a tableadapter to a stored procedure in my SQL Server 2005 Express. The stored procedure, however, uses a temporary table called #temp. When creating the table adapter, Visual Studio complains "Unknown Object '#temp'" and says that the stored procedure returns 0 columns. This is problematic because I use that stored procedure with a crystal report, and need those columns.

How can I fix this?

like image 669
Malfist Avatar asked May 28 '09 16:05

Malfist


People also ask

What is the advantage of using a temporary table instead of a table?

Temporary tables behave just like normal ones; you can sort, filter and join them as if they were permanent tables. Because SQL Server has less logging and locking overheads for temporary tables (after all, you're the only person who can see or use the temporary table you've created), they execute more quickly.

What is the alternative for temporary table in SQL Server?

A valuable alternatives for the SQL temp table and table variable are SCHEMA_ONLY Memory-Optimized tables and the Memory-optimized Table Variable, where the data will be completely stored in the memory without the need to touch the TempDB database, providing the best data access performance.

Does using temp tables improve performance?

I did try to apply an index to the temp table but this will only reduce performance as: The number of indexes on a table is the most dominant factor for insert performance. The more indexes a table has, the slower the execution becomes.

How can you determine the datatype of a column in a temp table?

Yes, the data types of the temp table will be the data types of the columns you are selecting and inserting into it. So just look at the select statement and determine each data type based on the column you select.


2 Answers

Bizarre. According to this you add

IF 1=0 BEGIN
    SET FMTONLY OFF
END

to the SP right after the AS part of the SP and it works. Visual Studio now has no problem with it. I have no idea why this works like this, or why it would work, but it does.

like image 58
Malfist Avatar answered Sep 29 '22 18:09

Malfist


This may be an old thread and the answer is found, but when someone gets into your stored procedure after and see this code, he really does not understand. There is another way to do this properly and it is to simply declare the table as a variable like this :

DECLARE @temp TABLE  
(
    SomeText1 nvarchar(255),
    SomeText2 nvarchar(255)
)

Also, don't forget to remove the DROP TABLE at the end.

PS : If you really need to use the temporary table because you need to create it, then you have to write the code given in the previous answer. Hope this helps.

like image 40
Crushermike Avatar answered Sep 29 '22 16:09

Crushermike