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)
 How do I make this a function?

Author  Topic 

DGOBRIEN
Starting Member

1 Post

Posted - 2010-04-21 : 11:52:39
I have this query:

DECLARE @CurrentNames varchar(max)
DECLARE @query varchar(max)
SELECT @CurrentNames = coalesce(@CurrentNames + ',' , '') + ' [' + [Name] + '] ' FROM [LC2].[dbo].[lu_tbl_Updates] where currentupdate = 1
set @query = N' select * from (
SELECT distinct host.[HostID], app.Name ,host.patchstatus
FROM [LC2].[dbo].[tbl_HostUpdatesCurrent] host
join [LC2].[dbo].[v_hostDetailsBaseLine] hosts on host.hostid = hosts.hostid
join dbo.lu_tbl_Updates app on app.uid = host.PatchID
group by host.[HostID],Hosts.[HostName],app.Name,host.patchstatus
) as s PIVOT ( min(s.patchstatus) for s.Name in (' + @CurrentNames + ')) as p;'
EXECUTE( @query )

It runs fine and returns the expected data... Now I want to make it a function so that I can then use it in a view
I've tried some examples i've found but none seem to work right...
Can someone assist me?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-21 : 23:43:56
you cant use dynamic sql in a function. why dont you make it procedure instead?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-04-22 : 03:18:39
Create it as a procedure as suggested
http://beyondrelational.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

Madhivanan

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

- Advertisement -