Continuing on with my same SQL project, I am trying to determine if it is possible to increment a count in a stored procedure. For each row in this particular SELECT statement, I want it to be able to count and then reflect the new number for each row. I am wondering if it is possible and, if so, what would be the best way to approach it. Thanks! (My code, thus far, is below)
CREATE PROCEDURE APInterface
@CurrentRun integer
AS
SET NOCOUNT ON
DECLARE @PayNumber integer
SET @PayNumber = 0
CREATE Table #tmp(Outline char(175), RecID int identity)
INSERT #tmp(Outline)
SELECT --Outline =
CONVERT(char(2),'10')+
CONVERT(char(10),'')+
SUBSTRING(CONVERT(char(10),GETDATE(),102),6,2)+
SUBSTRING(CONVERT(char(10),GETDATE(),102),9,2)+
CONVERT(char(4),YEAR(GETDATE()))+
CONVERT(char(2),'XR')+
CONVERT(char(158),'')
--UNION ALL
INSERT #tmp(Outline)
SELECT --Outline =
CONVERT(char(2),'20')+
'0' + crPayerCompany +
-- This is where I am trying to put this, but not sure how best to do it if it's possible.
-- WHILE @@FETCH_STATUS = 0
-- BEGIN
-- --RIGHT(CONVERT(char(5),'00000') +
-- CONVERT(char(6),@PayNumber + 1) +
-- END
CONVERT(char(6),'') + --This needs to be changed to reflect the actual Pay Number that we will be using. Probably needs a temp table.
SUBSTRING(CONVERT(char(10),GETDATE(),102),6,2)+
SUBSTRING(CONVERT(char(10),GETDATE(),102),9,2)+
CONVERT(char(4),YEAR(GETDATE()))+
CONVERT(char(2),'XR')+
CONVERT(char(2),'') +
CASE
WHEN crCheckAmount >= 0 then CONVERT(char(1),1)
ELSE
CONVERT(char(1),2)
END +
CASE
WHEN Vendor_ID is null then CONVERT(char(9),'000000000')
ELSE
CAST(REPLICATE('0',9) + Vendor_ID as varchar(15))
END
FROM L_CheckRequestChecks JOIN L_CheckRequest on L_CheckRequestChecks.crCheckRunSerial = L_CheckRequest.crCheckRunSerial
JOIN L_Address on Lease_ID = crPropertyID
WHERE L_CheckRequest.crCheckRunSerial = @CurrentRun and Address_Type = 'Payment' and Vendor_ID = crVendorID
-- left(Company_Name,10) = left(crCompany,10) and left(City,7) = left(crCity,7)
ORDER BY L_CheckRequestChecks.crCheckSerial
SELECT Outline FROM #tmp
Order By RecID Insanity is merely a state of mind while crazy people have a mind of their own.
CREATE PROCEDURE APInterface
@CurrentRun integer
AS
SET NOCOUNT ON
DECLARE @PayNumber integer
SET @PayNumber = 0
CREATE Table #tmp(Outline char(175), RecID int identity)
INSERT #tmp(Outline)
SELECT --Outline =
CONVERT(char(2),'10')+
CONVERT(char(10),'')+
SUBSTRING(CONVERT(char(10),GETDATE(),102),6,2)+
SUBSTRING(CONVERT(char(10),GETDATE(),102),9,2)+
CONVERT(char(4),YEAR(GETDATE()))+
CONVERT(char(2),'XR')+
CONVERT(char(158),'')
--UNION ALL
INSERT #tmp(Outline)
SELECT --Outline =
CONVERT(char(2),'20')+
'0' + crPayerCompany +
-- This is where I am trying to put this, but not sure how best to do it if it's possible.
-- WHILE @@FETCH_STATUS = 0
-- BEGIN
-- --RIGHT(CONVERT(char(5),'00000') +
-- CONVERT(char(6),@PayNumber + 1) +
-- END
CONVERT(char(6),'') + --This needs to be changed to reflect the actual Pay Number that we will be using. Probably needs a temp table.
SUBSTRING(CONVERT(char(10),GETDATE(),102),6,2)+
SUBSTRING(CONVERT(char(10),GETDATE(),102),9,2)+
CONVERT(char(4),YEAR(GETDATE()))+
CONVERT(char(2),'XR')+
CONVERT(char(2),'') +
CASE
WHEN crCheckAmount >= 0 then CONVERT(char(1),1)
ELSE
CONVERT(char(1),2)
END +
CASE
WHEN Vendor_ID is null then CONVERT(char(9),'000000000')
ELSE
CAST(REPLICATE('0',9) + Vendor_ID as varchar(15))
END
FROM L_CheckRequestChecks JOIN L_CheckRequest on L_CheckRequestChecks.crCheckRunSerial = L_CheckRequest.crCheckRunSerial
JOIN L_Address on Lease_ID = crPropertyID
WHERE L_CheckRequest.crCheckRunSerial = @CurrentRun and Address_Type = 'Payment' and Vendor_ID = crVendorID
-- left(Company_Name,10) = left(crCompany,10) and left(City,7) = left(crCity,7)
ORDER BY L_CheckRequestChecks.crCheckSerial
SELECT Outline FROM #tmp
Order By RecID Insanity is merely a state of mind while crazy people have a mind of their own.