Why my query isn't showing correct results? - sql

I have many tables but for instance let's consider 2 tables i.e. Employees and FileEntries. I am picking these fields from both tables EmployeeID,FileStatus,FileName,EmployeeName.
These both tables have 1 common field i.e. EmployeeID. FileEntries can have EmployeeID being present even if it's not in Employees table. So i want to pick all records from both tables but a new field should show 'NotRegistered' if it is not in Employees table.
)
#EmployeeID int
)
Alter PROCEDURE [dbo].[Modify_RejectedFiles] -- '2015-01-06 07:41:00', '2015-01-06 07:41:00',1,3,'','20001018783815'
(
#FromDate SMALLDATETIME,
#ToDate SMALLDATETIME,
#OfficeID INT=0, --it represents id of a company/branch/organization being logged in
#Type INT=0, --it represents type of a user being logged in i.e. 1=orgranization, 2=company, 3=branch
#EmployerUniqueID VARCHAR(15)='',
#EmployeeUniqueID VARCHAR(15)=''
)
AS
BEGIN
DECLARE #Branches TABLE
(
BranchID INT
)
IF #Type = 1 --Organization
BEGIN
INSERT INTO #Branches
SELECT BranchID From vw_OrganizationTree WHERE OrganizationID = #OfficeID --inserts 3 always because we got only 1 organization i.e. 3
END
IF #Type = 2 --Company
BEGIN
INSERT INTO #Branches
SELECT BranchID From vw_OrganizationTree WHERE CompanyID = #OfficeID --inserts 3 always because we got only 1 company i.e. 3
END
IF #Type = 3 -- i.e. Branch
BEGIN
INSERT INTO #Branches
SELECT BranchID From vw_OrganizationTree WHERE BranchID = #OfficeID
END
Declare #IsRegistered varchar(20)= 'Registered'
If ((Select count(*) from RegisteredEmployees where EmployeeUniqueID= #EmployeeUniqueID) <1)
Begin
Set #IsRegistered = 'Not Yet'
End
Select distinct FE.EmployeeUniqueID, RE.EmployeeName, Empr.EmployerName, Br.BranchName,
FE.IsRejected, FE.RejectedFileCreationDateTime
From
File_EDREntries FE
left Join
RegisteredEmployees RE
ON FE.EmployeeUniqueID = RE.EmployeeUniqueID
left Join Employers Empr
ON RE.Employer_ID = Empr.ID
left Join Branches Br
ON Br.BranchID = Empr.Branch_ID
WHERE
FE.IsRejected = 1 --20017128203780
AND Empr.Branch_ID in (Select BranchID from #Branches)

Use Case when RE.EmployeeUniqueID is null then 'Not Yet' else 'Registered' End in your final select Query

Related

Stored procedure for finding out if student is eligible for course with pre reqs

I have this stored procedure for a course registration system I am working on. My intention is to return a value of -1 if the query returns a course which a student has not taken according to the course pre requisites.
The pre req table only has two columns; CourseID being the course and PreReqCourse_ID being the required course for that specified course. If the student has taken all pre req courses then it should return a value of 1. I keep getting a value of -1 even when I run the query for a student who has taken the required pre req courses. Any help would be much appreciated!
CREATE PROCEDURE CheckPreReq
#StudentID INT,
#CourseID INT
AS
DECLARE #theCount INT
IF EXISTS (SELECT *
FROM PreReq
INNER JOIN Student_History ON (PreReq.Course_ID = #CourseID)
WHERE Student_History.Course_ID != PreReq.PreReqCourse_ID
AND Student_History.Student_ID = #StudentID)
BEGIN
SET #theCount =-1
END
ELSE
BEGIN
SET #theCount = 1
END
RETURN #theCount
Would something like this work?
DECLARE #PreReqsTotal tinyint
DECLARE #PreReqsFulfilled tinyint
-- Count pre-req courses.
SELECT #PreReqsTotal = COUNT(*)
FROM PreReq
WHERE [CourseID] = #CourseId
-- Count how many fulfilled by student.
SELECT #PreReqsFulfilled = count(*)
FROM Student_History hist
JOIN PreReq pre
on hist.Course_ID = pre.PreReqCourse_ID
WHERE pre.CourseID = #CourseID
and hist.Student_ID = #StudentID
RETURN CASE WHEN #PreReqsTotal = #PreReqsFulfilled THEN 1 ELSE -1 END
...or something like this:
IF EXISTS
(
SELECT blah.*
FROM
(
SELECT pre.*
,[PreFulfilled] = CASE WHEN hist.Course_ID is null THEN 0 ELSE 1 END
FROM PreReq pre
LEFT JOIN
Student_History hist
on pre.PreReqCourse_ID = hist.Course_ID
and hist.Student_ID = #StudentID
WHERE pre.CourseID = #CourseID
) blah
WHERE blah.[PreFulfilled] = 0 -- Unfulfilled PreReq.
)
BEGIN
RETURN -1 -- Has an unfulfilled PreReq.
END
RETURN 1 -- No unfulfilled PreReqs.
You should JOIN the PreReq table with the Student_History on the the PreReq.PreReqCourse_ID and Student_History.CourseID columns (take a look at my example). Then your SP should work.
--create tmp example tables
IF OBJECT_ID('tempdb..#PreReq') IS NOT NULL DROP TABLE #PreReq
CREATE TABLE #PreReq(
CourseID int,
PreReqCourse_ID int
)
--insert Course 3 which depends on Course 2 and 1 in #PreReq
INSERT INTO #PreReq
values(3,2),(3,1)
IF OBJECT_ID('tempdb..#Student_History') IS NOT NULL DROP TABLE #Student_History
CREATE TABLE #Student_History(
CourseID int not null,
StudentID int not null
);
--insert Student 1 who has visited Course 1 and 2
insert into #Student_History
VALUES(1,1),(2,1)
--declare variables
DECLARE #CourseID AS INT = 3
,#StudentID AS INT = 1
--COUNT on how many Courses #CourseID depends
,#necessaryCourses AS INT
--COUNT on how many Courses the Student has taken
,#countTakenCourses AS INT
,#theCount AS INT
SET #necessaryCourses = (SELECT count(*) FROM #PreReq WHERE CourseID = #CourseID);
SET #countTakenCourses = (
SELECT count(*)
FROM #PreReq p
--JOIN with Student_History to check if the student has visited the necessary course
JOIN #Student_History h on p.PreReqCourse_ID = h.CourseID
WHERE p.CourseID = #CourseID AND h.StudentID = #StudentID
)
IF #necessaryCourses = #countTakenCourses
BEGIN
set #theCount = 1
END
ELSE
BEGIN
set #theCount = -1
END
SELECT #theCount AS theCount

Why doesn't a where condition work correctly?

I have this procedure and it should return records based on give dates and few other inputs. First it places a branch_ID in a temp table #branches and then pick that branch_ID from temp table but it returns records for all branches not only for the one that has been picked. Why ? But when i put static Branch_ID i.e. 3 then it picks for that.
ALTER PROCEDURE [dbo].[usp_RejectedFiles]
(
#FromDate SMALLDATETIME,
#ToDate SMALLDATETIME,
#OfficeID INT=0,
#Type INT=0
)
AS
BEGIN
DECLARE #Branches TABLE
(
BranchID INT
)
IF #Type = 1
BEGIN
INSERT INTO #Branches SELECT BranchID From vw_OrganizationTree WHERE OrganizationID = #OfficeID
END
IF #Type = 2
BEGIN
INSERT INTO #Branches SELECT BranchID From vw_OrganizationTree WHERE CompanyID = #OfficeID
END
IF #Type = 3
BEGIN
INSERT INTO #Branches SELECT BranchID From vw_OrganizationTree WHERE BranchID = #OfficeID
END
SELECT C.CompanyName,B.BranchName,E.EmployerName,FE.EmployeeUniqueID,pcr.EmployerUniqueID,
FE.IncomeFixedComponent,FE.IncomeVariableComponent,
S.StatusDescription, FE.IsRejected, FE.ID 'EdrID'
From File_EdrEntries FE JOIN PAFFiles pe ON pe.ID = FE.PAFFile_ID
inner join File_PCREntries pcr on pe.ID=pcr.PAFFile_ID
JOIN Employers E ON E.EmployerID = pcr.EmployerUniqueID
JOIN Branches B ON B.BranchID = E.Branch_ID JOIN companies C ON C.COMPANYID = B.COMPANY_ID
JOIN Statuses S ON S.StatusID = FE.Status_ID
where fe.IsRejected=1
AND E.Branch_id = (Select Branch_ID from #Branches)
END
Note :Please confirm that it is not typo
The Problem with the column Name i.e Column Name declare in #Branches table is different than the used in where clause.
Column Name in #Branches table
DECLARE #Branches TABLE
(
BranchID INT
)
And in where clause
...
AND E.Branch_id = (Select Branch_ID from #Branches)
There may be possibility that #Branches table may have multiple rows so instead of comparing equality you should use IN of EXISTS
AND E.Branch_id IN (Select BranchID from #Branches)
Rather than using this temp table, you should be able to compose everything into a single query:
...
JOIN Statuses S ON S.StatusID = FE.Status_ID
where fe.IsRejected=1
AND EXISTS (
SELECT * FROM vw_OrganizationTree v
WHERE v.BranchID = e.BranchID AND
(#Type = 1 and v.OrganizationID = #OfficeID) or
(#Type = 2 and v.CompanyID = #OfficeID) or
(#Type = 3 and v.BranchID = #OfficeID)
)
And as JaydipJ alludes to, the reason why your current code isn't working is because here:
AND E.Branch_id = (Select Branch_ID from #Branches)
There is no column called Branch_ID in the #Branches table. So it becomes a reference to a column in the outer query (if there were more than one such column, it would generate an error). And so it has effectively become:
AND E.Branch_id = E.Branch_ID
Which is why it returns all rows. This is why it's always a good idea to use prefixed column names when using subqueries. This:
AND E.Branch_id = (Select x.Branch_ID from #Branches x)
Would have generated an error about a non-existent column and given a clue to why it wasn't working.

Inserting a temporary table with cursor search function to an existing SQL query

I am needing help with trying to join a there two queries.
The first query is my main query for the report i am trying to build.
PARAMETERS [DocumentNo] String "", [hideComments] boolean False;
Select cus.name, cus.addra, cus.addrb, cus.addrc, cus.addrd, cus.addre, cus.pcode as PostCode, br.name As BranchName, br.addra As BrAddrA, br.addrb As BrAddrB,
br.addrc As BrAddrC, br.addrd As BrAddrD, br.addre As BrAddrE, br.postcode As postcodebr, br.Phonenumber as brphonenumber, v.make as vmake, v.YoM as vyear, v.model as vmodel, v.carcolour as vcolor, v.pkey as license, v.state as vstate, V.mileage as mileageout
,h.datetime, h.document, h.acct, h.inits, h.payref, h.corder, h.goods, h.vat,
(Case IsNull(p.supppart,'') when '' then l.part else p.supppart end) part, l.bopdes,
l.qty, l.unit, c.a2 As 'WorkOrderComments'
from jheads h
inner join jlines l on l.document = h.document
inner join customer cus on cus.keycode = h.acct
left join plines p on l.porder = p.document + '/' + cast(p.seqno as varchar(3))
left join codes c on c.keycode= h.branch and c.prefix='WA' and c.subkey1=0
left join branches br on br.branch = h.branch
left join vehicles v on v.Custacnum = h.acct
where H.Document= [DocumentNo]
The information i am needing to add to this report query is; i need the due date for two types based in the "reminders" table. How this information works is that the type column is populated with one of two options 'EMSN or 'INSP. the "VRM" coloumn from this table related to the "pkey" from vehicles table which is all ready in the first query. The VRM is the license of related to an order, but a license can have multiple EMSN and INSP records, there records are sorted sequentically and i need the latest records to be merged into the report for each type ( so i need two record specifically drawn out)
The query i have used to gather this information and temporary store this is
use [Masterdatabase]
GO
DECLARE #source TABLE
(
CustAcNum VARCHAR(255) NOT NULL,
SeqNo INT NOT NULL,
Class VARCHAR(255),
[type] VARCHAR(255) NOT NULL,
Vrm VARCHAR(12),
Duedate DATETIME,
VehicleId UNIQUEIDENTIFIER
);
DECLARE #filtered TABLE
(
CustAcNum VARCHAR(255) NOT NULL,
SeqNo INT NOT NULL,
Class VARCHAR(255),
[type] VARCHAR(255) NOT NULL,
Vrm VARCHAR(12),
Duedate DATETIME,
VehicleId UNIQUEIDENTIFIER
);
INSERT INTO #source
SELECT v.CustAcNum, r.SeqNo, r.[Class], r.[type], r.Vrm, r.Duedate, r.VehicleId
FROM dbo.Vehicles AS v
INNER JOIN dbo.Reminders AS r ON r.VRM = v.Pkey
WHERE r.[Type] = 'EMSN'
GROUP BY v.CustAcNum, r.SeqNo, r.[Class], r.[type], r.Vrm, r.Duedate, r.VehicleId
UNION
SELECT v.CustAcNum, r.SeqNo, r.[Class], r.[type], r.Vrm, r.Duedate, r.VehicleId
FROM dbo.Vehicles AS v
INNER JOIN dbo.Reminders AS r ON r.VRM = v.Pkey
WHERE r.[Type] = 'INSP'
GROUP BY v.CustAcNum, r.SeqNo, r.[Class], r.[type], r.Vrm, r.Duedate, r.VehicleId
ORDER BY v.CustAcNum, r.Seqno DESC;
DECLARE
#custAcNum VARCHAR(255)
,#seqNo INT
,#class VARCHAR(255)
,#type VARCHAR(255)
,#vrm VARCHAR(12)
,#duedate DATETIME
,#vehicleId UNIQUEIDENTIFIER
DECLARE source_cursor CURSOR
FAST_FORWARD
FOR SELECT * FROM #source WHERE NULLIF(ltrim(rtrim(CustAcNum)), '') IS NOT NULL;
OPEN source_cursor
FETCH NEXT FROM source_cursor
INTO #custAcNum, #seqNo, #class, #type, #vrm, #duedate, #vehicleId;
WHILE ##FETCH_STATUS = 0
BEGiN
IF NOT EXISTS (SELECT 1 FROM #filtered WHERE CustAcNum = #custAcNum AND [type] = #type)
BEGIN
INSERT INTO #filtered
VALUES (#custAcNum, #seqNo, #class, #type, #vrm, #duedate, #vehicleId);
END
FETCH NEXT FROM source_cursor
INTO #custAcNum, #seqNo, #class, #type, #vrm, #duedate, #vehicleId;
END
CLOSE source_cursor;
DEALLOCATE source_cursor;
select * from #filtered;
I'm now needing to join these two together and be able to extract the due date out of them for both type's.
I'm stuck on this so any help i would appreciate it.

Loop through a recordset and use the result to do another SQL select and return the results

I am completely new to stored procedure. This time, I need to create a stored procedure in MS SQL.
Let's say I have the following table.
Table name: ListOfProducts
--------------------------
SomeID, ProductID
34, 4
35, 8
35, 11
How do I pass in a SomeID. Use this SomeID to select a recordset from table, ListOfProducts. Then loop through this record set.
Let's say I pass in SomeID = 35.
So, the record set will return 2 records with SomeID 35. In the loop, I will get ProductID 8 and 11, which will be used to do another select from another table.
The stored procedure should return the results from the 2nd select.
How can I do this in MS SQL stored procedure?
Sorry, for this newbie question. Thanks for any help.
If you want looping through the records. You can do like:
--Container to Insert Id which are to be iterated
Declare #temp1 Table
(
tempId int
)
--Container to Insert records in the inner select for final output
Declare #FinalTable Table
(
Id int,
ProductId int
)
Insert into #temp1
Select Distinct SomeId From YourTable
-- Keep track of #temp1 record processing
Declare #Id int
While((Select Count(*) From #temp1)>0)
Begin
Set #Id=(Select Top 1 tempId From #temp1)
Insert Into #FinalTable
Select SomeId,ProductId From ListOfProducts Where Id=#Id
Delete #temp1 Where tempId=#Id
End
Select * From #FinalTable
There is probably no point in writing an explicit loop if you don't need to preform some action on the products that can't be done on the whole set. SQL Server can handle stuff like this much better on its own. I don't know what your tables look like, but you should try something that looks more like this.
CREATE PROC dbo.yourProcName
#SomeID int
AS
BEGIN
SELECT
P.ProductId,
P.ProductName
FROM
Product P
JOIN
ListOfProducts LOP
ON LOP.ProductId = P.ProductId
WHERE
LOP.SomeId = #SomeID
END
I had to do something similar in order to extract hours from a select resultset start/end times and then create a new table iterating each hour.
DECLARE #tCalendar TABLE
(
RequestedFor VARCHAR(50),
MeetingType VARCHAR(50),
RoomName VARCHAR(MAX),
StartTime DATETIME,
EndTime DATETIME
)
INSERT INTO #tCalendar(RequestedFor,MeetingType,RoomName,StartTime,EndTime)
SELECT req as requestedfor
,meet as meetingtype
,room as rooms
,start as starttime
,end as endtime
--,u.datetime2 as endtime
FROM mytable
DECLARE #tCalendarHours TABLE
(
RequestedFor VARCHAR(50),
MeetingType VARCHAR(50),
RoomName VARCHAR(50),
Hour INT
)
DECLARE #StartHour INT,#EndHour INT, #StartTime DATETIME, #EndTime DATETIME
WHILE ((SELECT COUNT(*) FROM #tCalendar) > 0)
BEGIN
SET #StartTime = (SELECT TOP 1 StartTime FROM #tCalendar)
SET #EndTime = (SELECT TOP 1 EndTime FROM #tCalendar)
SET #StartHour = (SELECT TOP 1 DATEPART(HOUR,DATEADD(HOUR,0,StartTime)) FROM #tCalendar)
SET #EndHour = (SELECT TOP 1 DATEPART(HOUR,DATEADD(HOUR,0,EndTime)) FROM #tCalendar)
WHILE #StartHour <= #EndHour
BEGIN
INSERT INTO #tCalendarHours
SELECT RequestedFor,MeetingType,RoomName,#StartHour FROM #tCalendar WHERE StartTime = #StartTime AND EndTime = #EndTime
SET #StartHour = #StartHour + 1
END
DELETE #tCalendar WHERE StartTime = #StartTime AND EndTime = #EndTime
END
Do something like this:
Declare #ID int
SET #ID = 35
SELECT
p.SomeID
,p.ProductID
FROM ListOfProducts p
WHERE p.SomeID = #ID
-----------------------
--Or if you have to join to get it
Declare #ID int
SET #ID = 35
SELECT
c.SomeID
,p.ProductID
,p.ProductName
FROM ListOfProducts p
INNER JOIN categories c on p.ProductID = c.SomeID
WHERE p.SomeID = #ID
You can use option with WHILE loop and BREAK/CONTINUE keywords
CREATE PROC dbo.yourProcName
#SomeID int
AS
BEGIN
IF OBJECT_ID('tempdb.dbo.#resultTable') IS NOT NULL DROP TABLE dbo.#resultTable
CREATE TABLE dbo.#resultTable
(Col1 int, Col2 int)
DECLARE #ProductID int = 0
WHILE(1=1)
BEGIN
SELECT #ProductID = MIN(ProductID)
FROM ListOfProducts
WHERE SomeID = #SomeID AND ProductID > #ProductID
IF #ProductID IS NULL
BREAK
ELSE
INSERT dbo.#resultTable
SELECT Col1, Col2
FROM dbo.yourSearchTable
WHERE ProductID = #ProductID
CONTINUE
END
SELECT *
FROM dbo.#resultTable
END
Demo on SQLFiddle

Optimizing T-SQL Insert -- temptable, CTEs, WHILE loops

I have a situation where I need to process and finally insert 1000s of records from a temptable into a database table. Before each insert, I need to ensure that conditions are met and right after each insert, I need to update a second table in my database. My problem is that currently, it takes approximately 25 minutes to run the query and I would like to drastically cut that time so my application can be more responsive. How can I go about doing this please?
DECLARE #rowcounter as INTEGER
CREATE TABLE #temporary_phonetable
(
rownumber int not null identity(1,1),
record_no BIGINT,
phone_name BIGINT,
phone_number Varchar(25) not null ,
responsemessage Varchar(200) not null ,
messagepriority Varchar(14) not null ,
phone_id BIGINT,
AD_show BIGINT,
power_show Varchar(400),
service_provider VARCHAR(30),
Phone_flag VARCHAR(30),
questionMessage BIGINT,
PRIMARY KEY (phone_id, phone_number, rownumber)
,UNIQUE (questionMessage, record_no, rownumber)
)
--GET PHONE DATA
--if phone numbers are sent in from the client, then we want to process those instead
IF ( ( ( #listofphones IS NULL OR LEN(#listofphones) <1) AND LEN(#peoplegroups) >0) )
BEGIN
--NO PHONENUMBER BUT THERE ARE GROUPS AVAILABLE
INSERT INTO #temporary_phonetable(phone_name, phone_number, messagepriority, phone_id, AD_show, power_show, responsemessage)
SELECT n.phone_name, n.phone_number,u.messagepriority, n.phone_id , u.AD_show, u.power_show , CASE #includegreetings WHEN 1 THEN LTRIM(RTRIM(phone_name)) + #responsemessages
ELSE #responsemessages END as text_message
FROM user u WITH(NOLOCK)
INNER JOIN Phonenumbers n WITH(NOLOCK) ON n.user_no = u.user_no
INNER JOIN PeopleGroupRelations g ON g.phone_id=n.phone_id
INNER JOIN ( Select items FROM Split(#peoplegroups, #listofphonesdelimiter)) gg ON g.group_no = gg.items
WHERE n.user_no=#userid
AND n.status=''active''
SET #rowcounter = ##ROWCOUNT
END
ELSE IF ( LEN(#listofphones) >1 AND LEN(#peoplegroups) >0)
BEGIN
--PHONENUMBER AND GROUPS
INSERT INTO #temporary_phonetable(phone_name, phone_number, messagepriority, phone_id, AD_show, power_show, responsemessage)
SELECT n.phone_name, n.phone_number,u.messagepriority, n.phone_id , u.AD_show, u.power_show , CASE #includegreetings WHEN 1 THEN LTRIM(RTRIM(phone_name)) + #responsemessages
ELSE #responsemessages END as text_message
FROM Split(#listofphones, ''|'') s
INNER JOIN PhoneNumbers n WITH(NOLOCK) ON n.phone_number = s.items
INNER JOIN User u WITH(NOLOCK) ON n.user_no =u.user_no
INNER JOIN PeoplegroupRelations g ON g.phone_id=n.phone_id
INNER JOIN ( Select items FROM Split(#peoplegroups, #listofphonesdelimiter)) gg ON g.group_no = gg.items
WHERE n.user_no=#userid
AND n.status=''active''
SET #rowcounter = ##ROWCOUNT
END
ELSE IF ( LEN(#listofphones) >1 AND LEN(#peoplegroups) >0)
BEGIN
--PHONENUMBER AND NO GROUPS
INSERT INTO #temporary_phonetable(phone_name, phone_number, messagepriority, phone_id, AD_show, power_show, responsemessage)
SELECT n.phone_name, n.phone_number,u.messagepriority, n.phone_id , u.AD_show, u.power_show , CASE #includegreetings WHEN 1 THEN LTRIM(RTRIM(phone_name)) + #responsemessages
ELSE #responsemessages END as text_message
FROM Split(#listofphones, ''|'') s
INNER JOIN PhoneNumbers n WITH(NOLOCK) ON n.phone_number = s.items
INNER JOIN User u WITH(NOLOCK) ON n.user_no =u.user_no
INNER JOIN PeopleGroupRelations g ON g.phone_id=n.phone_id
INNER JOIN ( Select items FROM Split(#peoplegroups, #listofphonesdelimiter)) gg ON g.group_no = gg.items
WHERE n.user_no=#userid
AND n.status=''active''
SET #rowcounter = ##ROWCOUNT
END
ELSE
BEGIN
-- NO PHONENUMBER NO GROUP --- IE. SEND TO ALL PHONE NUMBERS
INSERT INTO #temporary_phonetable(phone_name, phone_number, messagepriority, phone_id, AD_show, power_show,responsemessage)
SELECT n.phone_name, n.phone_number,u.messagepriority, n.phone_id , u.AD_show, u.power_show , CASE #includegreetings WHEN 1 THEN LTRIM(RTRIM(phone_name)) + #responsemessages
ELSE #responsemessages END as text_message
FROM User u
INNER JOIN PhoneNumbers n ON n.user_no = u.user_no
WHERE
n.status=''active''
AND n.user_no=#userid
SET #rowcounter = ##ROWCOUNT
END
IF( #rowcounter>0)
BEGIN
DECLARE #service_provider as Varchar(30)
DECLARE #PhoneType as Varchar(30)
IF (LOWER(RTRIM(LTRIM(#sendresponseswhen))) ='now')
BEGIN
SET #dateresponsessent = GETDATE()
END
DECLARE #rownumber int
DECLARE #power_show BIT
DECLARE #AD_show BIT
set #rownumber = 0
WHILE #rownumber < #rowcounter
BEGIN
set #rownumber = #rownumber + 1
-- THE VARIABLES
DECLARE #record_no as BIGINT
DECLARE #phone_name VARCHAR(30)
DECLARE #messagepriority as INTEGER
DECLARE #phone_number VARCHAR(30)
DECLARE #phone_id BIGINT
DECLARE #questionMessage BIGINT
SELECT
#phone_name = n.phone_name, #phone_number =n.phone_number, #messagepriority =n.messagepriority, #phone_id=n.phone_id ,
#AD_show=n.AD_show, #power_show=n.power_show
FROM
#temporary_phonetable n WITH(NOLOCK)
WHERE n.rownumber = #rownumber
SET #record_no = AddMessageToQueue(#phone_number, #responsemessages, #dateresponsessent, #savednames, #userid, un.messagepriority, #responsetype,
un.AD_show, un.power_show, #service_provider, #PhoneType)
If(#questionid > 0)
BEGIN
SET #questionMessage = AddQuestionMessage(#questionid,#phone_id, #record_no, DATEADD(d, 30, GETDATE()) )
END
UPDATE #temporary_phonetable SET record_no = #record_no, questionMessage=#questionMessage WHERE phone_number = #phone_number AND rownumber = #rownumber
END
IF( #power_show >0)
BEGIN
SET #responsemessages = #responsemessages + dbo.returnPoweredBy()
END
IF( #AD_show > 0)
BEGIN
SELECT #responsemessages = #responsemessages + CASE
WHEN (LEN(#responsemessages) + 14)< 160 THEN dbo.returnAD(#responsemessages)
ELSE '''' END
END
RETURN #rowcounter
END
I believe this is the place where the bulk of the issue resides.
WHILE #rownumber < #rowcounter
BEGIN
set #rownumber = #rownumber + 1
-- THE VARIABLES
DECLARE #record_no as BIGINT
DECLARE #phone_name VARCHAR(30)
DECLARE #messagepriority as INTEGER
DECLARE #phone_number VARCHAR(30)
DECLARE #phone_id BIGINT
DECLARE #questionMessage BIGINT
SELECT
#phone_name = n.phone_name, #phone_number =n.phone_number, #messagepriority =n.messagepriority, #phone_id=n.phone_id ,
#AD_show=n.AD_show, #power_show=n.power_show
FROM
#temporary_phonetable n WITH(NOLOCK)
WHERE n.rownumber = #rownumber
SET #record_no = AddMessageToQueue(#phone_number, #responsemessages, #dateresponsessent, #savednames, #userid, un.messagepriority, #responsetype,
un.AD_show, un.power_show, #service_provider, #PhoneType)
If(#questionid > 0)
BEGIN
SET #questionMessage = AddQuestionMessage(#questionid,#phone_id, #record_no, DATEADD(d, 30, GETDATE()) )
END
UPDATE #temporary_phonetable SET record_no = #record_no, questionMessage=#questionMessage WHERE phone_number = #phone_number AND rownumber = #rownumber
END
Add a unique constraint to rownumber in your temp table. Rewrite the WHILE as a CTE. Use APPLY to call the functions.
You also might want to consider using a table variable rather than a temporary table. You won't be writing to the tempdb and as table variables are created in memory they're faster.
This article has a nice comparison.