我正在努力获得类似以下代码的功能。如果我硬编码 “WHERE ip.ICD9Code IN(SELECT RiskFactorICD9 FROM @ RiskFactorICD9 WHERE RiskFactorType ='Tobacco')”存储过程有效但我想传递@RiskFactorType看起来像“WHERE ip.ICD9Code IN(SELECT RiskFactorICD9 FROM @ RiskFactorICD9 WHERE RiskFactorType = @RiskFactorType)“。这可能吗?
CREATE TYPE typeRiskFactorsTable AS TABLE
(RiskFactorType VARCHAR(30), RiskFactorICD9 VARCHAR(10))
GO
DECLARE @RiskFactors typeRiskFactorsTable
GO
CREATE PROCEDURE dflt.GetRiskFactors
@RiskFactorICD9 typeRiskFactorsTable READONLY,
@RiskFactorType typeRiskFactorsTable READONLY
AS
DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
SET @StartDate = '2014-01-01 00:00:00'
SET @EndDate = '2015-12-31 23:59:59'
SELECT X.*
INTO [Dflt].[RiskFactorsX]
FROM
(SELECT c.ID, ip.AdmitDateTime as DateTime, ip.Sta3n, ip.ICD9Code, ip.ICD9Description, 'IP' as DS
FROM dbo.Inpat_Inpatient ip
INNER JOIN dbo.COHORT c ON (c.ID = ip.ID)
WHERE ip.ICD9Code IN (SELECT RiskFactorICD9 FROM @RiskFactorICD9 WHERE RiskFactorType = @RiskFactorType)
AND ip.AdmitDateTime between @StartDate and @EndDate
UNION ALL
SELECT c.ID, ov.VisitDateTime as DateTime, ov.Sta3n, ov.ICD9Code, ov.ICD9Description, 'OP' as DS
FROM dbo.Outpat_Visit ov
INNER JOIN dbo.COHORT c ON (c.ID = ov.ID)
WHERE ov.ICD9Code IN (SELECT RiskFactorICD9 FROM @RiskFactorICD9 WHERE RiskFactorType = @RiskFactorType)
AND ov.VisitDateTime between @StartDate and @EndDate
) X
ORDER BY ID, DateTime, DS
GO
DECLARE @RiskFactors typeRiskFactorsTable
INSERT @RiskFactors(RiskFactorType,RiskFactorICD9)
VALUES ('Tobacco', '305.1'),
('Tobacco', '989.84'),
('Tobacco', 'V15.82'),
('PVD', '443.9'),
('PTSD', '309.81'),
('Hypotension', '458.0'),
('Hypotension', '458.1'),
('Hypotension', '458.2'),
EXEC dflt.GetRiskFactors @RiskFactorType = 'Tobacco'
GO
@Mark T @eug 这是工作代码......谢谢。
CREATE TYPE typeRiskFactorsTable AS TABLE
(RiskFactorType VARCHAR(30), RiskFactorICD9 VARCHAR(10))
GO
CREATE PROCEDURE dflt.GetRiskFactors
@RiskFactorICD9 typeRiskFactorsTable READONLY,
@RiskFactorType VARCHAR(30)
AS
DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
SET @StartDate = '2014-01-01 00:00:00'
SET @EndDate = '2015-12-31 23:59:59'
SELECT X.*
INTO [Dflt].[RiskFactorsX]
FROM
(SELECT c.ID, ip.AdmitDateTime as DateTime, ip.Sta3n, ip.ICD9Code, ip.ICD9Description, 'IP' as DS
FROM dbo.Inpat_Inpatient ip
INNER JOIN dbo.COHORT c ON (c.ID = ip.ID)
WHERE ip.ICD9Code IN (SELECT RiskFactorICD9 FROM @RiskFactorICD9 WHERE RiskFactorType = @RiskFactorType)
AND ip.AdmitDateTime between @StartDate and @EndDate
UNION ALL
SELECT c.ID, ov.VisitDateTime as DateTime, ov.Sta3n, ov.ICD9Code, ov.ICD9Description, 'OP' as DS
FROM dbo.Outpat_Visit ov
INNER JOIN dbo.COHORT c ON (c.ID = ov.ID)
WHERE ov.ICD9Code IN (SELECT RiskFactorICD9 FROM @RiskFactorICD9 WHERE RiskFactorType = @RiskFactorType)
AND ov.VisitDateTime between @StartDate and @EndDate
) X
ORDER BY ID, DateTime, DS
GO
DECLARE @RiskFactors typeRiskFactorsTable
INSERT @RiskFactors(RiskFactorType,RiskFactorICD9)
VALUES ('Tobacco', '305.1'),
('Tobacco', '989.84'),
('Tobacco', 'V15.82'),
('PVD', '443.9'),
('PTSD', '309.81'),
('Hypotension', '458.0'),
('Hypotension', '458.1'),
('Hypotension', '458.2'),
EXEC dflt.GetRiskFactors @RiskFactorType = 'Tobacco'
要使存储过程GetRiskFactors适用于所有RiskFactors,我需要将创建的表名称传递给类似
的过程SELECT X.*
INTO @Table
而不是
SELECT X.*
INTO [Dflt].[RiskFactorsX]
我如何做到这一点。
答案 0 :(得分:1)
传入存储过程的@RiskFactorType
类型为typeRiskFactorsTable
。我认为你希望它实际上是一个VARCHAR
。
CREATE PROCEDURE dflt.GetRiskFactors
@RiskFactorICD9 typeRiskFactorsTable READONLY,
@RiskFactorType VARCHAR(30)
AS
DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
SET @StartDate = '2014-01-01 00:00:00'
SET @EndDate = '2015-12-31 23:59:59'
SELECT X.*
INTO [Dflt].[RiskFactorsX]
FROM
(SELECT c.ID, ip.AdmitDateTime as DateTime, ip.Sta3n, ip.ICD9Code, ip.ICD9Description, 'IP' as DS
FROM dbo.Inpat_Inpatient ip
INNER JOIN dbo.COHORT c ON (c.ID = ip.ID)
WHERE ip.ICD9Code IN (SELECT RiskFactorICD9 FROM @RiskFactorICD9 WHERE RiskFactorType = @RiskFactorType)
AND ip.AdmitDateTime between @StartDate and @EndDate
UNION ALL
SELECT c.ID, ov.VisitDateTime as DateTime, ov.Sta3n, ov.ICD9Code, ov.ICD9Description, 'OP' as DS
FROM dbo.Outpat_Visit ov
INNER JOIN dbo.COHORT c ON (c.ID = ov.ID)
WHERE ov.ICD9Code IN (SELECT RiskFactorICD9 FROM @RiskFactorICD9 WHERE RiskFactorType = @RiskFactorType)
AND ov.VisitDateTime between @StartDate and @EndDate
) X
ORDER BY ID, DateTime, DS
GO
答案 1 :(得分:0)
我发现该代码中存在一些可能妨碍其正常运行的问题。
在宣布您的手术时,您有:
CREATE PROCEDURE dflt.GetRiskFactors
@RiskFactorICD9 typeRiskFactorsTable READONLY,
@RiskFactorType typeRiskFactorsTable READONLY
@RiskFactorType
看起来VARCHAR
应该是EXEC dflt.GetRiskFactors @RiskFactorType = 'Tobacco'
,因为你的病情有效。
此外,您的过程有两个参数,但您可以这样调用它:
var myCheckBox = new MaterialCheckbox(el);
您似乎缺少第一个参数。