目标:我想总共查询三个表并分别显示每一行。
我只需要显示来自TblA和TblF的结果,因为TblProperty是父表,所以虽然我们可能使用它搜索但我不需要它的数据。
即。用户可能搜索邮政编码,但用户可能只搜索TblA中的评级。
我提供了两段代码。第一个是减少版本,我认为这可能有助于指导读者和我自己的解决方案。第二个代码是完整版。 (我需要添加一些引号等等。但是在我主要部分工作后我正在测试我的时间)
要点: 如果我有一个来自TblA的结果和一个来自TblF的结果我想要两行数据而不是一行。
是:
SQL Server Management Studio 2012
查询:
我希望在这个阶段能看到一双新鲜的眼睛。也许我需要首先搜索两个表然后搜索属性或者查找创建临时表?
代码1:
USE DB
DECLARE @QUERY NVARCHAR(MAX) = ''
DECLARE @QUERYSTRING NVARCHAR(MAX) = ''
DECLARE @sTypeOfUtility NVARCHAR(MAX) = '2'
SET @QUERY =
'SELECT
p.ID AS ID,
p.UPRN AS UPRN,
COALESCE(a.OverallRiskCategory,''0'') AS RiskType2,
COALESCE(f.RiskRating,''0'') AS RiskType3,
COALESCE(a.TypeOfUtility,'''') + COALESCE(f.TypeOfUtility,'''') AS TypeOfUtility
FROM TblProperty AS p'
SET @QUERY = @QUERY + ' INNER JOIN TblA AS a on a.UPRN = p.UPRN'
SET @QUERY = @QUERY + ' INNER JOIN TblFAS f on f.FIREUPRN = p.UPRN'
IF @sTypeOfUtility = '2'
SET @QUERYSTRING = @QUERYSTRING + ' AND a.TypeOfUtility LIKE ''%' + LTRIM(RTRIM(@sTypeOfUtility)) + '%'''
IF @sTypeOfUtility = '3'
SET @QUERYSTRING = @QUERYSTRING + ' AND f.TypeOfUtility LIKE ''%' + LTRIM(RTRIM(@sTypeOfUtility)) + '%'''
SET @QUERY = LTRIM(RTRIM(@QUERY)) + ' WHERE 1 = 1 ' + LTRIM(RTRIM(@QUERYSTRING)) + ' ORDER BY typeofutility DESC'
EXECUTE(@QUERY)
代码2(目前为止只有完整代码,但只有两个表格):
USE [DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
ALTER PROCEDURE [dbo].[spGridSearch]
@sRiskRating NVARCHAR(50),@sUPRN NVARCHAR(20),
@sPostcode VARCHAR(20), @sPropertyName NVARCHAR(50) ,
@sStreet NVARCHAR(50), @sTypeOfUtility NVARCHAR(10),
@sDateFrom DATETIME, @sDateTo DATETIME
AS
BEGIN
DECLARE @QUERY NVARCHAR(MAX) = ''
DECLARE @QUERYSTRING NVARCHAR(MAX) = ''
SET @QUERY =
'SELECT
p.ID AS ID,
p.UPRN AS UPRN,
COALESCE(a.OverallRiskCategory,''0'') AS OverallRiskCategory,
COALESCE(a.TypeOfUtility,''0'') AS TypeOfUtility,
COALESCE(a.SurveyDate,'''') AS SurveyDate, COALESCE(a.ItemRef, '''') AS ItemRef,
COALESCE(a.NextSurveyDue,'''') AS NextSurveyDue ,
COALESCE(a.Recommendations,''NO DATA'') AS Recommendations,
COALESCE(a.StatusOfIssue,''0'') As StatusOfIssue
FROM TblProperty AS p '
SET @QUERY = @QUERY + ' LEFT JOIN TblA AS a on p.UPRN = a.UPRN '
IF @sRiskRating <> '1234xyz'
SET @QUERYSTRING = @QUERYSTRING + ' AND a.OverallRiskCategory LIKE ''%' + LTRIM(RTRIM(@sRiskRating)) + '%'''
IF @sTypeOfUtility <> '1234xyz'
SET @QUERYSTRING = @QUERYSTRING + ' AND a.TypeOfUtility LIKE ''%' + LTRIM(RTRIM(@sTypeOfUtility)) + '%'''
--IF @sDateFROM <> '2050-01-01' AND @sDateTO <> '2050-01-01'
--SET @QUERYSTRING = @QUERYSTRING + ' AND a.SurveyDate BETWEEN ' + @sDateFrom + ' AND ' + @sDateTo
IF @sUPRN <> '1234xyz'
SET @QUERYSTRING = @QUERYSTRING + ' AND p.UPRN LIKE ''%' + LTRIM(RTRIM(@sUPRN)) + '%'''
IF @sPostcode <> '1234xyz'
SET @QUERYSTRING = @QUERYSTRING + ' AND p.Postcode LIKE ''%' + LTRIM(RTRIM(@sPostcode)) + '%'''
IF @sPropertyName <> '1234xyz'
SET @QUERYSTRING = @QUERYSTRING + ' AND p.BuildingNo LIKE ''%' + LTRIM(RTRIM(@sPropertyName)) + '%'''
IF @sStreet <> '1234xyz'
SET @QUERYSTRING = @QUERYSTRING + ' AND p.Street LIKE ''%' + LTRIM(RTRIM(@sStreet)) + '%'''
IF LEN(LTRIM(RTRIM(@QUERYSTRING))) > 5
--Remove last as we dont need it
--SET @QUERYSTRING = LEFT(@QUERYSTRING, NULLIF(LEN(@QUERYSTRING)-1,-1))
SET @QUERY = LTRIM(RTRIM(@QUERY)) + ' WHERE 1 = 1 ' + LTRIM(RTRIM(@QUERYSTRING))
EXECUTE(@QUERY)
END
参考文献: http://support.sas.com/documentation/cdl/en/sqlproc/62086/HTML/default/viewer.htm#a001361784.htm http://www.w3schools.com/sql/sql_join.asp
答案 0 :(得分:1)
正如我在评论中提到的,你应该使用UNION ALL语句来获取两个表的所有结果。这要求两个select都具有相同的列数,并且列应具有相同的数据类型 你的查询基本上是这样的:
SELECT
...
FROM TblProperty AS p
INNER JOIN TblA AS a on a.UPRN = p.UPRN
UNION ALL
SELECT
...
FROM TblProperty AS p
INNER JOIN TblFAS f on f.FIREUPRN = p.UPRN
我还建议使用sp_executesql和命名参数,例如named parameters in sp_executesql