Error Message:
Must declare the scalar variable
Root Cause:
1) Collation Settings are different from default, so the variable you have declared and variable you have used is different.
For Example you have declared @Serviceaccount but while using in T-SQL you have used as Select @serviceaccount so SQL Server will throw this error because my collation settings is Latin1_General_CS_AS_KS_WS but the default collation is SQL_Latin1_General_CP1_CI_AS.
2) The variables are declared outside of dynamic SQL that you are executing and you are trying to use those variables inside Dynamic SQL statement.
For Example
Declare @SQL VarChar(2000),@var1 varchar(50),@var2 int,@total int
SELECT @SQL = 'select @var1 = name, @var2 = sum(age), @total = count(*)from dbo.'+@tableName+' where authority='+@authorityId+'group by name'
Exec (@SQL)
Solution:
1) Make the case of declared variable and used variabled the same.
2) Declare variables inside the scope of dynamic SQL.
No comments:
Post a Comment
Please do not spam!