• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

Variable in Select Stmt

Hello Experts,

How can I used a variable for the column name in the select statement?

Thank...
declare	  @roll_id				int	
		, @field_to_correct_value	varchar(100)
		, @roll_id_number	int
		, @field_to_correct	varchar(100)
 
 
select @roll_id 			= (select max(roll_id) from hcad..rolls_diary)	
 
select @field_to_correct_value = 'owner_name_1'  --this is a column name
select @roll_id_number = @roll_id
 
select @roll_id_number, strap, '@field_to_correct', owner_name_1, @field_to_correct_value
  from history.dbo.pcr_history

Open in new window

0
thewayne73
Asked:
thewayne73
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
no, unless you used dynamic sql.
0
 
Raja Jegan RSQL Server DBA & ArchitectCommented:
Just replace this part given below:

select @roll_id_number, strap, '@field_to_correct', owner_name_1, @field_to_correct_value
  from history.dbo.pcr_history

select @roll_id_number as roll_id_number, strap, @field_to_correct as field_to_correct, owner_name_1, @field_to_correct_value
  from history.dbo.pcr_history
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now