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)=''
AS
BEGIN
SET NOCOUNT ON;
DECLARE @len AS int,@Crd AS VARCHAR(50), @In AS int, @to AS int
DECLARE @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=@iter
IF @Crd LIKE 'D%'
BEGIN
SET @Crd=SUBSTRING(@Crd,2,10)
END
IF CHARINDEX('''',@Newval)>0
BEGIN
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)<@to
BEGIN
SET @e=@e +REPLICATE(' ',@to-LEN(@e))
END
ELSE IF ISNULL(@e,'0')='0'
BEGIN
SET @e=REPLICATE(' ',@to)
END
SET @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 stringa
SET @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 |