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)
 dynamic table

Author  Topic 

daniel50096230
Yak Posting Veteran

99 Posts

Posted - 2014-01-06 : 04:23:11
DECLARE @pCourse_Group varchar(max) = '[01],[02],[03],[04],[04A],[05],[05C],[05O],[06],[06P],[07],[08],[09]'

How can i change my query so that when I select it, it will become multiple columns instead of single column?


SELECT @Pcourse_group

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-06 : 06:41:26
you need to use dynamic sql for that

DECLARE @SQL varchar(max)
DECLARE @pCourse_Group varchar(max) = '[01],[02],[03],[04],[04A],[05],[05C],[05O],[06],[06P],[07],[08],[09]'
SELECT @SQL= 'SELECT ' + @pCourse_Group + ' FROM tablename ... '
EXEC (@SQL)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2014-01-06 : 07:59:53
Or, if you are going to do this many times, use a function or some CLR function.
Go to Top of Page

daniel50096230
Yak Posting Veteran

99 Posts

Posted - 2014-01-06 : 20:09:18
Hi, I tried to use dynamic sql for that. But the length is exceed 4000 nvarchar. May I know how can I extend the length or can I create another dynamic sql and combine them into one?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-07 : 01:04:57
quote:
Originally posted by daniel50096230

Hi, I tried to use dynamic sql for that. But the length is exceed 4000 nvarchar. May I know how can I extend the length or can I create another dynamic sql and combine them into one?


why not use nvarchar(max). also why do you need unicode? wont varchar be enough?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2014-01-07 : 07:39:06
quote:
Originally posted by daniel50096230

DECLARE @pCourse_Group varchar(max) = '[01],[02],[03],[04],[04A],[05],[05C],[05O],[06],[06P],[07],[08],[09]'

How can i change my query so that when I select it, it will become multiple columns instead of single column?


SELECT @Pcourse_group


Why do you want to do this?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -