logo

Warning: The forum is now for consultation only. Please use GitHub Discussions to post any questions or comments.


Welcome Guest ! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
smbrandonjr  
#1 Posted : Friday, May 26, 2017 12:22:33 AM(UTC)
smbrandonjr

Rank: Newbie

Groups: Registered
Joined: 5/25/2017(UTC)
Posts: 2
United States
Location: VA

Thanks: 1 times
I have a query that works in MSSQL but fails when I attempt to use to populate SealMasterTable. I am getting errors related to incorrect syntax near keywords.

Code:

SET NOCOUNT ON

DECLARE @vStartDate DATETIME = '2017-04-18 00:00:00', 
		@vEndDate DATETIME =  '2017-04-18 23:59:59', 
		@weekSec INTEGER = 604800,
		@vCompany INTEGER = 7,
		@vMaxAllowed INTEGER = 30, 
		@vCarcount INTEGER,
		@vOverageCounter INTEGER,
		@vY2K DATETIME = '2000-01-01 00:00:00',
		@vStartSeconds INTEGER,
		@vEndSeconds INTEGER,
		@vStartCount INTEGER,
		@vRowCounter INTEGER = 0;

-- DATETIME conversion--DATETIME VALUE in DB is TIME since 1/1/2000 @ 00:00
SET @vStartSeconds = DATEDIFF(second, @vY2K, @vStartDate);
PRINT 'Report start date/time: ' + CAST(@vStartDate AS VARCHAR)
SET @vEndSeconds = DATEDIFF(second, @vY2K, @vEndDate);
PRINT 'Report end date/time: ' + CAST (@vEndDate AS VARCHAR)
------------------------------------------------------------------------------

SET @vStartCount = 0;
SET @vCarCount = 0;
SET @vOverageCounter = 0;

--GET START COUNT
WITH CTE AS
(
SELECT ROW_NUMBER() OVER(PARTITION BY M_Trans.Badge ORDER BY M_Trans.[DateTime] DESC) AS 'Row #',
	   M_Trans.Badge,
	   [DateTime] AS 'Time Used',
	   RecType
FROM M_Trans
LEFT JOIN M_Subscribers ON M_Trans.Badge = M_Subscribers.Badge
GROUP BY M_Trans.Badge, ISLastVer, Deleted, M_Trans.CompanyID, [DateTime], RecType
HAVING ISLastVer = 1 AND Deleted = 0 AND M_Trans.CompanyID = @vCompany AND [DateTime] BETWEEN (@vStartSeconds-@weekSec) AND (@vStartSeconds)
)
SELECT @vStartCount = COUNT(Badge) FROM CTE WHERE [Row #] = 1 AND RecType = 51;

--SELECT CTE.Badge AS 'Card #', COUNT(CTE.Badge) FROM CTE WHERE [Row #] = 1 ORDER BY [Time Used] DESC

---@vCarCount = @vStartCount
---Iterate through each transaction from start datetime to end datetime and if 51, increment carcount, if 52, decrement carcount, if 0 do nothing
---If @vCarCount >= to @vMaxAllowed and if RecType = 51 then increment @vOverageCounter else do nothing (or add zero to @vOverageCounter)
---Return @vOverageCounter
PRINT 'Starting car count: ' + CAST(@vStartCount AS VARCHAR);

DECLARE @cBadge INTEGER, @cRecType INTEGER, @cDateTime INTEGER;
DECLARE parkEVENT_CURSOR CURSOR FOR 
	SELECT Badge, RecType, [DateTime] FROM M_Trans WHERE CompanyID = @vCompany AND [DateTime] BETWEEN @vStartSeconds AND @vEndSeconds ORDER BY [DateTime] ASC;

SET @vCarCount = @vStartCount;

OPEN parkEVENT_CURSOR

FETCH NEXT FROM parkEVENT_CURSOR INTO
	@cBadge, @cRecType, @cDateTime

WHILE @@FETCH_STATUS = 0
BEGIN
	IF (@cRecType = 51)
		BEGIN
		SET @vCarCount = @vCarCount + 1
		IF (@vCarcount >= @vMaxAllowed)
		BEGIN
			SET @vOverageCounter = @vOverageCounter + 1;
			PRINT 'Card used during overage: Card # ' + CAST(@cBadge AS VARCHAR) + ' used on ' + CAST((DATEADD(s, @cDateTime, @vY2K)) AS VARCHAR)
		END
		ELSE
			SET @vOverageCounter = @vOverageCounter + 0;
		END
	ELSE
		BEGIN
		IF (@cRecType = 52)
			SET @vCarCount = @vCarCount - 1;
		ELSE
			SET @vCarCount = @vCarCount + 0;
		END;
SET @vRowCounter = @vRowCounter + 1;
FETCH NEXT FROM parkEVENT_CURSOR INTO
	@cBadge, @cRecType, @cDateTime;
END;
CLOSE parkEVENT_CURSOR;
DEALLOCATE parkEVENT_CURSOR;
PRINT 'Ending car count: ' + CAST(@vCarCount AS VARCHAR)
PRINT 'Overage count: ' + CAST(@vOverageCounter AS VARCHAR)
PRINT 'Total number of access events: ' + CAST(@vRowCounter AS VARCHAR)



Thanks,

Mike

Edited by user Friday, May 26, 2017 3:32:31 PM(UTC)  | Reason: Not specified

epf  
#2 Posted : Monday, May 29, 2017 7:54:02 AM(UTC)
epf

Rank: Administration

Groups: Administrators
Joined: 12/20/2013(UTC)
Posts: 1,209
Switzerland

Thanks: 14 times
Was thanked: 206 time(s) in 199 post(s)
Yes, that is normal..
The SealMasterTable is built by doing a :
Code:
SELECT * FROM (your sql statement) a


In order to read the table catalog, to apply dynamic restrictions...and also to perform joins with other table.
So you SQL statement should return a query result.

Depending on you database, your query may work in your database engine but not for the SealMasterTable.

In you case you should write your code in a function returning the table wanted.
Search also in the forum: you can create Temporary tables using PreSQL Statement.

Edited by user Monday, May 29, 2017 9:07:17 AM(UTC)  | Reason: Not specified

thanks 1 user thanked epf for this useful post.
smbrandonjr on 5/31/2017(UTC)
smbrandonjr  
#3 Posted : Wednesday, May 31, 2017 12:30:44 PM(UTC)
smbrandonjr

Rank: Newbie

Groups: Registered
Joined: 5/25/2017(UTC)
Posts: 2
United States
Location: VA

Thanks: 1 times
Thank you for the info
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.