question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to create stored procs

See original GitHub issue

System information:

  • Operating system (distribution) and version: Windows 10
  • DBeaver version: Version 21.2.0.202108310918
  • Additional extensions

Connection specification:

  • Database name and version: SQL Server
  • Driver name
  • Do you use tunnels or proxies (SSH, SOCKS, etc)? No

Describe the problem you’re observing:

Often run into problems when executing certain SQL. A common one seems to be related to creating SP’s or views. I get this error:

Error occurred during SQL script execution

Reason: SQL Error [102] [S0001]: Incorrect syntax near ‘;’.

Steps to reproduce, if exist:

Run this SQL. The problem is with this line: ;WITH EA AS

CREATE PROCEDURE [dbo].[procSubReport_ScreenPanelReplacementParts]
(
				@AreaID						INT,
				@EquipmentTypeID			INT,
				@multiFaultID				VARCHAR(4000),
				@ReplacementDateStart		DATETIME,
				@ReplacementDateEnd			DATETIME,
				@Interval					INT
)
AS

-- Round @ReplacementDateEnd to the 23:59:59 to include the whole day
SET @ReplacementDateEnd = CAST(CAST(@ReplacementDateEnd AS date) AS datetime) + '23:59:59' 

BEGIN

;WITH EA AS
(
	SELECT
		EA.EquipmentID,
		EA.EquipmentActionID,
		EA.Comments,
		CONVERT(DATETIME,EA.RemovalDate) RemovalDate,
		EA.Rotated
	FROM
		Equipment E
	INNER JOIN
	(
		SELECT
			PEA.EquipmentID,
			PEA.EquipmentActionID,
			PEA.Comments,
			MIN(PEA.[Remove]) RemovalDate,		
			MIN(PEA.Rotate) Rotated
		FROM
		(
			SELECT
				EA.EquipmentID,
				EA.EquipmentActionID,
				EA.Comments,
				STRING_AGG(EA.EquipmentActionDate,',') EquipmentActionDate,
				EAT.Name
			FROM
				EquipmentActions EA
			INNER JOIN EquipmentActionTypes EAT
				ON EA.EquipmentActionTypeID = EAT.EquipmentActionTypeID
				AND EAT.Name IN ('Remove','Rotate')	
			WHERE
				EA.IsDeleted = 0
				AND EAT.IsDeleted = 0
			GROUP BY
				EquipmentID,
				EA.EquipmentActionID,
				EA.Comments,
				EAT.Name
		) A
		PIVOT	(
			MIN(A.EquipmentActionDate)
			FOR A.[Name] IN ([Remove],[Rotate])
		) PEA
		GROUP BY
			PEA.EquipmentID,
			PEA.EquipmentActionID,
			PEA.Comments
	) EA ON E.EquipmentID = EA.EquipmentID
	WHERE
		E.AreaID = @AreaID
		AND E.IsDeleted=0
		AND E.EquipmentTypeID = @EquipmentTypeID
		AND CAST(EA.RemovalDate as datetime) BETWEEN			@ReplacementDateStart AND @ReplacementDateEnd
),
PC AS 
(
	SELECT			E.AreaID,
					E.SortOrder,
					EA.EquipmentActionID,
					PhotoImage = ISNULL(A.LargeThumbnail, A.Data),
					EA.Comments,
					PhotoCaption = EAL.Caption,
					EAL.LastSavedDateTime,
					DisplayOrder = ROW_NUMBER() OVER (ORDER BY EAL.LastSavedDateTime),
					RowGroup = FLOOR(( ROW_NUMBER() OVER (ORDER BY EAL.LastSavedDateTime) - 1) / 4),
					ColGroup = ROW_NUMBER() OVER (ORDER BY EAL.LastSavedDateTime) % 4
					
	FROM			Equipment E
	INNER JOIN		Equipment_ScreenPanel SP				ON	E.EquipmentID					= SP.EquipmentID
	LEFT OUTER JOIN	EA									ON	E.EquipmentID					= EA.EquipmentID
	LEFT OUTER JOIN EntitiesAttachmentsLink EAL			ON	EAL.EntityID					= EA.EquipmentActionID
	LEFT OUTER JOIN Attachments A						ON	EAL.AttachmentID				= A.AttachmentID
	
	WHERE			E.AreaID							=	@AreaID
	AND				E.EquipmentTypeID					=	@EquipmentTypeID
	AND				E.IsInstalled						=	0
	AND				EA.RemovalDate		BETWEEN			@ReplacementDateStart AND @ReplacementDateEnd
	AND				E.IsDeleted							=	0
	AND				EAL.EntityType						=	'EquipmentAction'
	AND				EAL.IsDeleted						=	0
	AND				A.IsDeleted							=	0
),
CTE AS 
(

	SELECT	E.SortOrder AS LinerID, 
			PC1.DisplayOrder 
	FROM Attachments A
	INNER JOIN (
				SELECT	PC.PhotoImage,
						CONCAT(
						'#',
						ROW_NUMBER() OVER (ORDER BY MIN(PC.SortOrder)),
						'- ',
						STUFF(
								(
									SELECT ', '+B.Comments
									FROM (
											SELECT	PC.PhotoImage,
													Comments, 
													MIN(PC.LastSavedDateTime) AS LastSavedDateTime
											FROM PC
											GROUP BY PhotoImage,Comments
									)B
									WHERE PC.PhotoImage=B.PhotoImage
									ORDER BY B.LastSavedDateTime
									FOR XML PATH(''), TYPE
								).value('.','VARCHAR(MAX)')
								,1,2,''
							)) AS Comments,
						DisplayOrder = ROW_NUMBER() OVER (ORDER BY MIN(PC.SortOrder)),
						RowGroup = FLOOR(( ROW_NUMBER() OVER (ORDER BY MIN(PC.SortOrder)) - 1) / 4),
						ColGroup = ROW_NUMBER() OVER (ORDER BY MIN(PC.SortOrder)) % 4
				FROM PC
				WHERE PhotoImage IS NOT NULL
				GROUP BY PhotoImage
	)PC1									ON	PC1.PhotoImage						=	ISNULL(A.LargeThumbnail, A.Data)
	INNER JOIN EntitiesAttachmentsLink EAL			ON	EAL.AttachmentID					=	A.AttachmentID
	INNER JOIN EquipmentActions EA					ON	EA.EquipmentActionID				=	EAL.EntityID
	INNER JOIN Equipment E							ON	E.EquipmentID						=	EA.EquipmentID
	WHERE A.EntityType = 'EquipmentAction'
)

