Solved

dynamic field name

Posted on 2002-03-17
5
212 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]
ID: 6876283
unless you can avoid the cursor, you must put everything into dynamic SQL...

CHeers
0
 

Author Comment

by:megatshamsul
ID: 6878814
how?

some example may be.. thanks!
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 6879334
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
ID: 9280760
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

776 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