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.
Author |
Topic |
t01621
Starting Member
2 Posts |
Posted - 2008-10-02 : 14:19:31
|
Hi EveryoneThis is my first post here so sorry if this isn't the correct place to post this kind of questionI am using the below query which displays all the pc's (concatenated into one field) that have a certain application installed, obviously i search for the certain application using @appFor example if I use the below query, searching for 'ms office 2000' it would display the following result ;Application/ Installed on/ Total pc'sMS office 2000/ pc1, pc2, pc3,/ 3The query I am using is;declare @app varchar(255)select @app = 'MS Office 2000'declare @comp varchar( 8000 )set @comp = ''select @comp = @comp + cn.computer + ', ' from sainstalledapplications iajoin sacomputernames cn on ia.computerid=cn.id where application = @appselect distinct Application, @comp[Installed On], count(computerid)[Total pc's] from sainstalledapplicationswhere application = @appgroup by applicationNow what I want this to do is go through all the applications field values and find the pc's that have that application installed instead of having to specify each application indivdually (there are over 300 applications and obviously this number will grow as people install more applications on their pc's)Do I have to use a while loop to do this (if so how, never used one before) or is there a better way to format the query?The tables have the following columns in case it is any help;Tablename- sainstalledapplications;columns - computerid, applicationTablename - sacomputernamescolumns - id(this links to computerid), computer, domainReally appreciate your help in advanced and if you need any more info please let me knowTom |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-02 : 14:29:42
|
one method is to create a udf like thisCREATE FUNCTION GetInstalledPCs(@app varchar(255))RETURNS varchar(8000) ASdeclare @comp varchar( 8000 )set @comp = ''select @comp = @comp + cn.computer + ', ' from sainstalledapplications iajoin sacomputernames cn on ia.computerid=cn.id where application = @appRETURN @compGO and then use it like thisselect m.Application,dbo.GetInstalledPCs(m.Application),m.[Total pc's] from(select Applicationcount(computerid)[Total pc's] from sainstalledapplicationsgroup by application)m |
|
|
t01621
Starting Member
2 Posts |
Posted - 2008-10-03 : 04:38:49
|
Thank you so much for your reply, this worked really well. It runs in less than 1 second on another database i have with over 600 entries, i'll definitely be looking into using functions for other things too!Just incase someone else wants to use this resolution i needed to add a begin and end to the function and in the select statement I needed to add a comma in between 'Application' and 'count(computerid)' -see below, thanks again visakh16CREATE FUNCTION GetInstalledPCs(@app varchar(255))RETURNS varchar(8000) ASbegindeclare @comp varchar( 8000 )set @comp = ''select @comp = @comp + cn.computer + ', ' from sainstalledapplications iajoin sacomputernames cn on ia.computerid=cn.id where application = @appRETURN @compendGOselect m.Application,dbo.GetInstalledPCs(m.Application)[PC List],m.[Total pc's] from(select Application, count(computerid)[Total pc's] from sainstalledapplicationsgroup by application)m |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-03 : 05:24:54
|
welcome |
|
|
|
|
|
|
|