Hi,I'm having problems understanding the use of variables in sp_executesql statements. I have the following code:DECLARE @Variable varchar(30);DECLARE @SQLString nvarchar(500);DECLARE @ParmDefinition nvarchar(500);DECLARE @max_title varchar(30);SET @Variable = 'DESCRIPTION';SET @SQLString = N'SELECT @max_titleOUT = @Cols FROM TBL_BACKUPTEST'SET @ParmDefinition = N'@Cols nvarchar(100),@max_titleOUT varchar(30) OUTPUT';EXECUTE sp_executesql @SQLString, @ParmDefinition, @Cols=@Variable, @max_titleOut=@max_title OUTPUT;PRINT @max_title;
the output of this is the column name (DESCRIPTION) but what i want is the value of the column.when i take out the variable @Cols i get the value:DECLARE @Variable varchar(30);DECLARE @SQLString nvarchar(500);DECLARE @ParmDefinition nvarchar(500);DECLARE @max_title varchar(30);SET @Variable = 'DESCRIPTION';SET @SQLString = N'SELECT @max_titleOUT = DESCRIPTION FROM TBL_BACKUPTEST'SET @ParmDefinition = N'@Cols nvarchar(100),@max_titleOUT varchar(30) OUTPUT';EXECUTE sp_executesql @SQLString, @ParmDefinition, @Cols=@Variable, @max_titleOut=@max_title OUTPUT;PRINT @max_title;
I'm not sure why using the variable @Cols does not produce the same result. It would be great if somebody could help me with this as i am really getting nowhere.Thanksrebecca