Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF4 - The selected stored procedure returns no columns

I have query in a stored procedure that calls some linked servers with some dynamic SQL. I understand that EF doesn't like that, so I specifically listed all the columns that would be returned. Yet, it still doesn't like that. What am I doing wrong here? I just want EF to be able to detect the columns returned from the stored procedure so I can create the classes I need.

Please see the following code that makes up the last lines of my stored procedure:

SELECT     #TempMain.ID,     #TempMain.Class_Data,     #TempMain.Web_Store_Class1,     #TempMain.Web_Store_Class2,     #TempMain.Web_Store_Status,     #TempMain.Cur_1pc_Cat51_Price,     #TempMain.Cur_1pc_Cat52_Price,     #TempMain.Cur_1pc_Cat61_Price,     #TempMain.Cur_1pc_Cat62_Price,     #TempMain.Cur_1pc_Cat63_Price,     #TempMain.Flat_Length,     #TempMain.Flat_Width,     #TempMain.Item_Height,     #TempMain.Item_Weight,     #TempMain.Um,     #TempMain.Lead_Time_Code,     #TempMain.Wp_Image_Nme,     #TempMain.Wp_Mod_Dte,     #TempMain.Catalog_Price_Chg_Dt,     #TempMain.Description,     #TempMain.Supersede_Ctl,     #TempMain.Supersede_Pn,     TempDesc.Cust_Desc,     TempMfgr.Mfgr_Item_Nbr,     TempMfgr.Mfgr_Name,     TempMfgr.Vendor_ID FROM     #TempMain         LEFT JOIN TempDesc ON #TempMain.ID = TempDesc.ID         LEFT JOIN TempMfgr ON #TempMain.ID = TempMfgr.ID 
like image 951
cjbarth Avatar asked Aug 20 '11 00:08

cjbarth


People also ask

What is set Fmtonly off?

When FMTONLY is ON , a rowset is returned with the column names, but without any data rows. SET FMTONLY ON has no effect when the Transact-SQL batch is parsed. The effect occurs during execution run time. The default value is OFF .

Why stored procedure Cannot be called in function?

You cannot execute a stored procedure inside a function, because a function is not allowed to modify database state, and stored procedures are allowed to modify database state.

What is returned by the ExecuteSqlRaw () when used to execute a stored procedure?

When calling any data modification stored procedure use the ExecuteSqlRaw() method on the Database property of your DbContext object. This method returns an integer value of the number of rows affected by the statement in that stored procedure.


1 Answers

EF doesn't support importing stored procedures which build result set from:

  • Dynamic queries
  • Temporary tables

The reason is that to import the procedure EF must execute it. Such operation can be dangerous because it can trigger some changes in the database. Because of that EF uses special SQL command before it executes the stored procedure:

SET FMTONLY ON 

By executing this command stored procedure will return only "metadata" about columns in its result set and it will not execute its logic. But because the logic wasn't executed there is no temporary table (or built dynamic query) so metadata contains nothing.

You have two choices (except the one which requires re-writing your stored procedure to not use these features):

  • Define the returned complex type manually (I guess it should work)
  • Use a hack and just for adding the stored procedure put at its beginning SET FMTONLY OFF. This will allow rest of your SP's code to execute in normal way. Just make sure that your SP doesn't modify any data because these modifications will be executed during import! After successful import remove that hack.
like image 92
Ladislav Mrnka Avatar answered Oct 02 '22 12:10

Ladislav Mrnka