SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Concatenate a field name variable in a loop
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Manjate
Starting Member

3 Posts

Posted - 11/13/2012 :  20:51:37  Show Profile  Reply with Quote
I have a table with the following fields:

Code Varhar(7)
EXIST1 Float
EXIST2 Float
Exist3 Float
Exist4 Float
...
Exist9 Float

I WANT UPDATE FIELDS BY USING A LOOP.
SOMETHING LIKE:

WHILE N<=9
UPDATE MYtABLE SET EXIST+'N'= EXIST+'N' + nnn.nn
N=N+1
LOOP

How can I Concatenate EXIST and 'N' so that I can have
the name of fields?

khtan
In (Som, Ni, Yak)

Singapore
17635 Posts

Posted - 11/13/2012 :  20:54:32  Show Profile  Reply with Quote
you can't do that. What is this for ?


KH
Time is always against us

Go to Top of Page

Manjate
Starting Member

3 Posts

Posted - 11/13/2012 :  23:29:04  Show Profile  Reply with Quote
IN A table1 I WANT TO BUILD A TRRIGGER THAT WILL UPDATE ONE FIELD OF OTHER table2
where table2 has Fields:
Code Varhar(7)
EXIST1 Float
EXIST2 Float
Exist3 Float
Exist4 Float

table1 updates one field of table2 depending on case 1,2,3,...9
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17635 Posts

Posted - 11/13/2012 :  23:47:42  Show Profile  Reply with Quote
You should consider normalize your table. It will make such things much easier to handle.

Other wise, you might need complex case when statement to handle the updating logic.

Another alternative is to use Dynamic SQL. But that does not look pretty also.




KH
Time is always against us

Go to Top of Page

Manjate
Starting Member

3 Posts

Posted - 11/14/2012 :  12:10:43  Show Profile  Reply with Quote
i have done, by building this function



declare @code as nvarchar(15)
set @code='003'
declare @Arm int
declare @Result float
set @arm=1
Declare @cmd as varchar(2000)
set @cmd = 'DECLARE RegCur CURSOR for Select exist'+cast(@arm as nvarchar(1)) +' from tb201 where codigo='''+@code +''''
EXEC (@cmd)
OPEN RegCur;
DECLARE @exist float
FETCH NEXT FROM RegCur INTO @exist;
set @total= cast(@exist as Varchar(11))
CLOSE RegCur
DEALLOCATE RegCur;
print @Result
GO
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000