Solved

Adding a field name to dynamic SQL

Posted on 2007-12-03
2
800 Views
Last Modified: 2010-04-21
In the code below, I have a dynamic SQL statement that works (thanks to imitchie). The result set returns a select statement using  

AS ''Promo' + Cast(PromoID as Varchar)

I need to change that to dynamically read the PromoName field that's in the KScoop_Promo table (aka, change Promo1 to Birthday Club, Promo2 to whatever, based on the PromoName in the table).

Help is greatly appreciated.
declare @dsql varchar(max)
declare @SELECT varchar(max)
declare @LEFTJOINS varchar(max)
 
DECLARE	@ActiveCode varchar(2)
DECLARE	@ActiveCode2 varchar(2)
DECLARE	@StoreActive varchar(2)
 
SET @ActiveCode = 1
SET	@ActiveCode2 = 0
 
 
	SELECT  @SELECT =
			coalesce(@SELECT + ',', '') +  -- first one does not need comma
			' ISNULL(Promo' + Cast(PromoID as Varchar) + '.PromoFlag, 0) AS ''Promo' +
			Cast(PromoID as Varchar) + '''', 
			@LEFTJOINS = coalesce(@LEFTJOINS, '') +
			' LEFT JOIN (
					SELECT  pfl.StoreID, pfl.PromoFlag
					FROM    KScoop_PromoFlag pfl INNER JOIN
									KScoop_Promo pro ON pfl.PromoID = pro.PromoID
					WHERE   pro.PromoID = ' + Cast(PromoID as Varchar) + ' ) Promo' + Cast(PromoID as Varchar) + 
				' ON sto.StoreID = Promo' + Cast(PromoID as Varchar) + '.StoreID
	' ---<<--- leave this on the new line!
	FROM KScoop_Promo Order by PromoID
	 
	SELECT @dsql =
	'SELECT  sto.StoreID, sto.StoreName, sto.StoreActive,' + @SELECT +
	'
	FROM KScoop_Store sto ' +
	@LEFTJOINS +
	'
	WHERE StoreActive IN (' + @ActiveCode + ',' + @ActiveCode2 + ')' + 
	'
	ORDER BY sto.StoreName'
 
	EXEC (@dsql)
 
 
Current result set:
StoreID	StoreName	StoreActive	Promo1	Promo2	Promo3	Promo5
19	Axxon	                  1	1	0	1	1
...

Open in new window

0
Comment
Question by:barnesco
2 Comments
 
LVL 11

Accepted Solution

by:
deroby earned 500 total points
ID: 20396296
Replace the  

'Promo' + Cast(PromoID as Varchar)

with the field you want (between Quotes for 'safety')


declare @dsql varchar(max)
declare @SELECT varchar(max)
declare @LEFTJOINS varchar(max)
 
DECLARE	@ActiveCode varchar(2)
DECLARE	@ActiveCode2 varchar(2)
DECLARE	@StoreActive varchar(2)
 
SET @ActiveCode = 1
SET	@ActiveCode2 = 0
 
 
SELECT  @SELECT =
			coalesce(@SELECT + ',', '') +  -- first one does not need comma
			' ISNULL(Promo' + Cast(PromoID as Varchar) + '.PromoFlag, 0) AS ''' + PromoName + '''', 
			@LEFTJOINS = coalesce(@LEFTJOINS, '') +
			' LEFT JOIN (
					SELECT  pfl.StoreID, pfl.PromoFlag
					FROM    KScoop_PromoFlag pfl INNER JOIN
									KScoop_Promo pro ON pfl.PromoID = pro.PromoID
					WHERE   pro.PromoID = ' + Cast(PromoID as Varchar) + ' ) Promo' + Cast(PromoID as Varchar) + 
				' ON sto.StoreID = Promo' + Cast(PromoID as Varchar) + '.StoreID
	' ---<<--- leave this on the new line!
	FROM KScoop_Promo 
    Order by PromoID
	 
	SELECT @dsql =
	'SELECT  sto.StoreID, sto.StoreName, sto.StoreActive,' + @SELECT +
	'
	FROM KScoop_Store sto ' +
	@LEFTJOINS +
	'
	WHERE StoreActive IN (' + @ActiveCode + ',' + @ActiveCode2 + ')' + 
	'
	ORDER BY sto.StoreName'
 
	EXEC (@dsql)

Open in new window

0
 

Author Closing Comment

by:barnesco
ID: 31412349
Thank you!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Insert to Begin if data exists 2 31
SSRS Deployment problem 5 64
fault SQL backup files that wont restore - how common 3 20
First Max value 3 27
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question