Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Replace cursors with table variables

Author  Topic 

mvandoni
Starting Member

12 Posts

Posted - 2012-06-18 : 10:11:54
Hi all,
I have a stored procedure which include this piece of code:
SET @sql='DECLARE dat_cursor CURSOR FOR SELECT D.[' + @crd + '] FROM AudipressDS.dbo.Datfiles D WHERE Indice=''' + @indice +''''
EXECUTE (@sql)
OPEN dat_cursor
FETCH NEXT FROM dat_cursor INTO @e
CLOSE dat_cursor
DEALLOCATE dat_cursor

Is it possible to avoid the use of cursors (which are cousing some lock problems) using table variables instead?
I'm trying it but it seem that the variable has to be declared inside the @sql otherwise it is not find by the EXECUTE statement.

Thanks
Michele

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-06-18 : 10:45:13
declare @sql varchar(1000)
select @sql = 'SELECT D.[' + @crd + '] FROM AudipressDS.dbo.Datfiles D WHERE Indice=''' + @indice +'''''
insert @e
exec (@sql)

Might have the quotes wrong

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

mvandoni
Starting Member

12 Posts

Posted - 2012-06-18 : 11:22:00
Thanks a lot!!!
After I realized that @e was a table variable it worked perfectly!!!

And you were right also on quotes
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-18 : 12:22:33
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

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

mvandoni
Starting Member

12 Posts

Posted - 2012-06-18 : 14:52:56
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
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-18 : 15:00:02
seems like you should be able to achieve this using UNPIVOT, but to confirm i need more info on exact scenario. can you post some data and explain what you're trying to achieve using code above out of it? i'm not able to understand what you're trying to achieve using code above

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

mvandoni
Starting Member

12 Posts

Posted - 2012-06-18 : 18:34:34
This is a bit complicate to explain in English.
Datfiles table contain data from a multirow datafile.
I have an index and N other fields which contain the full content of each line of a record, so in each field I have the content of a lot ov variables.
Format table tells me in which line/field each variable is stored and the start column and lenght for of it.
This function is called in another routine which has to change the values of some of these variables for each record.
For each call i know the ID of the record (@Indice), the name of the variable (@campo and @iter) and the new value it has to assume (@NewVal).

In any case, don't loose yuor mind on this; I just needed to substitute the cursor I used to search for variable information in Format table with something else to avoid any possible lock due to the contemporary use of this routine by more then one user.
Table variables should work properly.

Thanks
Michele
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-06-18 : 22:48:18
Cant make out much from explanation. I'm leaving this to you unless you give me some sample data to understand the scenario

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -