I have the following query and its timing out in .net but executing fine in sql server does .net not like temp tables or something ??? I run it in .net and i get a timeout error I dont understand what is going on at all.
SET DATEFORMAT dmy
declare #AbsenceReasonRestrictions varchar(500)
set #AbsenceReasonRestrictions ='1'
create table #absence
(
record_id INT,
emp_no int,
staff_no varchar(max),
emp_name varchar(max),
details text,
leave_reason int,
leave_reason_desc varchar(30),
current_status int,
date_added datetime,
dept int,
dept_desc varchar(100),
location int,
location_desc varchar(100),
division int,
division_desc varchar(100),
emptype int,
emptype_desc varchar(100),
contype int,
contype_desc varchar(100),
conclass int,
conclass_desc varchar(100),
line_manager int,
line_manager_name varchar(510)
)
INSERT INTO #absence (record_id,
emp_no,
staff_no,
emp_name,
details,
leave_reason,
leave_reason_desc,
current_status,
date_added,
dept,
dept_desc,
location,
location_desc,
division,
division_desc,
emptype,
emptype_desc,
contype,
contype_desc,
conclass,
conclass_desc,
line_manager,
line_manager_name)
select ua.record_id,
ua.emp_no,
e.staff_no,
rtrim(e.surname)+', '+rtrim(e.forename1),
ua.details,
ua.absence_reason,
ar.desc_,
ua.current_status,
ua.date_added,
c.dept,
rtrim(dept.desc_),
c.location,
rtrim(loc.desc_),
c.division,
rtrim(div.desc_),
c.emptype,
rtrim(emptype.desc_),
c.type,
rtrim(contype.desc_),
c.classification,
rtrim(conclass.desc_),
ua.manager_user_id,
(select rtrim(e.surname) + ', ' + rtrim(e.forename1) as emp_name from employee e
inner join userlist_mss um on e.emp_no = um.pams_id
where um.record_id = ua.manager_user_id)
from ess_absence_requests ua
inner join employee e on e.emp_no=ua.emp_no
inner join absreas ar on ar.code=ua.absence_reason
inner join contract c on ua.emp_no = c.emp_no
join dept on c.dept=dept.code
join location loc on c.location=loc.code
join division div on c.division=div.code
join emptype on c.emptype=emptype.code
join contype on c.type=contype.code
join conclass on c.classification=conclass.code
where e.emp_no like '%'
AND c.main_contract=1
AND ua.current_status in (1,2,3,4)
AND (dbo.fn_XmlElementDateValue(ua.details, 'start_date') >='1/10/2013')
AND (dbo.fn_XmlElementDateValue(ua.details, 'end_date') <='31/10/2013')
AND e.active_leaver like 'ACTIVE'
AND e.emp_no like '%'
and c.dept like '%'
and c.location like '%'
and c.division like '%'
and c.emptype like '%'
and c.classification like '%'
and c.type like '%'
and ua.emp_no in (select employee_id from userlist_mss_employee_access_rights where manager_id=2)
order by (dbo.fn_XmlElementDateValue(ua.details, 'start_date'))
if #AbsenceReasonRestrictions!=''
begin
set #AbsenceReasonRestrictions=','+#AbsenceReasonRestrictions+','
delete #absence where charindex(','+cast(leave_reason as varchar(10))+',', #AbsenceReasonRestrictions) = 0
end
select record_id,
emp_no,
staff_no,
emp_name,
details,
leave_reason,
leave_reason_desc,
current_status,
date_added,
dept_desc,
location_desc,
division_desc,
emptype_desc,
contype_desc,
conclass_desc,
line_manager,
line_manager_name from #absence
drop table #absence
select * from emp_anal
I think that the problem is that management studio has no timeout limit for your query, but when you run it within .net code you are limited connection timeout. Try to change the timeout directly in you connection string or in connection initialization place.
More info:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectiontimeout(v=vs.110).aspx
Changing SqlConnection timeout
I guess it's because your direct sql call (using management studio?) uses a different query plan than your .net code ... and the plan .net uses seems to be no good ...
to resolve this try updating table statistics (update statistics ) for the tables used by your join and dbcc freeproccache to clear the cached query plans ...
Related
I set-up a connection from R to (Microsoft SQL Server Studio) using the RODBC package. While I am able to run simple SQL queries directly from R, I find that the more complex sql queries containing special characters such as "#" for declaring a table while creating a temp table tend to return an error from R. I have tried to escape this within R itself (by placing it in quotes) , however, this is failing, as SQL could not interpret this escape characters (I guess).
My goal is to perform soundex/fuzzy matching of some client records again the clients in the database (~3M rows). I have tried getting this done directly using the stringdist package in R but the matching process is blowing out my RAM (16GB), hence, the reason why I have resulted to matching the data from within SQL itself. I could have easily done this in SQL, however, I need to set-up this in R so that non-technical individuals can easily run the R script and query, database and perform further work on the resulting dataset.
I have tried the suggestion in this post but did not find it helpful to resolve this issue
Any tips on how to escape SQL special characters like the # symbol would be useful.
I get this error in R:
1" 42000 102 [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'go'."
2 "[RODBC] ERROR: Could not SQLExecDirect '\nSET DATEFORMAT dmy; \ngo\nDECLARE #VerifyClientID TABLE (firstname varchar(100), middlename varchar(100), lastname varchar(100)~
The script:
SET DATEFORMAT dmy;
go
DECLARE #VerifyClientID TABLE (firstname varchar(100), middlename varchar(100), lastname varchar(100), dob date, mobile varchar(100), ID int)
INSERT INTO #VerifyClientID (firstname, lastname, mobile, ID)
VALUES
('JOHN','DOE','0444 444 444',1)
drop table if exists #clientTABLE
select v.ID, p.ABC_NCMID, P.ABC_RowID, P.ABC_FirstName, P.ABC_GuardianName, P.ABC_LastName, P.ABC_SexCode_ID, P.ABC_CellPhone, P.ABC_DOB, ABC_StreetAddress, ABC_City, ABC_Zip, ABC_SSN, ABC_HomePhone
into #clientTABLE
from #VerifyClientID V
inner join dbo.DV_Person P on soundex(V.firstname) = soundex(P.ABC_FirstName)
and soundex(V.lastname) = soundex(P.ABC_LastName)
and (convert(varchar,replace(replace(P.ABC_CELLPHONE,' ',''),0,'')) = convert(varchar,replace(replace(V.mobile,' ',''),0,''))
or convert(varchar,replace(replace(P.ABC_HomePhone,' ',''),0,'')) = convert(varchar,replace(replace(V.mobile,' ',''),0,''))
)
where 1=1
and p.ABC_NCMID in (select Per.PER_CLIENTID from AtlasPublic.View_UODS_Person Per)
and P.ABC_IsPatient = 1
select distinct ID
,firstname
,middlename
,lastname
,dob
,mobile
, (SELECT TOP 1 ABC_NCMID FROM #clientTABLE WHERE id = v.id) as MatchedID
, (SELECT TOP 1 convert(varchar, ABC_DOB, 103) FROM #clientTABLE WHERE id = v.id) as MatchedDOB
, (SELECT TOP 1 case when ABC_SexCode_ID = 8089 then 'Male'
when ABC_SexCode_ID = 8088 then 'Female'
else null end FROM #clientTABLE WHERE id = v.id) as MatchedSEX
, (SELECT TOP 1 ABC_StreetAddress FROM #clientTABLE WHERE id = v.id) as MatchedStreet
, (SELECT TOP 1 ABC_City FROM #clientTABLE WHERE id = v.id) as MatchedCity
, (SELECT TOP 1 ABC_Zip FROM #clientTABLE WHERE id = v.id) as MatchedZip
from #VerifyClientID V
ALTER procedure [dbo].[SPEmployeeInformation]
#searchText nvarchar(100)
as
begin
select EmployeeInformation.LineId, FirstName, MidName, FatherName, MotherName, dob, MeritalStatus, ParmanentAddress,
DepartmentMaster.DepartmentName, DesignationMaster.Designation, EmployeeCategory.EmployeeCategory, Salary.Salary
from EmployeeInformation
inner join DepartmentMaster on EmployeeInformation.DepartmentName=DepartmentMaster.DepartmentLineId
inner join DesignationMaster on EmployeeInformation.Designation=DesignationMaster.DesignationLineId
inner join EmployeeCategory on EmployeeInformation.EmployeeCategory=EmployeeCategory.EmployeeCategoryLineId
inner join Salary on EmployeeInformation.LineId=Salary.EmployeeLineID
where FirstName+MidName+FatherName+MotherName+MeritalStatus+ParmanentAddress+
DepartmentMaster.DepartmentName+DesignationMaster.Designation+EmployeeCategory.EmployeeCategory like '%'+#searchText+'%'
end
I just try to test with your scenario with sample data and its works. That means you doing wrong in query like column name or int datatype value etc.
Without sample data we can't do, just check the concept below.
declare #table table (fname varchar(200), lname varchar(200))
declare #patientName varchar(200) ='john'
insert into #table values ('doe', 'john'),('ritu', 'pawar'),('Vogue', 'jani')
select * from #table where fname+lname like '%'+#patientName+'%'
set #patientName ='ej'
select * from #table where fname+lname like '%'+#patientName+'%'
Output are
First query return below
fname lname
doe john
Second query return below
fname lname
doe john
Vogue jani
Using MS SQL
Create procedure [dbo].[SPEmployeeInformation]
#searchText nvarchar(100)
as
begin
select EmployeeInformation.LineId, FirstName, MidName, FatherName, MotherName, dob, MeritalStatus, ParmanentAddress,
DepartmentMaster.DepartmentName, DesignationMaster.Designation, EmployeeCategory.EmployeeCategory, Salary.Salary
from EmployeeInformation
inner join DepartmentMaster on EmployeeInformation.DepartmentName=DepartmentMaster.DepartmentLineId
inner join DesignationMaster on EmployeeInformation.Designation=DesignationMaster.DesignationLineId
inner join EmployeeCategory on EmployeeInformation.EmployeeCategory=EmployeeCategory.EmployeeCategoryLineId
inner join Salary on EmployeeInformation.LineId=Salary.EmployeeLineID
where FirstName+MidName+FatherName+MotherName+MeritalStatus+ParmanentAddress+ DepartmentMaster.DepartmentName+DesignationMaster.Designation+EmployeeCategory.EmployeeCategory like '%'+#searchText+'%'
end
FOR EXECUTE YOUR SP
exec [Your SP Name] 'Your Pram Value'
---Example---
exec SPEmployeeInformation 'your search text'
Happy Coding. :-)
Sir, below is my SQL query, followed with two images of Query Result & Required Output.
As you can see on Result image that I have got this output from executing the following query.
There is second image of Required Output. I would need to display the output in that format. As First Department name should be at top & below Project details correspond to that department. The same cycle should get repeated for each and every department.
How should I achieve this ?
SQL Code:
DECLARE #ColName varchar(20)=null,
#Query varchar(MAX)=null,
#DepartmentName varchar(50)=null,
#deptt_code varchar(4)=null,
#DistrictId varchar(4)='0001',
#Deptt_Id char(4)=null,
#stYear varchar(4)=null,
#cYear varchar(4)=null,
#yr varchar(9)='2017-2018',
#tno int
BEGIN
set #stYear = SUBSTRING(#yr,0,5);
set #cYear = SUBSTRING(#yr,6,4);
--CREATE DYNAMIC TABLE WITH COLs
DECLARE #DepartmentTable table
(
department_name varchar(50),
department_code varchar(4)
);
DECLARE #ProjectTable table
(
project_name varchar(130),
project_code varchar(15),
department_code varchar(4),
department_name varchar(50),
district_code varchar(4),
district_name varchar(30),
tehsil_code varchar(6),
tehsil_name varchar(30),
service_code varchar(3),
[service_name] varchar(30),
sector_code varchar(3),
sector_name varchar(30),
project_start_year varchar(8),
project_compl_year varchar(8),
financial_year varchar(9)
);
--Insert into Department Table
INSERT INTO #DepartmentTable (department_code, department_name)
select deptt_code,deptt_name+'('+ RTRIM(LTRIM(deptt_short))+')' as dept_name from m_Department
where deptt_code in (select distinct department_code from t_Project_Details where district_id=#DistrictId
and financial_year=#yr);
--Insert into Project Table With Corresponding Department Name & Code
insert into #ProjectTable (
department_code,
project_code,
project_name,
department_name,
district_code,
district_name,
tehsil_code,
tehsil_name,
service_code,
[service_name],
sector_code,
sector_name,
project_start_year,
project_compl_year,
financial_year
)
SELECT pd.department_code,
pd.project_Code,
pd.project_name,
d.deptt_name,
pd.district_id,
di.district_name,
pd.tehsil_id,
t.tehsil_name,
pd.service_code,
s.[service_name],
pd.sector_code,
se.sector_name,
CONVERT(varchar,YEAR(pd.project_initiation_fin_from_yr)) + ' ' + CONVERT(varchar(3),pd.project_initiation_fin_from_yr,100) as project_start_year,
CONVERT(varchar,YEAR(pd.project_initiation_fin_to_yr)) + ' ' + CONVERT(varchar(3),pd.project_initiation_fin_to_yr,100) as project_compl_year,
pd.financial_year
FROM t_Project_Details pd
INNER JOIN m_Department d
ON d.deptt_code=pd.department_code
INNER JOIN m_Service s
ON s.service_code = pd.service_code
INNER JOIN m_Sector se
ON se.sector_code=pd.sector_code
INNER JOIN m_District di
ON di.district_code=pd.district_id
INNER JOIN m_Tehsil t
ON t.tehsil_code = pd.tehsil_id
WHERE
district_id=#DistrictId and
financial_year=#yr;
--select all from Department Table;
select * from #ProjectTable;
END
Query Result Image: https://drive.google.com/open?id=0Bxn7UXgmstmRaS1qX21kbjlwZzg
Required Output Image: https://drive.google.com/open?id=0Bxn7UXgmstmRekJkUWhBcmNCbk0
Your required output is a presentation layer issue; SQL can return your results but cannot make one row contain one thing and following rows contain many different things in a different format.
A SQL resultset will contain the same number of columns in all rows each with the same datatype.
Use something like SSRS to acheive your desire output.
I have a situation like this to a instant search but this query have more NOT IN check
ALTER PROC [dbo].[DPR_SP_GetEmployeeDetailsByKeyword]
#keyword varchar(10),
#hotelId int
AS
BEGIN
SELECT EmployeeDetails.EmployeeId,EmployeeDetails.SageEmployeeId,EmployeeDetails.FirstName,EmployeeDetails.LastName
FROM EmployeeDetails
WHERE (EmployeeDetails.EmployeeId NOT IN ( SELECT EmployeeWorksIn.EmployeeId
FROM EmployeeWorksIn
WHERE EmployeeWorksIn.HotelId=#hotelId
)
AND
EmployeeDetails.EmployeeId LIKE #keyword+'%')
OR
(EmployeeDetails.EmployeeId NOT IN ( SELECT EmployeeWorksIn.EmployeeId
FROM EmployeeWorksIn
WHERE EmployeeWorksIn.HotelId=#hotelId
)
AND
OR
(EmployeeDetails.EmployeeId NOT IN ( SELECT EmployeeWorksIn.EmployeeId
FROM EmployeeWorksIn
WHERE EmployeeWorksIn.HotelId=#hotelId
)
AND
EmployeeDetails.DateOfBirth LIKE #keyword+'%')
How can I optimise this query
Thanks
You cam rewrite this as
alter proc dbo.DPR_SP_GetEmployeeDetailsByKeyword
#keyword varchar(10),
#hotelId int
As
Select
e.EmployeeId,
e.SageEmployeeId,
e.FirstName,
e.LastName
From
EmployeeDetails e
Left Outer Join
EmployeeWorksIn w
on e.EmployeeId = w.EmployeeId and w.HotelId = #hotelId
Where
w.EmployeeID Is Null And (
e.EmployeeId Like #keyword+'%' Or
e.Something Like #keyword+'%' Or -- appears to be missing from question
e.DateOfBirth Like #keyword+'%'
);
after that you'll be looking at indexing to speed things up.
I have a Projects table with ID and Responsible manager. The Responsible manager columns has values as John,Jim for Project 1 and Jim,Julie for Project 2.
But if I pass Jim to my stored procedure I should get 2 projects (1,2). This returns no rows because the column is John,Jim but SQL Server is looking for ='Jim':
select distinct ID,Manager from Projects where Manager=#Manager
WHERE ',' + Manager + ',' LIKE '%,Jim,%'
Or I suppose to match your actual code:
WHERE ',' + Manager + ',' LIKE '%,' + #Manager + ',%'
Note that your design is extremely flawed. There is no reason you should be storing names in this table at all, never mind a comma-separated list of any data points. These facts are important on their own, so treat them that way!
CREATE TABLE dbo.Managers
(
ManagerID INT PRIMARY KEY,
Name NVARCHAR(64) NOT NULL UNIQUE, ...
);
CREATE TABLE dbo.Projects
(
ProjectID INT PRIMARY KEY,
Name NVARCHAR(64) NOT NULL UNIQUE, ...
);
CREATE TABLE dbo.ProjectManagers
(
ProjectID INT NOT NULL FOREIGN KEY REFERENCES dbo.Projects(ProjectID),
ManagerID INT NOT NULL FOREIGN KEY REFERENCES dbo.Managers(ManagerID)
);
Now to set up the sample data you mentioned:
INSERT dbo.Managers(ManagerID, Name)
VALUES(1,N'John'),(2,N'Jim'),(3,N'Julie');
INSERT dbo.Projects(ProjectID, Name)
VALUES(1,N'Project 1'),(2,N'Project 2');
INSERT dbo.ProjectManagers(ProjectID,ManagerID)
VALUES(1,1),(1,2),(2,2),(2,3);
Now to find all the projects Jim is managing:
DECLARE #Manager NVARCHAR(32) = N'Jim';
SELECT p.ProjectID, p.Name
FROM dbo.Projects AS p
INNER JOIN dbo.ProjectManagers AS pm
ON p.ProjectID = pm.ProjectID
INNER JOIN dbo.Managers AS m
ON pm.ManagerID = m.ManagerID
WHERE m.name = #Manager;
Or you can even manually short circuit a bit:
DECLARE #Manager NVARCHAR(32) = N'Jim';
DECLARE #ManagerID INT;
SELECT #ManagerID = ManagerID
FROM dbo.Managers
WHERE Name = #Manager;
SELECT p.ProjectID, p.Name
FROM dbo.Projects AS p
INNER JOIN dbo.ProjectManagers AS pm
ON p.ProjectID = pm.ProjectID
WHERE pm.ManagerID = #ManagerID;
Or even more:
DECLARE #Manager NVARCHAR(32) = N'Jim';
DECLARE #ManagerID INT;
SELECT #ManagerID = ManagerID
FROM dbo.Managers
WHERE Name = #Manager;
SELECT ProjectID, Name
FROM dbo.Projects AS p
WHERE EXISTS
(
SELECT 1
FROM dbo.ProjectManagers AS pm
WHERE pm.ProjectID = p.ProjectID
AND pm.ManagerID = #ManagerID
);
As an aside, I really, really, really hope the DISTINCT in your original query is unnecessary. Do you really have more than one project with the same name and ID?
In a WHERE clasue a = operator looks for an exact match. You can use a LIKE with wildcards for a partial match.
where Manager LIKE '%Jim%'
You may try the following:
SELECT DISTINCT
ID,
Manager
FROM
Projects
WHERE
(
(Manager LIKE #Manager + ',*') OR
(Manager LIKE '*,' + #Manager) OR
(Manager = #Manager)
)
That should cover both names and surnames, while still searching for literal values. Performance can be a problem however, depending on table
please tried with below query
select distinct ID,Manager from Projects where replace('#$#' + Manager + '#$#', ',', '#$#') like '%Jim%'