我处理包含一个存储过程的旧项目。我无法理解存储过程然后什么是下面存储过程的SQL查询。
USE [Mydb]
GO
/****** Object: StoredProcedure [dbo].[usp_SearchCAMAStructure] Script Date: 10/19/2013 2:08:16 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_SearchCAMAStructure]
@section as nvarchar(max),
@quality as nvarchar(max),
@style as nvarchar(max),
@ward as nvarchar(max),
@improvment as nvarchar(max),
@condition as nvarchar(max)
As
Begin
SET NOCOUNT ON;
--SET STATISTICS TIME ON;
Declare @Where as varchar(max)
Declare @Select as varchar(max)
Set @Select = ' Distinct vi.struct_no as structure, a.assesmt_no as assessment, o.own_last+'' , ''+o.own_first as taxpayer, id.year_built as built, id.effect_age as age, vi.aprais_val as mktvalue, a.parcel_no as parcel
From [dbo].assessments a
inner join parcel p on a.parcel_no = p.parcel_no
inner join valueimp vi on vi.assesmt_no = a.assesmt_no and (vi.status is null or vi.status = 0)
inner join owner o on o.id = a.owner_id
inner join imp_details id on id.improvementId = vi.id and (id.isdeleted is null or id.isdeleted = 0)
inner join quality_details qd on qd.quality_id = id.quality_id
inner join section_details sd on sd.section_id = id.section_id
inner join style_details stdl on stdl.style_id = id.style_id
inner join parcel pw on p.ward_no = pw.ward_no'
Set @Where = ' where ( @section IS NULL OR sd.section_id = @section)
AND (@quality IS NULL OR qd.quality_id = @quality)
AND (@style IS NULL OR stdl.style_id = @style)
AND (@ward IS NULL OR pw.ward_no = @ward)
AND (@improvment IS NULL OR id.improvementId = @improvment)'
if @condition = 1 --Equal to
Begin
Set @Where = @Where + ' and (' +@improvment+ ' is null or id.effect_age = ' +@improvment+ ' )'
end
else
if @condition = 2 --Greater than or Equal
Begin
Set @Where = @Where + 'and (' +@improvment+ ' is null or id.effect_age > ' +@improvment+ ' )'
end
else
if @condition = 3 --Less than or equal
Begin
Set @Where = @Where + 'and (' +@improvment+ ' is null or id.effect_age < ' +@improvment+ ' )'
end
DECLARE @QUERY NVARCHAR(MAX)
SET @QUERY= 'Select '+ @SELECT + @WHERE
print @QUERY
EXEC sp_executesql @QUERY , N'@section as nvarchar(max) ,@quality as nvarchar(max),@style as nvarchar(max),@ward as nvarchar(max),@improvment as nvarchar(max)',@section ,@quality,@style,@ward,@improvment
END
那么上面存储过程的sql查询是什么。
答案 0 :(得分:1)
不确定传递此存储过程的所有参数,但这可能等同于下面的脚本,只需用适当的值替换参数
SELECT Distinct vi.struct_no as structure,
a.assesmt_no as assessment,
o.own_last ,
o.own_first as taxpayer,
id.year_built as built,
id.effect_age as age,
vi.aprais_val as mktvalue,
a.parcel_no as parcel
From [dbo].assessments a
inner join parcel p on a.parcel_no = p.parcel_no
inner join valueimp vi on vi.assesmt_no = a.assesmt_no and (vi.status is null or vi.status = 0)
inner join owner o on o.id = a.owner_id
inner join imp_details id on id.improvementId = vi.id and (id.isdeleted is null or id.isdeleted = 0)
inner join quality_details qd on qd.quality_id = id.quality_id
inner join section_details sd on sd.section_id = id.section_id
inner join style_details stdl on stdl.style_id = id.style_id
inner join parcel pw on p.ward_no = pw.ward_no
WHERE
( @section IS NULL OR sd.section_id = @section) AND
( @quality IS NULL OR qd.quality_id = @quality) AND
( @style IS NULL OR stdl.style_id = @style) AND
( @ward IS NULL OR pw.ward_no = @ward) AND
( @improvment IS NULL OR id.improvementId = @improvment) AND
1 = (CASE
WHEN @condition = 1 AND ( @improvment is null or id.effect_age = @improvment) THEN 1
WHEN @condition = 2 AND ( @improvment is null or id.effect_age > @improvment) THEN 1
WHEN @condition = 3 AND ( @improvment is null or id.effect_age < @improvment) THEN 1
END)
答案 1 :(得分:0)
这里有一些参数,用于执行此存储过程。
@section as nvarchar(max),
@quality as nvarchar(max),
@style as nvarchar(max),
@ward as nvarchar(max),
@improvment as nvarchar(max),
@condition as nvarchar(max)
你有两部分sql查询,一部分是@Select变量,另一部分是@Where变量。 @Select包含连接和选定的列名,您正在根据@Condition输入创建@Where子句。 这里使用了字符串连接,你可以在sql server中的字符串连接上google了解更多信息 最后,您将使用
中的@Select和@Where子句进行完整查询SET @QUERY= 'Select '+ @SELECT + @WHERE
打印并执行查询。