我的查询转换了存储过程'输入

时间:2017-08-03 08:23:25

标签: sql sql-server sql-server-2008

Declare @projeId Nvarchar(50)
Declare @creatorId Nvarchar(50)
Declare @users Nvarchar(50)
Declare @subject Nvarchar(50)

Set @subject =''
Set @users = 'kemalaydik'
Set @projeId = ''
Set @creatorId = 'kemalaydik'

select * 
    from (select  STUFF((
          SELECT  ',' + u.userid
             from Users u, AuTable a
                where ((a.isAllUsers = 1 ) or
                      (a.DepartmentId is not null and u.DepartmentId = a.DepartmentId) or
                      (a.userId is not null and u.userId= a.UserId)) and
                      a.actionId = ac.ActionId
                    FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)'),1,1,'') responsibleId, 
                 ac.* 
                from Actions ac) x
        where    (x.ProjectId = @projeId or @projeId = '') and 
                 (x.CreatorId = @creatorId or @creatorId='')  and 
                 ((x.responsibleId like '%,'+ @users+ ',%' or 
                   x.responsibleId like '%,'+ @users or
                   x.responsibleId like  @users+ ',%' or
                   x.responsibleId = @users or @users='') and 
                 (x.Subject=@subject or @subject='') )

我制作了这段代码,但我的老师告诉我转换为'存储过程'有了输入,我怎么能这样做,你们可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

CREATE PROCEDURE test_procedure (@projeId Nvarchar(50), @creatorId Nvarchar(50), @users Nvarchar(50), @subject Nvarchar(50))
AS
BEGIN


select * 
    from (select  STUFF((
          SELECT  ',' + u.userid
             from Users u, AuTable a
                where ((a.isAllUsers = 1 ) or
                      (a.DepartmentId is not null and u.DepartmentId = a.DepartmentId) or
                      (a.userId is not null and u.userId= a.UserId)) and
                      a.actionId = ac.ActionId
                    FOR XML PATH(''), TYPE ).value('.', 'NVARCHAR(MAX)'),1,1,'') responsibleId, 
                 ac.* 
                from Actions ac) x
        where    (x.ProjectId = @projeId or @projeId = '') and 
                 (x.CreatorId = @creatorId or @creatorId='')  and 
                 ((x.responsibleId like '%,'+ @users+ ',%' or 
                   x.responsibleId like '%,'+ @users or
                   x.responsibleId like  @users+ ',%' or
                   x.responsibleId = @users or @users='') and 
                 (x.Subject=@subject or @subject='') )
END
GO
EXEC test_procedure '', 'kemalaydik', '', 'kemalaydik'