【问题标题】:SQL Server - Stuck with Stored ProcedureSQL Server - 卡在存储过程中
【发布时间】:2014-04-13 09:16:21
【问题描述】:

我有这个存储过程:

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 

我想在 SQL Server 本身中使用 productID = 2 之类的硬编码值执行此查询,并且我不想要声明。如果我只需要@sql,其中所有值都必须在没有声明的情况下给出

我想查看 SQL Server 本身的值。我需要怎么做?我对 SQL Server 很陌生。请通过提供您的解决方案来帮助我。

【问题讨论】:

  • 如果您运行存储过程,您将运行定义中的所有内容。如果您只想运行在其中构建的查询,则可以将其作为单独的查询运行,使用硬编码值而不是参数。我现在收到你的问题了吗?
  • 我的实际问题是我需要在 android 中复制和粘贴单个查询,因为我已经在 android 的这个存储过程中使用了所有表。我不能在这里给出相同的声明。
  • 旁注:您应该为您的存储过程使用sp_ 前缀。微软有reserved that prefix for its own use (see Naming Stored Procedures),你确实会在未来某个时候冒着名称冲突的风险。 It's also bad for your stored procedure performance。最好只是简单地避免 sp_ 并使用其他东西作为前缀 - 或者根本不使用前缀!
  • 我还是没有收到你的问题。这是什么意思 “我想在 Sql Server 本身中执行这个查询”“我想在 SQL Server 本身中查看值。”。它本身是什么意思?

标签: sql sql-server stored-procedures sql-server-2012


【解决方案1】:

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

例如

EXEC Sp_GetAllInventoryPivot 10,2,4,50

当您向以下参数提供值时显示结果

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

【讨论】:

    【解决方案2】:

    您可以直接调用 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
    

    如果这样做,请不要忘记在末尾注释掉END,并且在切换回创建 SP 时,取消注释原始行并注释这八行。

    【讨论】:

      【解决方案3】:

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

      EXEC Sp_GetAllInventoryPivot @UserId = 1, @ClientId = 2, @projectID = 2, @productID = 2; 
      

      EXECkeyword 用于执行命令,在本例中是您的存储过程。然后,您可以指定参数及其值。参数名称是可选的,所以你也可以这样做:

      EXEC Sp_GetAllInventoryPivot 1,2,2,2;
      

      编辑:假设您的问题是关于使用所谓的“默认”值运行存储过程,以便即使不指定任何参数也可以运行,您可以像这样初始化参数:

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

      现在您可以简单地调用过程而无需指定参数值。

      【讨论】:

      • 我不想声明。我需要一个没有声明的查询。即,我只想要@sql
      • 什么意思?在您的存储过程中有一个名为@sql 的变量。那是你想要运行的吗?
      猜你喜欢
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多