Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable for Column Alias

Status
Not open for further replies.

03Explorer

Technical User
Joined
Sep 13, 2005
Messages
304
Location
US
I am trying to make a variable driven code that the user enters criteria at the top of the SQL code and the output is custom driven.

Example:

Code:
DECLARE @StartDate DATETIME     	SET @StartDate = '2009/01/01 00:00:00'
DECLARE @EndDate DATETIME       	SET @EndDate   = '2009/05/20 23:59:59'
DECLARE @APPLICATION VARCHAR(3)		SET @APPLICATION = 'PRV'
DECLARE @ELEMENT1name VARCHAR(100) 	SET @ELEMENT1name = 'MemberID'
DECLARE @ELEMENT1 VARCHAR(100) 		SET @ELEMENT1 = 'PatID_ValidIDPrompt'
DECLARE @ELEMENT2name VARCHAR(100) 	SET @ELEMENT2name = 'Total_Calls'
DECLARE @ELEMENT2 VARCHAR(100) 		SET @ELEMENT2 = 'PatID_Silencer'

SELECT
  apname as @application,
  table2.elemname as @ELEMENT1name,
  table1.elemname as @ELEMENT2name,
  @ELEMENT1 / @ELEMENT2 as 'percentage'
FROM
 ...

This is a simplified snipit of the code I am trying to make more dynamic.
 
If you want to format your columns certain way (make the names of the fields to match your variables), then you should construct the whole query as one string and execute it using execute statement.
 
I am not certain on what you are saying... I want the column names to be resembling the field variables, but not as technical.

Can you share a simple example of what you are describing?
 
DECLARE @StartDate DATETIME SET @StartDate = '2009/01/01 00:00:00'
DECLARE @EndDate DATETIME SET @EndDate = '2009/05/20 23:59:59'
DECLARE @APPLICATION VARCHAR(3) SET @APPLICATION = 'PRV'
DECLARE @ELEMENT1name VARCHAR(100) SET @ELEMENT1name = 'MemberID'
DECLARE @ELEMENT1 VARCHAR(100) SET @ELEMENT1 = 'PatID_ValidIDPrompt'
DECLARE @ELEMENT2name VARCHAR(100) SET @ELEMENT2name = 'Total_Calls'
DECLARE @ELEMENT2 VARCHAR(100) SET @ELEMENT2 = 'PatID_Silencer'

declate @SqlStr varchar(4000)

SET @SqlStr = 'SELECT
apname as ' + @application + ', table2.elemname as ' + @ELEMENT1name + ', table1.elemname as ' + @ELEMENT2name +
',' + @ELEMENT1 +'/'+ @ELEMENT2 + 'as '''percentage''' FROM '...
print @SqlStr -- to test
execute(@SqlStr)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top