这些是示例表
UserDetails : id,UserName,IsShow
Brand : id,BrandName
Product : id,ProductName
BrandProduct:id,brandid,productid,price
如果Isshow为假,则显示整个价格,否则隐藏第二至第四位数字(价格为5位数字)
Sp 1:
create procedure Sp_Details
@brandid int,
@productid int,
@IsShow bit
as
begin
select B.Brandname,P.Producetname,Bp.Price
from BrandProduct Bp
inner join B
on Bp.brandid = B.id
inner join P
on Bp.productid = P.id
where brandid = @brandid
and productid = @productid
// If Isshow is true show this as result,else pass this result to one sp(generic sp) which take that result and do following (replace ) and give result to this sp.
REPLACE(Price, (SUBSTRING(Price,2, 3*@Isshow)), 'xxx')
end
该怎么做?如何将结果传递给另一个sp,将计算结果返回给该sp ?。有如此多的地方/页面需要执行,这就是为什么要创建通用sp。许多用户可以进行交互的原因同时具有相同的sp。一个用户数据不应与其他数据混合。
答案 0 :(得分:1)
您可以使用简单的case
表达式在单个存储过程中完成所有操作:
CREATE PROCEDURE Stp_Details -- NOTE THE CHANGE OF THE NAME!
@brandid int,
@productid int,
@IsShow bit
AS
BEGIN
SELECT B.Brandname,
P.Producetname,
CASE WHEN @IsShow = 1 THEN
Bp.Price
ELSE
RIGHT(Price ,2) + 'xxx'
END AS Price
FROM BrandProduct Bp
INNER JOIN B
ON Bp.brandid = B.id
INNER JOIN P
ON Bp.productid = P.id
WHERE brandid = @brandid
AND productid = @productid
END
一些注意事项:
sp_
前缀。此前缀由Microsoft保留用于系统过程,因此您可能会遇到名称冲突。 REPLACE(Price, (SUBSTRING(Price,2, 3*@Isshow)), 'xxx')
和简单的字符串连接,而不是麻烦的RIGHT
。答案 1 :(得分:0)
如果需要通用存储过程,则可以使用临时表在存储过程之间共享数据。内部存储过程将看到在外部存储过程中创建的临时表。示例:
create procedure Sp_Details
@brandid int,
@productid int,
@IsShow bit
as
begin
select SomeColumns
into #tableForMechanism
from SomeData
if @IsShow = 0 then
begin
exec innerDetailsProcedure
end
select SomeColumns from #tableForMechanism
end
go
create procedure innerDetailsProcedure
begin
if OBJECT_ID('tempdb..#tableForMechanism') is NULL
begin
raiserror('Table for mechanism is not created', 16, 1);
return -1;
end;
INSERT/UPDATE/DELETE #tableForMechanism
end
go
如果总是返回数据,则应将参数@IsShow重命名以表示其真实目的,如@doXYZModification
也请检查以下链接:如何在存储过程http://www.sommarskog.se/share_data.html之间共享数据