Solved

dynamic field name

Posted on 2002-03-17
5
208 Views
Last Modified: 2008-02-20
hi experts,

i heve the following code:

declare cursor_me cursor for select a_fielname from a_table
open cursor_me
fetch next from cursor_me into @my_var1

is there a way that based of some logic, it will tell me which field in the table that i should select?

i try to do the following but it did not give me the correct value

declare cursor_me cursor for select @my_var2 a_table
open cursor_me
fetch next from cursor_me into @my_var1

please help me out. thanks in advance!

0
Comment
Question by:megatshamsul
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
unless you can avoid the cursor, you must put everything into dynamic SQL...

CHeers
0
 

Author Comment

by:megatshamsul
Comment Utility
how?

some example may be.. thanks!
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
Comment Utility
cursor in dynamic SQL:

declare @sql nvarchar(1000)
declare @myvar varchar(100)

set @sql = N'declare cursor_me cursor for select cast(' + @my_var2 + 'as varchar(100)) from a_table
open cursor_me
fetch next from cursor_me into @my_var1
close cursor_me
deallocate cursor_me'

exec sp_executesql @sql, N'@my_var1 varchar(100) OUTPUT', @myvar OUTPUT

select @myvar

Cheers
0
 

Expert Comment

by:CleanupPing
Comment Utility
megatshamsul:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now