Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language

Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Sql Server Programming

Select with variable containing a Column name

Hi ,
I would like to use a column name in a variable as follows:
Declare @amount numeric(25,0)
Declare @ColumnName varchar(20)
Set @ColumnName= 'Amount1'

SELECT @amount = @ColumnName FROM Sale WHERE year=1998
---error: convertion from varchar to numeric

SELECT @amount = Amount1 FROM Sale WHERE year=1998
--- is running fine

How can I fix this syntax problem?
Any advice will be very much appreciated.


You are trying to write dynamic-SQL.  Which means that you must build a
complete string of the statement then execute it.  Indirection within T-SQL
statements is invalid syntax.

A popular reference for Dynamic SQL is:


"Truc H." <T@discussions.microsoft.com> wrote in message


Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc