I am trying to create a stored procedure which searches employees from the table employee.
My query looks like this
ALTER PROCEDURE [dbo].[EmployeeList]
#CurrentPageNo INT = null,
#PageSize INT = 5,
#FirstName VARCHAR(100),
#Email VARCHAR(100),
#DeptId int = null
AS
BEGIN
DECLARE #StartRecordNo INT, #EndRecordNo INT
IF #CurrentPageNo IS NULL
SET #CurrentPageNo = 1
SET #StartRecordNo = ((#CurrentPageNo - 1) * #PageSize)
SET #EndRecordNo = #StartRecordNo + #PageSize
SELECT
ROW_NUMBER() OVER (ORDER BY CreatedByDate desc) As srno,
EmployeeID, FirstName, LastName, Email, DateOfBirth, Joining,
MobileNumber, MonthsOfExperience, TotalExperience, DepartmentName
FROM
Employee
INNER JOIN
Department ON Employee.DeptId = Department.DepartmentID
WHERE
(ISNULL(#FirstName, '') = '' OR Employee.FirstName LIKE '%' + FirstName + '%')
AND
(ISNULL(#Email, '') = '' OR Employee.Email LIKE '%' + #Email + '%')
AND
(#DeptId IS NULL OR Employee.DeptId = #DeptId)
END
The procedure is compiled successfully. but when execute it like
exec EmployeeList 1,10,'shaili','',null
It shows other records too. Can anyone explain this? I want only the record with the given name.
Result I got:
#Siddhant only little mistake by you. i.e you pass FirstName instead of #FirstName. check below code.
ALTER PROCEDURE [dbo].[Test_EmployeeList]
#CurrentPageNo INT = null,
#PageSize INT=5,
#FirstName VARCHAR(100),
#Email VARCHAR(100),
#DeptId int = null
AS
BEGIN
DECLARE #StartRecordNo INT,#EndRecordNo INT
IF #CurrentPageNo IS NULL
SET #CurrentPageNo=1
SET #StartRecordNo=((#CurrentPageNo-1) * #PageSize)
SET #EndRecordNo=#StartRecordNo + #PageSize
SELECT ROW_NUMBER()OVER(ORDER BY Reg_Date desc) As srno,
F_Name, L_Name, Email, Mobile, Reg_Date from tbl_LoginMaster
inner join tbl_Login_Detail
ON tbl_LoginMaster.User_Id = tbl_Login_Detail.User_Id
WHERE (ISNULL(#FirstName,'')='' OR tbl_LoginMaster.F_Name LIKE '%'+#FirstName+'%') AND
(ISNULL(#Email,'')='' OR tbl_LoginMaster.Email LIKE '%'+#Email+'%') AND
(#DeptId is null OR tbl_LoginMaster.User_Id = #DeptId )
END
GO
exec Test_EmployeeList 1,10,'Mohit','',null
Related
I am using below code to update a record, it updates the specific record, but it returns -1 .
I am using Dapper to execute this procedure:
ALTER PROCEDURE [dbo].[Contact]
#ContactId VARCHAR(50) ,
#AccountId VARCHAR(50) ,
#EmailAddress NVARCHAR(100)
AS
BEGIN
DECLARE MID bigint = 0
SELECT TOP 1 MID = CID
FROM Type3002
WHERE ATTRIBUTE_2710 LIKE CAST(#EMailAddress AS VARCHAR(100))
IF NOT EXISTS (SELECT * FROM Type3002
WHERE ContactId LIKE '%,' + CAST(#ContactId AS VARCHAR(100)) + ',%')
BEGIN
UPDATE Type3002
SET ATTRIBUTE_2710 = LEFT(#EMailAddress, 50),
ContactId = ContactId +',' + #ContactId,
AccountId = AccountId ++','+ #AccountId
WHERE CID = MID
END
ELSE IF NOT EXISTS (SELECT * FROM Type3002
WHERE AccountId LIKE '%,' + #AccountId + ',%'
AND ContactId LIKE '%,' + #ContactId)
BEGIN
UPDATE Type3002
SET AccountId = AccountId +','+ #AccountId
WHERE CID = MID
END
END
Code that call this procedure is like this :
var result = await connection.ExecuteAsync("[Contact]",
You procedure is missing a RETURN statement.
Note that in Transact-SQL, RETURN is seldom used (it can only be an int value, and cannot be NULL). OUTPUT parameters are much more useful.
Also MID should be #MID everywhere.
How do I count the number of actual records returned by a group by query
For e.g
-- Exec SP_GET_ITEM_STOCK 0,10,NULL,'Charger'
ALTER PROCEDURE [dbo].[SP_GET_ITEM_STOCK]
#startRowIndex int ,
#pageSize int,
#ItemID bigint = null,
#ItemName varchar(250) = null
AS
BEGIN
DECLARE #SQL varchar(MAX)
DECLARE #SQLWHERE varchar(MAX)
SET #SQL = 'WITH DATA AS (
select
ROW_NUMBER() OVER (ORDER BY Item_ID) ''SNo'',
Item_ID,
Item_Name,
SUM(Inward) as Total_Purchase,
SUM(Outward) as Total_Sale,
(sum(Inward) - sum(outward))as Balance_Stock'
Set #SQLWHERE = ' from Item_Ledger_Details where Active = 1'
IF #ItemID IS NOT NULL and #ItemID <> ''
SET #SQLWHERE = #SQLWHERE + ' and Item_ID = ' + CONVERT(varchar,#ItemID) + ''
IF #ItemName IS NOT NULL and #ItemName <> ''
SET #SQLWHERE = #SQLWHERE + ' and Item_Name like ''%' + #ItemName + '%'''
SET #SQL = #SQL + #SQLWHERE + ' group by Item_ID,Item_Name) SELECT * FROM DATA WHERE SNo BETWEEN ' + CONVERT(Varchar,#startRowIndex) + ' AND ' + CONVERT(Varchar,#startRowIndex+#pageSize) + ' ORDER BY SNo'
EXEC(#SQL +';SELECT COUNT(*) ''Count'' '+ #SQLWHERE)
print(#SQL)
END
Which returns:
I need to count the above first result records to get 1 + 1 = 2 in second result where I get count = 48
Continue Anand Answer. I've just modify his query. Below query is solved my answer. But I think may be this query needs optimization.
-- SP_GET_ITEM_STOCK 0,10,NULL,NULL
ALTER PROCEDURE [dbo].[SP_GET_ITEM_STOCK]
#startRowIndex int,
#pageSize int,
#ItemID bigint = null,
#ItemName varchar(250) = null
AS
BEGIN
Declare #Temp Table (
SNo bigint,
Item_ID bigint,
Item_Name varchar(max),
Total_Purchase money,
Total_Sale money,
Balance_Stock money
);
WITH DATA AS (
select
ROW_NUMBER() OVER (ORDER BY Item_ID) as SNo,
Item_ID,
Item_Name,
SUM(Inward) as Total_Purchase,
SUM(Outward) as Total_Sale,
(sum(Inward) - sum(outward))as Balance_Stock
from Item_Ledger_Details
where Active = 1
and (coalesce(#ItemID, '') = '' or Item_ID = CONVERT(varchar,#ItemID))
and ( coalesce(#ItemName, '') = '' or Item_Name like '%' + #ItemName + '%')
group by Item_ID,
Item_Name
)
insert into #Temp
SELECT *
FROM DATA
WHERE SNo BETWEEN #startRowIndex AND #startRowIndex+#pageSize
ORDER BY SNo
select * from #temp
SELECT COUNT(*) as Count from #temp
END
There, I've cleaned it up:
ALTER PROCEDURE [dbo].[SP_GET_ITEM_STOCK]
#startRowIndex int,
#pageSize int,
#ItemID bigint = null,
#ItemName varchar(250) = null
AS
BEGIN
;WITH DATA AS (
select
ROW_NUMBER() OVER (ORDER BY Item_ID) as SNo,
Item_ID,
Item_Name,
SUM(Inward) as Total_Purchase,
SUM(Outward) as Total_Sale,
(sum(Inward) - sum(outward))as Balance_Stock
from Item_Ledger_Details
where Active = 1
and (coalesce(#ItemID, '') = '' or Item_ID = CONVERT(varchar,#ItemID))
and ( coalesce(#ItemName, '') = '' or Item_Name like '%' + #ItemName + '%')
group by Item_ID,
Item_Name
)
SELECT *
FROM DATA
WHERE SNo BETWEEN #startRowIndex AND #startRowIndex+#pageSize
ORDER BY SNo
SELECT COUNT(*) as Count
from DATA
END
alter PROCEDURE sp_Get_CustInfoSerach2
(#PageIndex INT = 1
,#PageSize INT = 10
,#RecordCount INT OUTPUT
,#ColumnName VARCHAR(50)=null
,#Value VARCHAR(50)=null
,#ddlValue VARCHAR(50)=null
,#txtValue VARCHAR(50)=null
,#status varchar(30))
AS
BEGIN
SET NOCOUNT ON;
DECLARE #cmd AS NVARCHAR(max)
if #txtValue IS NULL
Begin
SET #Value = ''''+#ddlValue+ ''''
End
else if #ddlValue IS NULL
begin
SET #Value = ''''+#txtValue+ ''''
end
SET #cmd = 'SELECT ROW_NUMBER() OVER
(
ORDER BY C_Register.UserId desc
)AS RowNumber
,C_Register.UserId, C_Register.Name, C_Register.UserName, C_Register.Status,
Packages.PackagePeriod, Packages.PackageName, C_Register.ActivationDate,
Receive_Payment.OldExpiryDate, Receive_Payment.Balance, Receive_Payment.PyingAmount,
Receive_Payment.LastPaidDate, C_Register.LastUpdateTime,
Area.AreaName, C_Register.MobNo, Employee.EmpName, C_Register.Address,C_Register.CreatedDate
INTO'+ #Results+'
FROM C_Register INNER JOIN Receive_Payment ON C_Register.UserId = Receive_Payment.UserId
INNER JOIN Area ON C_Register.AreaId = Area.AreaId
INNER JOIN Employee ON Receive_Payment.EmpId = Employee.EmpId
INNER JOIN Packages ON Receive_Payment.PackageId = Packages.PackageId
where C_Register.AccountExpiry= Receive_Payment.OldExpiryDate And C_Register.Status = '+#status+'And
' + #ColumnName + ' = ' + #Value
SELECT #RecordCount = COUNT(*)
FROM #Results
SELECT * FROM #Results
WHERE RowNumber BETWEEN(#PageIndex -1) * #PageSize + 1 AND(((#PageIndex -1) * #PageSize + 1) + #PageSize) - 1
DROP TABLE #Results
EXEC(#cmd)
END
throwing error:
Invalid column name '#Results'.
how to solve it?
If i understand your problem correctly you should first exec dynamic sql before select from temporary table
Alter PROCEDURE sp_Get_CustInfoSerach2
(
#PageIndex INT = 1 ,
#PageSize INT = 10 ,
#RecordCount INT OUTPUT ,
#ColumnName VARCHAR(50)=null ,
#Value VARCHAR(50)=null ,
#ddlValue VARCHAR(50)=null ,
#txtValue VARCHAR(50)=null ,
#status varchar(30)
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #cmd AS NVARCHAR(max)
If #txtValue IS NULL
Begin
SET #Value = ''''+#ddlValue+ ''''
End
Else
if #ddlValue IS NULL
Begin
SET #Value = ''''+#txtValue+ ''''
End
SET #cmd = 'SELECT ROW_NUMBER() OVER ( ORDER BY C_Register.UserId desc )AS RowNumber,
C_Register.UserId, C_Register.Name, C_Register.UserName, C_Register.Status,
Packages.PackagePeriod, Packages.PackageName, C_Register.ActivationDate,
Receive_Payment.OldExpiryDate, Receive_Payment.Balance, Receive_Payment.PyingAmount,
Receive_Payment.LastPaidDate, C_Register.LastUpdateTime,
Area.AreaName, C_Register.MobNo, Employee.EmpName, C_Register.Address,C_Register.CreatedDate
INTO #Results
FROM C_Register INNER JOIN Receive_Payment ON C_Register.UserId = Receive_Payment.UserId
INNER JOIN Area ON C_Register.AreaId = Area.AreaId
INNER JOIN Employee ON Receive_Payment.EmpId = Employee.EmpId
INNER JOIN Packages ON Receive_Payment.PackageId = Packages.PackageId
where C_Register.AccountExpiry= Receive_Payment.OldExpiryDate And C_Register.Status = '+#status+'And
' + #ColumnName + ' = ' + #Value
/*First Execute above dynamic Sql query */
EXEC(#cmd)
/* From the above execute statemnet the query will executed and temporary table will created on the fly */
SELECT #RecordCount = COUNT(*)
FROM #Results
SELECT * FROM #Results
WHERE RowNumber BETWEEN(#PageIndex -1) * #PageSize + 1 AND(((#PageIndex -1) * #PageSize + 1) + #PageSize) - 1
DROP TABLE #Results
Problem in your query is creation of temp table thats not right way to create temp table inside dynamic query.
But even if you create temp table inside a dynamic query it can be accessed only inside the session of dynamic query. If you try to select the temp outside of dynamic query which is created inside the dynamic query you will get a error saying object doesnot exist.
Since you know the number of columns present in the result of dynamic query you can create the temp table outside of dynamic query and insert the records through dynamic query.
Try changing your procedure like this.
ALTER PROCEDURE Sp_get_custinfoserach2 (#PageIndex INT = 1,
#PageSize INT = 10,
#RecordCount INT output,
#ColumnName VARCHAR(50)=NULL,
#Value VARCHAR(50)=NULL,
#ddlValue VARCHAR(50)=NULL,
#txtValue VARCHAR(50)=NULL,
#status VARCHAR(30))
AS
BEGIN
SET nocount ON;
DECLARE #cmd AS NVARCHAR(max)
IF #txtValue IS NULL
BEGIN
SET #Value = '''' + #ddlValue + ''''
END
ELSE IF #ddlValue IS NULL
BEGIN
SET #Value = '''' + #txtValue + ''''
END
/*create a temp as same structure of your dynamic query select statement*/
CREATE TABLE #result
(
rownum INT,
userid INT,
NAME VARCHAR(100),
username VARCHAR(100),
status VARCHAR(15),
packageperiod VARCHAR(15),
packagename VARCHAR(100),
activationdate DATETIME,
oldexpirydate DATETIME,
balance NUMERIC(22, 4),
pyingamount NUMERIC(22, 4),
lastpaiddate DATETIME,
lastupdatetime DATETIME,
areaname VARCHAR(100),
mobno INT,
empname VARCHAR(100),
address VARCHAR(5000),
createddate DATETIME
)
SET #cmd =
' Insert into #result
SELECT ROW_NUMBER() OVER (ORDER BY C_Register.UserId desc )AS RowNumber,
C_Register.UserId, C_Register.Name, C_Register.UserName,
C_Register.Status, Packages.PackagePeriod, Packages.PackageName,
C_Register.ActivationDate,Receive_Payment.OldExpiryDate,
Receive_Payment.Balance, Receive_Payment.PyingAmount,
Receive_Payment.LastPaidDate, C_Register.LastUpdateTime,
Area.AreaName, C_Register.MobNo, Employee.EmpName,
C_Register.Address,C_Register.CreatedDate
FROM C_Register
INNER JOIN Receive_Payment
ON C_Register.UserId = Receive_Payment.UserId
INNER JOIN Area
ON C_Register.AreaId = Area.AreaId
INNER JOIN Employee
ON Receive_Payment.EmpId = Employee.EmpId
INNER JOIN Packages
ON Receive_Payment.PackageId = Packages.PackageId
where C_Register.AccountExpiry= Receive_Payment.OldExpiryDate And C_Register.Status = ' + #status + ' And ' + #ColumnName + ' = ' + #Value
SELECT #RecordCount = Count(*)
FROM #results
SELECT *
FROM #results
WHERE rownumber BETWEEN( #PageIndex - 1 ) * #PageSize + 1 AND( (
( #PageIndex - 1 ) * #PageSize + 1 ) +
#PageSize ) - 1
DROP TABLE #results
EXEC(#cmd)
END
Note : I have given generic datatypes to temp table columns please change the datetypes according to your schema.
**
I have make a Stored procedure in sql-server 2008 for search
purpose,Below code represents my stored procedure, while executing
stored procedure it gives error like conversion failed varchar to data
type int
**
ALTER PROCEDURE [dbo].[SUMMARY_DETAILS_PROJECT_SEARCH_MAP]
#PROJECT_CAT INT=NULL,
#PROJECT_TYPE INT=NULL,
#ADDRESS VARCHAR(150)=NULL,
#MIN_PRICE INT=NULL,
#MAX_PRICE INT=NULL,
#BEDROOM VARCHAR(150)=NULL
AS
CREATE TABLE #TEMP_MAP
(
SN INT IDENTITY,
PROJECT_ID INT,
PROJECT_NAME VARCHAR(100),
LAT VARCHAR(50),
LONG VARCHAR(50),
PROJECT_LOC VARCHAR(150),
MIN_PRICE VARCHAR(100),
MAX_PRICE VARCHAR(100),
PROJECT_IMAGE VARCHAR(250)
)
DECLARE #SQL NVARCHAR(MAX),#PARAMETER_LIST NVARCHAR(MAX)
DECLARE #COUNT_ZONE INT
DECLARE #COUNT_CITY INT
SELECT #COUNT_ZONE= (SELECT COUNT(*) FROM ZONE_MASTER WHERE ZONE_NAME LIKE #ADDRESS + '%')
SELECT #SQL = ('SELECT PROJECT_ID FROM PROJECTS WHERE STATUS=1')
IF(#PROJECT_CAT !=0)
SELECT #SQL = #SQL+' AND PROJECT_CAT_ID=#XP_PROJECT_CAT'
IF(#PROJECT_TYPE !=0)
SELECT #SQL = #SQL+' AND PROJECT_TYPE_ID=#XP_PROJECT_TYPE'
IF(#COUNT_ZONE !=0)
SELECT #SQL = #SQL+' AND ZONE_ID IN (SELECT ZONE_ID FROM ZONE_MASTER WHERE ZONE_NAME LIKE '''+#ADDRESS+''' + ''%'')'
ELSE
SELECT #SQL = #SQL+' AND BLOCK_ID IN (SELECT BLOCK_ID FROM BLOCK_MASTER WHERE BLOCK_NAME LIKE '''+#ADDRESS+''' + ''%'')'
IF(#MIN_PRICE !=0)
SELECT #SQL = #SQL+' AND MIN_ID >=#XP_MIN_PRICE'
IF(#MAX_PRICE !=0)
SELECT #SQL = #SQL+' AND MAX_ID <=#XP_MAX_PRICE'
IF(#BEDROOM !='')
SELECT #SQL = #SQL+' AND BEDROOM=#XP_BEDROOM'
SELECT #SQL = #SQL + ' ORDER BY PROJECT_ENTRY_DATE DESC '
SELECT #PARAMETER_LIST = '#XP_PROJECT_CAT INT,#XP_PROJECT_TYPE INT,#XP_MIN_PRICE INT,#XP_MAX_PRICE INT,#XP_BEDROOM VARCHAR(100)'
INSERT INTO #TEMP_MAP (PROJECT_ID)
EXEC SP_EXECUTESQL #SQL,#PARAMETER_LIST,'#XP_PROJECT_CAT','#XP_PROJECT_TYPE','#XP_MIN_PRICE','#XP_MAX_PRICE','#XP_BEDROOM'
PRINT #SQL
UPDATE #TEMP_MAP SET
PROJECT_NAME = P.PROJECT_NAME,
LAT = P.PROJECT_LAT,
LONG = P.PROJECT_LONG,
PROJECT_LOC = P.PROJECT_LOC,
MIN_PRICE = P.MAX_PRICE,
MAX_PRICE = P.MIN_PRICE,
PROJECT_IMAGE = './UploadImage/Upload_Project/' + P.PROJECT_NAME +'/' + 'Google' + '/' + [IMG1]
FROM Projects P WHERE P.PROJECT_ID=#TEMP_MAP.PROJECT_ID
SELECT * FROM #TEMP_MAP
DROP TABLE #TEMP_MAP
It is because you are putting single quotes around the values you are passing to sp_executesql, e.g:
EXEC SP_EXECUTESQL #SQL,#PARAMETER_LIST,'#XP_PROJECT_CAT'
Here you are not passing the integer value of #XP_PROJECT_CAT, but you are passing the string '#XP_PROJECT_CAT', and the procedure is expecting an int.
As an aside, I am not sure why you have opted for parameterising everything apart from address? Surely it would make sense to parameterise this too - e.g.
IF(#COUNT_ZONE !=0)
SELECT #SQL = #SQL+' AND ZONE_ID IN (SELECT ZONE_ID
FROM ZONE_MASTER
WHERE ZONE_NAME LIKE #ADDRESS + ''%'')'
ELSE
SELECT #SQL = #SQL+' AND BLOCK_ID IN (SELECT BLOCK_ID
FROM BLOCK_MASTER
WHERE BLOCK_NAME LIKE #ADDRESS + ''%'')'
Then add #Address as a parameter to sp_executesql
I'm passing parameters to a stored procedure from an asp.net webform.
However, for one particular combination of parameters a single parameter appear to being ignored.
The parameters I'm passing that are causing the issue are as follows:
EXEC #return_value = [dbo].[spProgressCohorts]
#StuYear = N'10',
#DataCollection = N'March 2013 Teacher Assessments',
#SubjectName = N'English',
#TeachingGroup = N'Select All',
#Subgroup = N'Select All',
#KS2 = '',
#Result = ''
When I run the stored procedure with these parameters the results return as though the #Result variable is being ignored.
The results the stored procedure return are (the ks2en and result columns are included for reference):
surname forename ks2en result
El Hajj Zeinab D
Grzelak Marlena F+
Sage Nigel
However, I get just get Nigel Sage's record (as expected and this is what should be returned by the stored procedure) when when I run the following query:
select surname, forename, ks2en, result
from student join subject on subject.upn=student.upn
where datacollection='March 2013 Teacher Assessments' and stuyear='10' and name='english' and result='' and ks2en=''
My stored procedure is massive, so I'll just include code specific to the parameters above:
ALTER PROCEDURE [dbo].[spProgressCohorts]
#StuYear varchar(2),
#DataCollection varchar(100),
#SubjectName varchar(100),
#TeachingGroup varchar(30),
#Subgroup varchar(10),
#Result varchar(4),
#KS2 varchar(4)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
--interfering with SELECT statements.
SET NOCOUNT ON;
SELECT Forename + ' ' + Surname
FROM student
JOIN subject
ON subject.upn = student.upn
WHERE
(#StuYear = [stuyear]
AND #TeachingGroup = 'Select All'
AND [DataCollection] = #DataCollection
AND [Name] = #SubjectName
AND #Subgroup='Select All'
AND #KS2 = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND [Result] like #Result + '%')
OR
(#StuYear = [stuyear]
AND #TeachingGroup Not Like 'Select All'
AND [DataCollection] = #DataCollection
AND [Name] = #SubjectName
AND [TeachingGroup] = #TeachingGroup
AND #Subgroup='Select All'
AND #KS2 = CASE #subjectName WHEN 'English' THEN KS2en WHEN 'Mathematics' THEN KS2ma ELSE KS2av END
AND [Result] like #Result + '%')
ORDER BY surname, forename
How about this?
...
AND [Result] LIKE CASE #Result WHEN '' THEN '' ELSE #Result + '%' END)