Error: Unknown Return Type, The return types for the following stored procedures could not be detected….. Error: The return types for the following stored procedures could not be detected. Set the return type for each stored procedure in the Properties window. Situations This error usually comes when you are trying to add your Stored Procedure into DBML (LINQ) file. Cause This problem occurs whenever the designer cannot figure out the return type of the SP. This usually happens when the stored procedure has multiple results or uses a temp table. The SQL Server feature that attempts to derive the Meta data of the function reports no columns for the result shape, so the designer defaults to thinking it only has the single int return value. Solutions · The one way to get these types of stored procedures to work is to edit DBML by hand or write your own method signature for the procedure in a partial class To Handle multiple record set return from stored procedure by LINQ see the link here.
· The second way is to avoid using #temp Table in your stored procedure, instead of you can use Table type variable like below (@TempTable) Ex: DECLARE @TempTable TABLE ( AttributeID INT, Value NVARCHAR(200) ) INSERT INTO @TempTable Select * from Attribute OR --Execute SP and insert results into @TempTable INSERT INTO @TempTable Exec GetAttribute @Id You can do all operation which you was doing with #Temp table like Join, Insert, Select etc. |
Wednesday, August 25, 2010
Error: Unknown Return Type, The return types for the following stored procedures could not be detected….(LINQ).
Thursday, August 19, 2010
Concatenating Row Values into one column in Oracle, SQL Server Database and LINQ
Concatenating Row Values into one column in Oracle, SQL Server Database and LINQ Concatenating row values is very common requirements in different databases, to write this kind of query is very time consuming, I tried to combine Oracle,SQL Server database and LINQ simple solution into below example. Maybe this is not matched with your requirement but you will get an idea to proceed. Requirement Let say you have one Employee table having FK reference with department table like below Employee table
How to write a select query so that it returns two columns DEPT_ID and Comma Separated employees under that department. Example
Solution (ORACLE DATABASE) To achieve above requirement there are different solution available in oracle different versions Oracle 10g: Use COLLECT function like SELECT DEPT_ID, CAST (COLLECT (TO_CHAR(EMP_ID)) AS SYS.ODCIVARCHAR2LIST) AS EMPLOYEES FROM EMPLOYEE GROUP BY DEPT_ID Note: If SYS.ODCIVARCHAR2LIST does not work then you have to create one table object, see below example CREATE OR REPLACE TYPE Collect_Type AS TABLE OF VARCHAR2 (4000); SELECT DEPT_ID, CAST (COLLECT (TO_CHAR(EMP_ID)) AS Collect_Type) AS EMPLOYEES FROM EMPLOYEE GROUP BY DEPT_ID Oracle 11g: Use LISTAGG function like SELECT DEPT_ID, LISTAGG (TO_CHAR(EMP_ID), ', ') WITHIN GROUP (ORDER BY DEPT_ID)as "EMPLOYEES" FROM employees; Ref: http://download.oracle.com/docs/cd/E11882_01/server.112/e10592/functions087.htm Result:
Solution (SQL SERVER DATABASE) SQL Server: SQL server database also you can achieve same result in multiple ways, like XML PATH or Custom Function etc. Using XML PATH Select Main.Dept_ID, Left(Main.Employee,Len(Main.Employee)-1) As "Employees" From(Select distinct ST2.Dept_ID, (Select cast(ST1.Emp_ID as varchar(50)) + ',' AS [text()] From dbo.Employee ST1 Where ST1.Dept_ID= ST2.Dept_ID ORDER BY ST1.Dept_ID For XML PATH ('')) [Employee] From dbo.Employee ST2) [Main] Using Custom Function CREATE FUNCTION ConcatEmp(@DeptId varchar(50)) RETURNS VARCHAR(1000) AS BEGIN DECLARE @temp VARCHAR(1000) SELECT @temp = COALESCE(@temp + ', ', '') + cast(Emp_ID as varchar(50)) FROM [Employee] WHERE Dept_ID = @DeptId return @temp END After creating above function you can simply execute in select statement like below SELECT Dept_ID , dbo.ConcatEmp(Dept_ID) AS Employees FROM Employee GROUP BY Dept_ID Result:
Solution (LINQ QUERY) LINQ is one of the .NET (Framework 3.0 and above) features to write SQL query in code, if you are trying to achieve same result using LINQ, you can get idea from following example Here I have created one small console application and write following code to get same results as SQL Server and Oracle Database using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { public class Test { static void Main() { var Employee = new[] { new { Dept="101", EmpId="1" }, new { Dept="101", EmpId="2" }, new { Dept="102", EmpId="3" }, new { Dept="102", EmpId="4" }, new { Dept="102", EmpId="5" }, }; var groupedUsers = Employee.GroupBy(Dept => Dept.Dept); foreach (var group in groupedUsers) { Console.Write(" {0} :", group.Key); foreach (var entry in group) { Console.Write(" {0}", entry.EmpId); } Console.WriteLine(); } Console.Read(); } } } Result: ---------- 101: 1,2 102: 3,4,5 |
Friday, August 13, 2010
How to get the time difference between two date columns? Oracle & SQL Server
How to get the time difference between two date columns? Oracle & SQL Server Oracle allows two date values to be subtracted from each other returning a numeric value indicating the number of days between the two dates (may be a fraction). This example will show how to relate it back to a time value.
Test Data: SQL> CREATE TABLE dates (date1 DATE, date2 DATE); Table created. SQL> INSERT INTO dates VALUES (SYSDATE, SYSDATE-1); 1 row created. SQL> INSERT INTO dates VALUES (SYSDATE, SYSDATE-1/24); 1 row created. SQL> INSERT INTO dates VALUES (SYSDATE, SYSDATE-1/60/24); 1 row created. SQL> SELECT (date1 - date2) FROM dates; DATE1-DATE2 ----------------------- 1 .041666667 .000694444 Solution 1 SQL> SELECT floor(((date1-date2)*24*60*60)/3600) 2 ' HOURS ' 3 floor((((date1-date2)*24*60*60) - 4 floor(((date1-date2)*24*60*60)/3600)*3600)/60) 5 ' MINUTES ' 6 round((((date1-date2)*24*60*60) - 7 floor(((date1-date2)*24*60*60)/3600)*3600 - 8 (floor((((date1-date2)*24*60*60) - 9 floor(((date1-date2)*24*60*60)/3600)*3600)/60)*60) )) 10 ' SECS ' time_difference 11 FROM dates; TIME_DIFFERENCE -------------------------------------------------------------------------------- 24 HOURS 0 MINUTES 0 SECS 1 HOURS 0 MINUTES 0 SECS 0 HOURS 1 MINUTES 0 SECS Solution 2 If you don't want to go through the floor and ceiling maths, try this method: SQL> SELECT to_number( to_char(to_date('1','J') + 2 (date1 - date2), 'J') - 1) days, 3 to_char(to_date('00:00:00','HH24:MI:SS') + 4 (date1 - date2), 'HH24:MI:SS') time 5 FROM dates;
---------- -------- 1 00:00:00 0 01:00:00 0 00:01:00 Solution 3 Here is a simpler method: SQL> SELECT trunc(date1-date2) days, 2 to_char(trunc(sysdate) + (date1 - date2), 'HH24 "Hours" MI "Minutes" SS "Seconds"') time 3 FROM dates; DAYS TIME ---------- ------------------------------ 1 00 Hours 00 Minutes 00 Seconds 0 01 Hours 00 Minutes 00 Seconds 0 00 Hours 01 Minutes 00 Seconds How to add a day/hour/minute/second to a date value? Oracle The SYSDATE pseudo-column shows the current system date and time. Adding 1 to SYSDATE will advance the date by 1 day. Use fractions to add hours, minutes or seconds to the date. Look at these examples: SQL> select sysdate, sysdate+1/24, sysdate +1/1440, sysdate + 1/86400 from dual;
SYSDATE SYSDATE+1/24 SYSDATE+1/1440 SYSDATE+1/86400 -------------------- -------------------- -------------------- -------------------- 03-Jul-2002 08:32:12 03-Jul-2002 09:32:12 03-Jul-2002 08:33:12 03-Jul-2002 08:32:13 SQL server:
DATEDIFF: This Function has been used to get the difference between two dates Reference: Oracle: http://www.orafaq.com/faq/how_does_one_get_the_time_difference_between_two_date_columns SQL Server : http://www.java2s.com/Code/SQLServer/Date-Timezone/DATEDIFFreturnthedifferencebetweentwodates.htm |