使用t-sql使用同一个表中的列运行查询

时间:2015-07-30 16:20:55

标签: sql-server tsql

我正在开发一个由SQL Server驱动的ASPX网站。该网站的一个功能是在后端数据库上运行一些报告,并将结果吐出给用户。我对整个ASPX的东西并不熟悉,所以这可能非常明显。

登录该网站的每个人都有一个site属性(例如他们所在的位置),他们可以为site生成报告。每个报告的SQL都作为文本存储在Reports表中的列中,该列也具有site列。

编写上一个SQL报告的人在每个查询的文本中都编写了硬编码site=#,所以现在他有一堆基本上具有相同查询的行,差异为WHERE site=1WHERE site=2等......如果站点数量很大,那么管理似乎很难。

是否有更有效的方法来限制查询,而不是在查询中放入WHERE语句pt.siteID=4

我希望有某种自我加入,但我似乎无法弄清楚语法。

DECLARE @myReports TABLE (ReportName varchar(max),
                          siteID int)

INSERT INTO @myReports
VALUES( 'SELECT pt.siteID, pt.patientID, endDate AS EndDate
         FROM Patient pt 
         LEFT OUTER JOIN 
              (SELECT DISTINCT patientID FROM MedicationSchedule) sm ON pt.patientID = sm.patientID
         WHERE mode = 1 AND sm.patientID IS NULL AND pt.siteID = 2', 2)

INSERT INTO @myReports
VALUES('SELECT pt.siteID, pt.patientID, endDate AS EndDate
        FROM Patient pt 
        LEFT OUTER JOIN 
            (SELECT DISTINCT patientID FROM MedicationSchedule) sm ON pt.patientID = sm.patientID
        WHERE mode = 1 AND sm.patientID IS NULL AND pt.siteID = 3', 3)

INSERT INTO @myReports
VALUES('SELECT pt.siteID, pt.patientID, endDate AS EndDate
        FROM Patient pt 
        LEFT OUTER JOIN 
            (SELECT DISTINCT patientID FROM MedicationSchedule) sm ON pt.patientID = sm.patientID
        WHERE mode = 1 AND sm.patientID IS NULL AND pt.siteID = 4', 4)

SELECT * from @myReports

2 个答案:

答案 0 :(得分:0)

这是一个例子,它作为一个程序存储在一个表中,然后你必须执行它。

create procedure ProcedureNameHere
(
    @SiteID int --or whatever datatype is appropriate
) as

    set nocount on;

    SELECT pt.siteID
        , pt.patientID
        , pt.endDate AS EndDate
    FROM Patient pt 
    LEFT OUTER JOIN 
    (
        SELECT DISTINCT ms.patientID 
        FROM MedicationSchedule ms
    ) sm ON pt.patientID = sm.patientID
    WHERE mode = 1 --not sure what table this is from, would be best to add the alias
        AND sm.patientID IS NULL 
        AND pt.siteID = @SiteID;

然后,您只需为SiteID传递适当的值即可获取相应的数据。

答案 1 :(得分:0)

有点像黑客,但这是在我的限制范围内做的:

DECLARE @myReports TABLE 
(ReportText varchar(max),
 ReportName varchar(max),
 siteID int
)

DECLARE @sites varchar(max)
SET @sites = '2,3,4,'

DECLARE @pos INT
DECLARE @len INT
set @pos = 0
set @len = 0

DECLARE @value varchar(8000)


DECLARE @mySQL varchar(max)
SET @mySQL ='SELECT pt.siteID, pt.patientID, endDate AS EndDate
           FROM Patient pt LEFT OUTER JOIN 
           (SELECT DISTINCT patientID FROM MedicationSchedule) sm ON pt.patientID=sm.patientID
         WHERE 
         mode=1 AND sm.patientID IS NULL AND 
         pt.siteID='

WHILE CHARINDEX(',', @sites, @pos+1)>0
BEGIN
    set @len = CHARINDEX(',', @sites, @pos+1) - @pos
    set @value = SUBSTRING(@sites, @pos, @len)
    --SELECT @pos, @len, @value /*this is here for debugging*/

    INSERT INTO @myReports
        SELECT @mySQL+@value AS ReportText,
        'Find No Schedules', @value  

    set @pos = CHARINDEX(',', @sites, @pos+@len) +1
END


SELECT * FROM @myReports