Saturday, February 25, 2012

Column name as variable

Does anyone know if I can use a variable for a column name in a query?
I'm wanting to reuse some code that updates a table, but, depending on
conditions, I want it to update a different column.
Something like:
declare @.col_name as '
Select @.col_name = "last_week"
update tblTest set @.col_name = blah blah blah
Perspiring minds want to know.
DSHello,
You may need to use dynamic sql for this. Take a look into EXEC and
SP_EXECUTESQL in books online
Thanks
Hari
"d.s." <nodamnspamok@.yahoo.com> wrote in message
news:1178038066.444666.41510@.y5g2000hsa.googlegroups.com...
> Does anyone know if I can use a variable for a column name in a query?
> I'm wanting to reuse some code that updates a table, but, depending on
> conditions, I want it to update a different column.
> Something like:
> declare @.col_name as '
> Select @.col_name = "last_week"
> update tblTest set @.col_name = blah blah blah
> Perspiring minds want to know.
> DS
>|||On May 1, 9:54 am, "Hari Prasad" <hari_prasa...@.hotmail.com> wrote:
> Hello,
> You may need to use dynamic sql for this. Take a look into EXEC and
> SP_EXECUTESQL in books online
> Thanks
> Hari
> "d.s." <nodamnspa...@.yahoo.com> wrote in message
> news:1178038066.444666.41510@.y5g2000hsa.googlegroups.com...
>
> > Does anyone know if I can use a variable for a column name in a query?
> > I'm wanting to reuse some code that updates a table, but, depending on
> > conditions, I want it to update a different column.
> > Something like:
> > declare @.col_name as '
> > Select @.col_name = "last_week"
> > update tblTest set @.col_name = blah blah blah
> > Perspiring minds want to know.
> > DS- Hide quoted text -
> - Show quoted text -
Gracias. That looks promising.

No comments:

Post a Comment