select data using stored procedure - sql

I use the stored procedure to get multiple records which that condition satisfies
CREATE PROCEDURE [dbo].[Sp_GetAttendanceBwDates]
#datefrom datetime,
#dateto datetime,
#empid int
ASBEGIN`
select AM.employee_Id,CONVERT(varchar(10),AM.date,111) from
tblAttendanceMaster AM where AM.employee_Id=#empid and
CONVERT(varchar(10),AM.date,111)<=CONVERT(varchar(10),#datefrom,111)
and CONVERT(varchar(10),AM.date,111)=CONVERT(varchar(10),#dateto,111)
END
in code behind,when execute the below code I getting the error.I didn't understand it
var objattendance = context.Sp_GetAttendanceBwDates(datefrom,dateto,emp);
error message
occurred in System.Data.Entity.dll but was not handled in user code.
Additional information: The data reader is incompatible with the specified 'FlairModel.Sp_GetAttendanceBwDates_Result'. A member of the type, 'record_Id', does not have a corresponding column in the data reader with the same name.

I guess theer is problem in the column i.e. property get created when you imported SP in you entiry frameowrk ,
So there might be problem with your select statement here which is not returning proper name matching with that generated complex type or generated type
for example this query with matching columns like
select AM.employee_Id as record_Id''need to be matching property name,
CONVERT(varchar(10),AM.date,111) as date''need to be matching property name
from
tblAttendanceMaster AM where AM.employee_Id=#empid and
CONVERT(varchar(10),AM.date,111)<=CONVERT(varchar(10),#datefrom,111)
and CONVERT(varchar(10),AM.date,111)=CONVERT(varchar(10),#dateto,111)

Related

How to find which field is giving data type error in QUERY

I have the query report for user but have some error message display:
Error converting data type nvarchar to bigint
only one message.
How to find which field is giving data type error in query? Anyone can give advice? Many thanks!
As you have not provided data, it looks like you have NVARCHAR datatype, which is not implicitly convertable to Bigint
SELECT CAST(N'287888' AS BIGINT) -- Success
SELECT CAST(N'ABC123' AS BIGINT) -- Failure
See which column is failing and accordingly fix it.
You can either only load the proper values:
SELECT * FROM Table WHERE TRY_CAST(ErrorField AS BIGINT) IS NOT NULL
Or, You can load them as NULL(provided the target column allows NULL)
SELECT TRY_CAST(ErrorField AS BIGINT) FROM Table

Conversion failed : converting from a character string to uniqueidentifier

I have the following function which returns the following result set when this string is called.
SELECT item
FROM dbo.DelimitedSplit8K('985B773F-5E36-47D4-9E84-E0CE35B34337,32237666-86F3-41FD-BCDE-794571CDAEA2',',')
Result set:
item
------------------------------------
985B773F-5E36-47D4-9E84-E0CE35B34337
32237666-86F3-41FD-BCDE-794571CDAEA2
Now the purpose of this function is to get the two or more IDs cause I will be passing multiple IDs from my C# program to one variable in my stored procedure.
Problem
The problem seems to be simple enough but I am not sure as to why it's occurring.
CREATE PROC [dbo].[usp_printMulitTest]
#multiApplicationId_FK uniqueidentifier = '',
#pDelimiter CHAR(1) = NULL
AS
;WITH image_CTE(imgBinary, imgCode, appID) AS
(
SELECT
[image], imageCode_FK, app
FROM
[dbo].Images
WHERE
CAST('985B773F-5E36-47D4-9E84-E0CE35B34337,32237666-86F3-41FD-BCDE-794571CDAEA2' AS UNIQUEIDENTIFIER)
IN ((SELECT item
FROM dbo.DelimitedSplit8K(CAST('985B773F-5E36-47D4-9E84-E0CE35B34337,32237666-86F3-41FD-BCDE-794571CDAEA2' AS UNIQUEIDENTIFIER),',')))
)
SELECT *
FROM image_CTE
This stored procedure works fine when I hard code the variables.
However, when I convert it to this
WHERE CAST(app AS UNIQUEIDENTIFIER)
IN((SELECT item FROM dbo.DelimitedSplit8K(CAST(#multiApplicationId_FK AS UNIQUEIDENTIFIER),',' )))
to get the results for the app IDs that I pass in, I get an error
Conversion failed when converting from a character string to uniqueidentifier
While looking for solutions two that were pointed out is the incorrect formation of the unique identifier and not using cast, however I checked the numbers and used a cast/convert and there has been no change.
Grateful for assistance in this.

How to pass multiply values parameter to a procedure

I want to pass multiply values parameter to my procedure and use it as a filter. My parameter is called #Month and it's datatype is NVARCHAR(MAX) in the procedure.
I have used filter as
WHERE (cal.CalendarYear = #Year) AND (cal.MonthId IN (#Month))
and also tried STRING_SPLIT function.
However, when I run my report, it return an error
Conversion failed when converting the nvarchar value '1,2,3,4,5,6,7,8,9,10,11,12' to data type int.
you cannot give a varchar string with comma separated values, and expect the query to process it as a 'in' list.
The way that would work is when you would concatenate your sql statement in the stored procedure, and then execute the sql string with sp_executesql.
Using string_split is one way of doing it, but you have to be aware that it gives you a table with varchars, not integers.
For completeness I should mention that the most 'robust' way of doing this is passing a table type variable to your stored procedure, as described here: How to pass table value parameters to stored procedure from .net code.
With the split_string, you could try something like this:
select
-- your fields
from
manytables mt
join string_split(#Month,',') months on cast(months.value as int) = cal.monthId
where
cal.Calenderyear = #year

Passing user defined TABLE type to stored Oracle function

I have an oracle function defined as:
FUNCTION SELECTINBOX (FA_FROMUSERLIKE IN PKGSMSTYPES.MAXVARCHAR2_T DEFAULT NULL ,
FA_INBOXOWNER IN PKGSMSTYPES.MAXVARCHAR2_T,
FA_A_URGENCY IN G_INTARRAY_TBL DEFAULT NULL ,
FA_PAGENO IN NUMBER DEFAULT 1
) RETURN G_SMSNOTES_TBL;
where G_INTARRAY_TBL is defined as,
create or replace
TYPE G_INTARRAY_TBL AS TABLE OF NUMBER;
I am building the query using eclipselink. The query works fine if I hardcode G_INTARRAY_TBL as null in the query string but if I try to pass a List of BigDecimals to it, I get an error,
Internal Exception: java.sql.SQLException: Invalid column type
Error Code: 17004
Include your code for your query.
You need to use a PLSQLStoredFunctionCall (#NamedPLSQLStoredFunctionQuery) for this. You also need to mirror the PLSQL TABLE type with a VARRAY type.
See,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/PLSQLStoredFunction

Problem In get dates between 2 date (vb.net | OLE)

I have an data base (.mdb) and it has a column with dates (dd/mm/yy) , some one give me a code to get all the dates in database between 2 dates , the code was :
Select * from table where date between 'StartDate' and 'EndDate'
but after I use the code , an error occurs told me that the types of data is not the same
System.Data.OleDb.OleDbException was
unhandled ErrorCode=-2147217913
Message="عدم تطابق نوع البيانات في
تعبير المعايير." Source="Microsoft
JET Database Engine"
although I convert the data type in the column of dates in database to (Date \ time) , and use OLE object to connect to data base
what is wrong , and what I have to do ?
You usually need to surround date/time types with # when working with Access, like so
#22/01/2009#
Instead of using dynamic SQL, instead use the Access Database Engine's CREATE PROCEDURE SQL DDL syntax to create a persisted object whose parameters have parameters strongly-typed as DATETIME with the NULL value as default. Handle the NULL value to use the DATETIME column's value instead e.g.
CREATE PROCEDURE GetStuff
(
arg_start_date DATETIME = NULL,
arg_end_date DATETIME = NULL
)
AS
SELECT lastvstart
FROM tb
WHERE lastvstart
BETWEEN IIF(arg_start_date IS NULL, lastvstart, arg_start_date)
AND IIF(arg_end_date IS NULL, lastvstart, arg_end_date);