Solved

dynamic field name

Posted on 2002-03-17
5
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 143

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 143

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

624 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