quote: Originally posted by visakh16 why is column name changing at runtime? can you elaborate your business scenario? you might be able to use a static query to achieve intended result
This is the full procedure modified without cursor.ALTER PROCEDURE [dbo].[AU_MyPut] @campo AS VARCHAR(50), @iter AS INT, @Indice AS VARCHAR(10), @NewVal AS VARCHAR(80)=''ASBEGINSET NOCOUNT ON;DECLARE @len AS int,@Crd AS VARCHAR(50), @In AS int, @to AS intDECLARE @e AS VARCHAR(150), @sql AS VARCHAR(500)DECLARE @tab AS TABLE(E VARCHAR(150))SELECT @len=[len], @Crd=Crd, @In=Inizio, @to=[to] FROM AudipressDS.dbo.Format WHERE Campo=@campo AND n=@iterIF @Crd LIKE 'D%'BEGIN SET @Crd=SUBSTRING(@Crd,2,10)ENDIF CHARINDEX('''',@Newval)>0BEGIN SET @NewVal=REPLACE(@NewVal,'''','''''')END--*****Tolto cursore in data 18-06-2012 Michele****SET @sql = 'SELECT D.[' + @crd + '] FROM AudipressDS.dbo.Datfiles D WHERE Indice=''' + @indice +''''INSERT @tab exec (@sql)SELECT @e=E FROM @tab--***************************************IF LEN(@e)<@toBEGIN SET @e=@e +REPLICATE(' ',@to-LEN(@e))ENDELSE IF ISNULL(@e,'0')='0'BEGIN SET @e=REPLICATE(' ',@to)ENDSET @e= STUFF(@e,@in-7,@len,RTRIM(LEFT(@NewVal,@len))+REPLICATE(' ',@len-LEN(RTRIM(LEFT(@NewVal,@len)))))--Aggiorno il campo @crd della tabella datfiles con la nuova stringaSET @sql='UPDATE AudipressDS.dbo.Datfiles SET [' + @crd + ']=''' + @e + ''' WHERE Indice=''' + @indice +''''EXECUTE (@sql)END It is used to punch a specific newValue for a variable in the correct field (@Crd - which represent a row of the original datafile) for a specific record(identified by @Indice) using a format table which contain the datamap of the file.@campo and @iter are the name and iteration of that variable.Do you think I can find a better solution?Michele |