|
sql
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
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. DS 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 >
Show quote
Hide quote
On May 1, 9:54 am, "Hari Prasad" <hari_prasa***@hotmail.com> wrote: Gracias. That looks promising.> 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 -
Other interesting topics
latest entry per distinct ID
Transaction Log getting bigger DBCC CLEANTABLE (SQL Server 2000) question printing members of server roles in 2005 Urgent help needed do delete a sub-string from long string in many records. Regarding how to compress the data file How can I pre-allocate file size of TempDB in SQL 2000? SET 'QUOTED_IDENTIFIER' error, in the Optimization Maintenance Plan How to pause database mirroring ? service pack4 |
|||||||||||||||||||||||