存储的程序

时间:2016-10-13 13:49:36

标签: sql sql-server stored-procedures

我是Stored Procedure的新手,我正在尝试创建一个。

场景:我需要从表1中获取数据,并根据我想从表2中获取数据的输出。

示例:我的结果应如下所示:

Vendor  applicationname        Rel  Ver     Filename                FileSize
Tibco   TIBCO Business Studio   3   3.5.20  tibcobusinessstudio.ini  572
Tibco   TIBCO Business Studio   3   3.8.0   makefile                12912
Tibco   TIBCO Business Studio   3   3.9.0   tibcobusinessstudio     71023
Tibco   TIBCO Business Studio   3   3.7.0   tibcobusinessstudio     71023

表2

alter procedure proc_MainFile    
(    
@packagename nvarchar(100),    

@appname nvarchar(100) out    
)    
As Begin    
select * from table1 where package_name=@packagename   

while (select Count(applicationname) from table1 where package_name=@packagename)>0
 begin  

   set @appname = (select applicationname from table1 where package_name=@packagename ) 

select * from table2                     
where applicationname like '%' + @appname + '%'  


end    
end  


declare @aplicationname nvarchar(100);                  
execute proc_MainFile @appname=@aplicationname out,@packagename='TibcoBusnsStudio_360_00'                   
select @aplicationname;                 
go      

我尝试使用以下代码,但while循环挂起。

Get-AppvClientPackage -Name $UserInput* 

2 个答案:

答案 0 :(得分:0)

基于所提供内容的纯粹推测。我想你可以按照这些方法重做你的程序。

alter procedure proc_MainFile    
(    
    @packagename nvarchar(100)
    , @appname nvarchar(100) out    
) As Begin    

    set nocount on;

    --Using top 1 here in case there is more than 1 row returned.
    select top 1 @appname = t2.applicationname 
    from table2 t2
    join table1 t1 on t1.package_name like '%' + t2.applicationname + '%'                     
    where t1.package_name = @packagename
    order by t2.applicationname
end  

答案 1 :(得分:0)

试图重写存储过程

ALTER PROCEDURE proc_MainFile    
(    
  @packagename nvarchar(100),    
  @appname nvarchar(100) out    
)    
As Begin    
     select * from table1 where package_name=@packagename   
     IF EXISTS(select 1 from table1 where package_name=@packagename)
     begin  
       set @appname = (select applicationname from table1 where package_name=@packagename ) 
       select * from table2 where applicationname like '%' + @appname + '%'  
     end    
end  

并调用此程序

DECLARE @appname NVARCHAR(100)
EXEC proc_MainFile 'TibcoBusnsStudio_360_00',@appname OUT
SELECT @appname;