Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Is dynamic SQL my only option?

Author  Topic 

Harry C
Posting Yak Master

148 Posts

Posted - 2010-04-19 : 17:34:14
My customer wants to select the fields that he wants to have in his result set. No big deal, right.

What I would like to do is to say something like
DECLARE @RaceID int
DECLARE @SHowAddressInfo bit

SELECT Name, phone, Fax, Race
IF SHowAddressInfo = 1 THEN
BEGIN
,Address, City, State
END
,nextcolumn, blah, blah, blah...

Is there a simple way to achieve this? or do have to use dynamic SQL?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-19 : 17:56:57
Yes you'd have to use dynamic SQL for this. Why don't you return back all of the columns, assuming it's not hundreds of columns or something, and then only display what he selects via the front-end application? If the selections are common, then perhaps use multiple views but I doubt I'd use that solution.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Harry C
Posting Yak Master

148 Posts

Posted - 2010-04-19 : 18:04:03
I had thought about that, but here was the problem..My client wanted a crazy layout for the returned rows. YOu have probably seen my posts about this before. What I ended up doing was this...

I select all the common rows that are part of the query. Then
do a lookup via a cursor to add in a related bit of data that MAY or MAY NOT exist. Then I do a
SELET * FROM #Test which is the name of the temp table. So, I can't list out of columns on the front end, because I do not know which ones will actually be there. That cursor could generate one extra column or ten extra columns. I don't know. Thanks for the advice



SELECT r.Guid as RegistrationID
,a.Guid as AthleteID
,CASE
WHEN r.WaiverInitials = '' THEN '.us'
ELSE '.com'
END AS SiteReg
,re.RegEvtKey as RegEvtKey
,a.AthKey as AthKey
,a.Firstname as FName
,a.LastName as LName
,Convert(varchar(10),a.DateOfBirth, 101) as DOB
,a.Gender as Sex
,a.Address1 as Addr
,a.City
,a.[State] as ST
,a.Zip
,a.MedicalConditions as Medical
,a.Email
,a.DayPhone
,a.EveningPhone
,re.USATNumber
,re.TShirtSize
,re.Division as Div
, CASE r.PaperRegistration
WHEN 1 THEN 'Paper'
ELSE
CASE r.PayByCheck
WHEN 1 THEN 'Check'
ELSE 'CC'
END
END as Paytype
,e.Name as [Event]
,IsNull(re.Fee,e.Fee) as EntryFee
,r.OnlineFee
,TotalFee
,Convert(varchar(10),r.Date, 101) as RegDate
,IsNull(rd.TotalDiscount,0.00) as TotalDiscount
/*, LEFT(q.questions, LEN(q.questions)-4) as questions
, LEFT(_af.fees, LEN(_af.fees)-4) as extras
, LEFT(_uf.usat, LEN(_uf.usat)-4) as usat
, LEFT(_rl.relay, LEN(_rl.relay)-4) as relays
*/
Into #Test
FROM Registration r
INNER JOIN RegistrationEvent re ON re.RegistrationGuid = r.Guid
INNER JOIN [Event] e ON e.Guid = re.EventGuid
INNER JOIN Athlete a ON a.AccountGuid = r.AccountGuid AND a.Guid = re.AthleteGuid
LEFT JOIN RegistrationDiscount rd ON rd.RegistrationGuid = r.Guid
WHERE TotalFee >0
AND r.IsPaid = 1
--AND r.WaiverInitials<>''
AND r.RaceGuid=@RaceGuid
ORDER BY RegistrationID, AthleteID, LName
END

DECLARE @SQL nvarchar(500)
DECLARE @AddFeeGuid uniqueidentifier
DECLARE @Name varchar(100)
DECLARE @Qty varchar(100)
DECLARE @Fee varchar(100)
--DECLARE @SQL nvarchar(500)

DECLARE my_cursor CURSOR FOR
SELECT Guid, Name, Fee
FROM AdditionalFee WHERE RaceGuid=@RaceGuid
ORDER BY DisplayOrder, Name

OPEN my_cursor

FETCH NEXT FROM my_cursor
INTO @AddFeeGuid,@Name,@Fee

WHILE @@FETCH_STATUS = 0
BEGIN
SET @SQL = 'ALTER Table #test
ADD [' + @Name + '] nvarchar(300) '
EXEC(@SQL)

SET @SQL = 'ALTER Table #test
ADD [' + @Name + '_' + @Fee + '] nvarchar(300) '
EXEC(@SQL)

SET @SQL = 'UPDATE #Test
SET [' + @Name + '] = cast(ra.Qty as nvarchar(10)) ' +
' ,[' + @Name + '_' + @Fee + '] = cast(ra.Fee as nvarchar(10)) ' +
' FROM #Test ' +
' INNER JOIN RegistrationAdditionalFee as ra ON ra.RegistrationGuid=RegistrationID ' +
' AND ''' + cast(@AddFeeGuid as nvarchar(100)) + '''= ra.AdditionalFeeGuid'

PRINT @SQL
EXEC(@SQL)
FETCH NEXT FROM my_cursor
INTO @AddFeeGuid,@Name,@Fee
END
CLOSE my_cursor
DEALLOCATE my_cursor


SELET * FROM #Test
Go to Top of Page
   

- Advertisement -