SQL Server-被存储过程卡住

SQL Server-被存储过程卡住,sql,sql-server,stored-procedures,sql-server-2012,Sql,Sql Server,Stored Procedures,Sql Server 2012,我有一个存储过程: CREATE procedure Sp_GetAllInventoryPivot (@UserID int, @clientID int, @projectID int, @productID int ) as begin declare @pivot varchar(max);

我有一个存储过程:

CREATE procedure Sp_GetAllInventoryPivot     
   (@UserID int,          
    @clientID int,          
    @projectID int,          
    @productID int          
)            
as begin            
   declare @pivot varchar(max);            
   declare @sql varchar(max);            

   DECLARE @AttributeNameList nvarchar(max);            
   SET @AttributeNameList = N'';            

   if exists(select AttributeName FROM Attribute where ShowButton=1 and ProductID= @productID)            
   begin            
      SELECT @AttributeNameList += '[' + AttributeName + ']' + N',' 
      FROM Attribute 
      WHERE ShowButton = 1 AND ProductID = @productID AND IsDeleted = 0;            

      SELECT @pivot = LEFT(@AttributeNameList,LEN(@AttributeNameList)-1);            
      print @Pivot            

      SELECT @sql = 'SELECT * FROM           
(SELECT            
 w.WorkFlowTransId as WorkFlowTransID,          
 w.ProjectId,          
 w.ProductID,          
 w.ClientId,    
 isnull((SELECT top 1 DiscrepancyStatusCode            
 FROM [Descrepancy]             
 where WorkFlowTransId=w.WorkFlowTransId order by DiscrepancyStatusCode asc ),'''') as DiscrepancyCode,          
  Attribute.AttributeName,          
   AttributeValue.AttributeValueName,  
   isnull((SELECT count(WorkFlowValueID)            
 FROM WorkFlowValue wfv            
 where wfv.WorkFlowTransId=w.WorkFlowTransId and wfv.WorkFlowValueName=''''),'''') as Status        
FROM Attribute             
JOIN AttributeValue ON Attribute.AttributeID = AttributeValue.AttributeID  
JOIN dbo.WorkFlowTransaction w on w.WorkFlowTransId=AttributeValue.WorkFlowTransId             
 where isnull(w.IsDeleted,0)=0 and isnull(Attribute.IsDeleted,0)=0) as s                   
pivot( MIN(AttributeValueName) for AttributeName in ('+')) as p           
where p.ProductId='+Convert(varchar,@productID)+'           
and p.ClientId='+Convert(varchar,@clientID)+'          
and p.ProjectId='+Convert(varchar,@projectID)+'          
'           
exec(@sql);             
end            
end 
我希望使用硬编码值(如
productID=2
)在SQL Server本身中执行此查询,而不需要声明。如果我只需要
@sql
,其中所有值都必须不带声明地给出


我想看看SQL Server本身的值。我需要怎么做?我对SQL Server非常陌生。请给出您的解决方案来帮助我。

假设您的存储过程已成功创建,您可以这样调用它:

EXEC Sp_GetAllInventoryPivot @UserId = 1, @ClientId = 2, @projectID = 2, @productID = 2; 
EXEC
关键字用于执行命令,在本例中为存储过程。然后可以指定参数及其值。参数名称是可选的,因此您还可以执行以下操作:

EXEC Sp_GetAllInventoryPivot 1,2,2,2;
编辑:假设您的问题是关于使用所谓的“默认”值运行存储过程,使其即使不指定任何参数也能运行,那么您可以初始化参数,如下所示:

CREATE procedure Sp_GetAllInventoryPivot
@UserID int = 1,          
@clientID int = 2,          
@projectID int = 2,          
@productID int = 2

现在,您只需调用过程,而无需为参数指定值。

在新的查询窗口中,使用EXEC命令将值传递给存储过程参数

乙二醇

向如下参数提供值时显示结果

@UserID =10          
@clientID=2         
@projectID=4          
@productID=50  

您可以直接调用SP:

exec Sp_GetAllInventoryPivot 1, 1, 2, 1
或者,即使没有exec,它也可以工作:

Sp_GetAllInventoryPivot 1, 1, 2, 1
或者,出于测试目的,您可以注释掉创建过程部分并自己声明变量:

--CREATE procedure Sp_GetAllInventoryPivot     
--(          
--@UserID int,          
--@clientID int,          
--@projectID int,          
--@productID int          
--)            
--as begin     
DECLARE @UserID int
DECLARE @clientId int
DECLARE @projectID int
DECLARE @productID int

SET @UserID = 1
SET @clientId = 1
SET @projectID = 2
SET @productId = 1

如果执行此操作,请不要忘记在结尾处注释掉
结尾
,在切换回SP创建时,取消注释原始行并注释这八行。

我不需要声明。我需要一个没有声明的查询,也就是说,我只需要@sqlWhat,你是什么意思?存储过程中有一个名为
@sql
的变量。这就是您想要运行的吗?如果您运行存储过程,那么您将运行定义中的所有内容。如果您只想运行在其内部构建的查询,可以将其作为单独的查询运行,并使用硬编码的值而不是参数。我现在明白你的问题了吗?我的实际问题是我需要在android中复制和粘贴单个查询,因为我在android中使用了这个存储过程中使用的所有表。在这里,我不能给出相同的声明。旁注:您不应该在存储过程中使用
sp_
前缀。微软已经这样做了,而且你确实有可能在将来的某个时候发生名称冲突。最好只是简单地避免使用
sp.
并使用其他东西作为前缀,或者根本不使用前缀!我还是不明白你的问题。“我想在Sql Server本身中执行此查询”、“我想查看Sql Server本身中的值”是什么意思。它本身意味着什么?
--CREATE procedure Sp_GetAllInventoryPivot     
--(          
--@UserID int,          
--@clientID int,          
--@projectID int,          
--@productID int          
--)            
--as begin     
DECLARE @UserID int
DECLARE @clientId int
DECLARE @projectID int
DECLARE @productID int

SET @UserID = 1
SET @clientId = 1
SET @projectID = 2
SET @productId = 1