Home All Groups Group Topic Archive Search About

Column name as variable



Author
1 May 2007 4:47 PM
d.s.
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

Author
1 May 2007 4:54 PM
Hari Prasad
Hello,

You may need to use dynamic sql for this. Take a look into EXEC and
SP_EXECUTESQL in books online

Thanks
Hari

Show quoteHide quote
"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
>
Are all your drivers up to date? click for free checkup

Author
1 May 2007 5:21 PM
d.s.
Show quote Hide quote
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.

Bookmark and Share