Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 23rd, 2007, 09:32 PM
Authorized User
 
Join Date: Oct 2006
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default Using variable as fieldname in SELECT statement

Hi,

Is there a ways to use a variable as fieldname using SQL statement?

Exampl:

  Instead of :

SELECT empname FROM emptable


   I would like to use a varible for the fieldname. the varialbe contains the fieldname i want to select i.e.:

SELECT myvariable FROM emptable

   where myvariable contains a fieldname of the table emptable.

   I remember using dbase III+, this can be done using macro (&) character. Unfortunately I can't make it work in SQL 2000.

Thanks for your help.

 
Old April 24th, 2007, 12:13 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You can use dynamic SQL:
Code:
DECLARE @ColumnName SYSNAME;
SET @ColumnName = 'MyColumn';
DECLARE @SQL NVARCHAR(1000);
SET @SQL = 'SELECT ' + @ColumnName + ' FROM tblMyTable;';
EXEC(@SQL);
Alternatively, instead of using EXEC use sp_ExecuteSql which maybe better for performance.

See http://www.sqlteam.com/item.asp?ItemID=4619.

--

Joe (Microsoft MVP - XML)
 
Old April 24th, 2007, 02:23 AM
Authorized User
 
Join Date: Oct 2006
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply Joe. I tried the code and put it in a stored procedure and it worked. However, I cannot combine it with WHERE clause. I tried using parametarized stored proc and it returns an error. The stored proc goes like this:


ALTER PROCEDURE dbo.myStoredProc

      (

        @prmproject_id nvarchar(50),
       @prmfieldname nvarchar(50),
     @prmaverage nvarchar(50)
   )

    AS

DECLARE @mySQL nvarchar(1000)
DECLARE @columnname sysname

SET @columnname=@prmfieldname



 SET @mySQL = 'SELECT ' + @columnname + ' FROM CompletionRateAverage '

     EXECUTE(@mySQL)


This works fine.

But when I tried combining WHERE clause as in:

SET @mySQL = 'SELECT ' + @columnname + ' FROM CompletionRateAverage ' + 'WHERE project_id = @prmproject_id'

Must declare the variable '@project_id' error is returned.

When I tried to change the statement to:

SET @mySQL = 'SELECT ' + @columnname + ' FROM CompletionRateAverage ' + 'WHERE project_id ='+ @prmproject_id


Invalid Column Name <value of project_id> is returned. As if the value of the project_id field is treated as column name.

Please help... Thanks!



 
Old April 26th, 2007, 10:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Code:
SET @mySQL = 'SELECT ' + @columnname + ' FROM CompletionRateAverage ' + 'WHERE project_id = ' + @prmproject_id
-- Try a PRINT next to this and copy paste the result of PRINT in query analyser,
-- see if that works, else post here your observations.
-- EXECUTE(@mySQL)
Print @mySQL
Cheers.

_________________________
- Vijay G
Strive for Perfection





Similar Threads
Thread Thread Starter Forum Replies Last Post
select statement ??? RinoDM SQL Server 2000 7 June 19th, 2008 08:40 AM
select Statement gregalb SQL Server 2000 3 January 15th, 2008 12:00 AM
Select from another select statement to a repeater simsen ASP.NET 2.0 Professional 0 May 2nd, 2007 04:34 PM
select statement help RinoDM SQL Server 2000 2 January 16th, 2007 11:35 AM
Select Statement jmss66 Oracle 1 May 27th, 2004 02:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.