Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

dynamic field name

Posted on 2002-03-17
5
Medium Priority
?
222 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 200 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

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
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
Viewers will learn how the fundamental information of how to create a table.

688 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