SELECT			E.AreaID,
				ScreenPanelSortOrder				=	E.SortOrder,
				E.Name,
				ESP.RowLabel,
				ESP.ColumnLabel,
				ESP.Width,
				ESP.Length,
				PartNumber							=	SPG.Name,
				PanelGroup							=	SPG.Name,
				PanelGroupSortOrder					=	SPG.SortOrder,
				PanelGroupBackColour				=	SPG.BackColour,
				PanelGroupFontColour				=	SPG.FontColour,
				PanelType							=	SPT.Name,
				PanelMaterialType					=	SMT.Name,
				PanelManufacturer					=	SPM.Name,
				ESP.ApertureLength,
				ESP.ApertureWidth,
				ESP.Thickness,
				E.InstalledDate,
				RemovalDate = EA.RemovalDate,
				Rotated								=	EA.Rotated,
				RotatedBackColor					=	CASE 
															WHEN EA.Rotated IS NOT NULL THEN '#ffc0cb'
														END,
				RotatedFontColor					=	CASE 
															WHEN EA.Rotated IS NOT NULL THEN '#000000'
														END,
				ReasonForReplacement				=	RFR.Name,
				PhotoNumber							=	PhotoNum.DisplayOrder,
				KeyBackColour						=	CASE
															WHEN DATEDIFF(day, EA.RemovalDate, @ReplacementDateEnd) <= (@Interval * 7.5) THEN '#339933'
															WHEN DATEDIFF(day, EA.RemovalDate, @ReplacementDateEnd) > (@Interval * 7) AND DATEDIFF(day, EA.RemovalDate, @ReplacementDateEnd) <= (@Interval * 7 * 2) THEN '#6777f0'
															WHEN DATEDIFF(day, EA.RemovalDate, @ReplacementDateEnd) > (@Interval * 7 * 2) AND DATEDIFF(day, EA.RemovalDate, @ReplacementDateEnd) <= (@Interval * 7 * 3) THEN '#f9ff03'
															WHEN DATEDIFF(day, EA.RemovalDate, @ReplacementDateEnd) > (@Interval * 7 * 3) THEN '#14394d'
														END,
				KeyFontColour						=	CASE
															WHEN DATEDIFF(day, EA.RemovalDate, @ReplacementDateEnd) <= (@Interval * 7) THEN '#ffffff'
															WHEN DATEDIFF(day, EA.RemovalDate, @ReplacementDateEnd) > (@Interval * 7) AND DATEDIFF(day, EA.RemovalDate, @ReplacementDateEnd) <= (@Interval * 7 * 2) THEN '#ffffff'
															WHEN DATEDIFF(day, EA.RemovalDate, @ReplacementDateEnd) > (@Interval * 7 * 2) AND DATEDIFF(day, EA.RemovalDate, @ReplacementDateEnd) <= (@Interval * 7 * 3) THEN '#222222'
															WHEN DATEDIFF(day, EA.RemovalDate, @ReplacementDateEnd) > (@Interval * 7 * 3) THEN '#ffffff'
														END

        
