| Author |
Topic |
|
pixelwiz
Starting Member
25 Posts |
Posted - 2011-08-26 : 14:15:39
|
Hi, I have the following SQL query and would like to append an ID to the end of a variable name. Is that possible?DECLARE @sideID1 int, @sideID2 intSET @sideID1 = (SELECT id FROM event_sides WHERE event_id = 5013 AND priority = 1)SET @sideID2 = (SELECT id FROM event_sides WHERE event_id = 5013 AND priority = 2)SELECT SUM (CASE WHEN hole_points > 0 THEN 1 ELSE 0 END) AS ('winningSide' + @sideID1), SUM (CASE WHEN hole_points < 0 THEN 1 ELSE 0 END) AS ('winningSide' + @sideID1)FROM ( SELECT m.id match_id, m.session, m.match_number, m.match_type_id, dbo.udf_mp_getMatchPoints(m.id,@sideID1,@sideID2) hole_points FROM matches m WHERE m.event_id = 5013 AND NOT EXISTS (SELECT 1 FROM match_points WHERE match_id = m.id)) sub1 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
BruceT
Yak Posting Veteran
78 Posts |
Posted - 2011-08-26 : 16:23:13
|
| Hi,You would have use dynamic sql to do that I believe. Store your built sql command in a varchar variable and then execute it.Do searches for "dynamic sql" and the sp_executesql command or just execute if you don't need to pass in parameters etc.Something likedeclare @sql varchar(1000), @id intset @id = 1set @sql = 'select mycol' + cast(@id as varchar(10) + ' from mytable'execute(@sql)Will execute: select mycol1 from mytable |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-27 : 04:44:06
|
| I think op was asking a way to make aliases dynamic. I didnt understand need for this though------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2011-08-29 : 07:12:15
|
quote: Originally posted by pixelwiz Hi, I have the following SQL query and would like to append an ID to the end of a variable name. Is that possible?DECLARE @sideID1 int, @sideID2 intSET @sideID1 = (SELECT id FROM event_sides WHERE event_id = 5013 AND priority = 1)SET @sideID2 = (SELECT id FROM event_sides WHERE event_id = 5013 AND priority = 2)SELECT SUM (CASE WHEN hole_points > 0 THEN 1 ELSE 0 END) AS ('winningSide' + @sideID1), SUM (CASE WHEN hole_points < 0 THEN 1 ELSE 0 END) AS ('winningSide' + @sideID1)FROM ( SELECT m.id match_id, m.session, m.match_number, m.match_type_id, dbo.udf_mp_getMatchPoints(m.id,@sideID1,@sideID2) hole_points FROM matches m WHERE m.event_id = 5013 AND NOT EXISTS (SELECT 1 FROM match_points WHERE match_id = m.id)) sub1
Why do you want to do this? Have a look at Cross-tab reports in SQL Server help file.MadhivananFailing to plan is Planning to fail |
 |
|
|
pixelwiz
Starting Member
25 Posts |
Posted - 2011-08-29 : 08:15:24
|
| I needed to have the ID added to the alias to make something else easier, but I found another way to go about it. It'd still be cool if there was a way to make a dynamic alias name. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-29 : 08:27:40
|
| id added to alias? whats the purpose of that? how do you handle this at front end if column name changes at runtime?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|