I'm trying to build an SQL string to execute via sp_executesql
but I can't seem to assign value to a declared variable within the passed in sql string.
So below is an example of my procedure
ALTER PROCEDURE [dbo].[selectRecords]
@psID INT --parameter passed in
AS
DECLARE @existingRecordCount INT=0
DECLARE @sql NVARCHAR(1000)
DECLARE @paramDefinitions NVARCHAR(1000)
SET @paramDefinitions=
'@psID INT,
@existingRecordCount INT OUTPUT'
SET @sql='
SELECT
@existingRecordCount=COUNT(Name)
FROM dbo.Asset_Log
WHERE ID=@psID
GROUP BY Name'
EXECUTE sp_executesql @sql, @paramDefinitions, @psID, @existingRecordCount
So, I'd expect my record count to be in the @existingRecordCount
variable but I receive the following error:
Incorrect syntax near 'OUPUT'.
Must declare the scalar variable "@existingRecordCount".
What am I doing wrong?
Thanks in advance. I'm using SQL Server 2008