FROM			Equipment E
INNER JOIN		Equipment_ScreenPanel ESP			ON	E.EquipmentID					= ESP.EquipmentID
INNER JOIN		vwScreenPanelTypes SPT				ON	ESP.ScreenPanelTypeID			= SPT.ScreenPanelTypeID

INNER JOIN		EA									ON	E.EquipmentID					= EA.EquipmentID

LEFT OUTER JOIN	vwScreenPanelMaterialTypes SMT		ON	ESP.ScreenPanelMaterialTypeID	= SMT.ScreenPanelMaterialTypeID
LEFT OUTER JOIN	vwScreenPanelManufacturers SPM		ON	ESP.ScreenPanelManufacturerID	= SPM.ScreenPanelManufacturerID
LEFT OUTER JOIN	vwScreenPanels SPG					ON	ESP.ScreenPanelID				= SPG.ScreenPanelID
LEFT OUTER JOIN (
					SELECT 
						CTE.LinerID,
						STUFF(
							(
								SELECT ', '+ CONVERT(NVARCHAR(5),B.DisplayOrder)
								FROM (
										SELECT	CTE.LinerID,CTE.DisplayOrder
										FROM CTE
										GROUP BY LinerID,DisplayOrder
								)B
								WHERE CTE.LinerID =B.LinerID
								ORDER BY B.DisplayOrder
								FOR XML PATH(''), TYPE
							).value('.','VARCHAR(MAX)')
							,1,2,''
						) AS DisplayOrder
					FROM CTE
					GROUP BY CTE.LinerID
				) PhotoNum							ON	PhotoNum.LinerID				=	E.SortOrder
LEFT OUTER JOIN (
					SELECT F.FaultID, EA.EquipmentActionID,F.Name
					FROM EquipmentActions EA
					INNER JOIN Faults F ON EA.FaultID=F.FaultID
				)RFR								ON	RFR.EquipmentActionID			=	EA.EquipmentActionID
WHERE			E.AreaID							=	@AreaID
AND				E.EquipmentTypeID					=	@EquipmentTypeID
AND				E.IsDeleted							=	0
AND				(
					RFR.FaultID IN	(
										SELECT	Value
										FROM	STRING_SPLIT(@multiFaultID, ',')
									) OR  
					@multiFaultID IS NULL
				)

GROUP BY		E.AreaID,
				E.SortOrder,
				E.Name,
				ESP.RowLabel,
				ESP.ColumnLabel,
				ESP.Width,
				ESP.Length,				
				SPG.Name,
				SPG.SortOrder,
				SPG.BackColour,
				SPG.FontColour,
				SPT.Name,
				SMT.Name,
				SPM.Name,
				ESP.ApertureLength,
				ESP.ApertureWidth,
				ESP.Thickness,
				E.InstalledDate,
				EA.RemovalDate,
				EA.Rotated,
				RFR.Name,
				PhotoNum.DisplayOrder
END

Include any warning/errors/backtraces from the logs

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
tati-krucommented, Sep 8, 2021

Hello @williambuchanan2,

You can select your entire script using <kbd>Ctrl+A</kbd> and then execute it using <kbd>Ctrl+Enter</kbd>.

0reactions
williambuchanan2commented, Sep 9, 2021

@williambuchanan2 Please be aware that with this option enabled the semicolon delimeter will be ignored in all of the scripts which can cause other problems, so you need to use this feature with caution.

Ok, thanks for the warning.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to create a stored procedure in SSMS - Stack Overflow
I am trying to create a stored procedure in SSMS to export the query result to CSV. But I am getting below error...
Read more >
Unable to create Stored Procedures. · Discussion #252 - GitHub
In 'Default' branch, not 'Production' branch, I am unable to create a Stored procedure on a newly created, empty database.
Read more >
Unable to create stored procedure with error - Liquibase Forum
I generated dbchangelog from production database and intent to create a local db with that schema. However, there's some stored procedures ...
Read more >
Unable to create stored procedure to access excel files in SQL ...
I believe the SQL Service account is attempting to access the file. For trouble-shooting purposes on a system for which you can test...
Read more >
CREATE PROCEDURE (Transact-SQL) - Microsoft Learn
Creates a Transact-SQL or common language runtime (CLR) stored procedure in SQL Server, Azure SQL Database, and Analytics Platform System ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found