【发布时间】: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