可以在sql中保存多个值的变量

时间:2014-12-22 16:42:20

标签: sql-server-2008

我在sql server 2008中面临一个严重的问题,这就是情况

IF @PatCntFamNbr is not null 
BEGIN

select  t.Cntid AS Center , t.PatFName AS FirstName   , t.PatMName AS MiddleName , t.PatLName AS     LastName  
from tblpatient t INNER JOIN  TblPatientCentres p
 ON   p.PatID_fk = t.PatId_PK
where   p.CentreID_fk=@cntid  and @patid in (select patid_fk from tblpatientcentres where cntfammbnbr=@cntfammbnbr)
END

我的主要问题是,这必须显示属于同一系列的许多名称,而变量@patis只接受一个变量,这不允许该过程正常工作

我将非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

如果您需要一个可以容纳多个值的变量,那么您应该使用Table Variable
IE就像DECLARE @patid TABLE(ID INT)

如果您需要通过客户端应用程序传递变量,那么研究表值类型

但是你的查询看起来不像它需要 - 它只需要修复......

IF @PatCntFamNbr is not null 
BEGIN

select  t.Cntid AS Center , t.PatFName AS FirstName   , t.PatMName AS MiddleName , t.PatLName AS     LastName  
from tblpatient t INNER JOIN  TblPatientCentres p
 ON   p.PatID_fk = t.PatId_PK
where   p.CentreID_fk=@cntid  and p.cntfammbnbr=@cntfammbnbr -- this is all you need
END
相关问题