Please see the SQL code below:
declare @Classification as varchar(5)
set @Classification =''
declare @ClassificationSQL as nvarchar(4000)
set @ClassificationSQL=''
declare @cnt int
declare @counts int
DECLARE NicheDeletionOffenderCursor CURSOR FOR
select classification from dbnicheoffenderclassificationlookup
Open NicheDeletionOffenderCursor
FETCH NEXT FROM NicheDeletionOffenderCursor INTO @Classification
WHILE @@FETCH_STATUS = 0
BEGIN
If @ClassificationSQL=''
set @ClassificationSQL='classification like ' + char(39) + '%' + @Classification + '%' + char(39)
else
set @ClassificationSQL=@ClassificationSQL + ' OR classification like ' + char(39) + '%' + @Classification + '%' + char(39)
FETCH NEXT FROM NicheDeletionOffenderCursor INTO @Classification
END
CLOSE NicheDeletionOffenderCursor
DEALLOCATE NicheDeletionOffenderCursor
SET @ClassificationSQL = 'select count(*) as cnt from person where id=903 and (' + @ClassificationSQL + ')'
EXECUTE sp_executesql @ClassificationSQL, N'@cnt int OUTPUT', @cnt=@Counts OUTPUT
How do I assign the count output from @ClassificationSQL to a variable to use in the next part of the TSQL?
SELECT @cnt = count(*) from person...
all kinds of things wrong here. Why notdbo.person
? Why@cnt
and@counts
? Why does the case differ? Why noN
prefix on the 2nd last line?