我正在尝试创建一个过程,我可以指定2个输入变量,并且有许多本地过程变量将返回SQL查询结果表。
这是我写的代码。
CREATE PROCEDURE sp_MS_Field_Mapping(
@MS_Table_Name VARCHAR(100),
@DCW_No VARCHAR(10)
)
AS
SET NOCOUNT ON;
DECLARE @Rsrc_Mapping varchar(100);
DECLARE @Appt_Types_Rsrc varchar(100);
DECLARE @Appt_Type_Settings varchar(100);
SET @Rsrc_Mapping = 'REF_'+@DCW_No+'_Rsrc_Mapping'
SET @Appt_Types_Rsrc = 'REF_'+@DCW_No+'_Appt_Types_Rsrc'
SET @Appt_Type_Settings = 'REF_'+@DCW_No+'_Appt_Type_Settings'
PRINT @Rsrc_Mapping
PRINT @Appt_Types_Rsrc
PRINT @Appt_Type_Settings
SELECT mst.Main_Bookshelf_Old
,mst.Main_Bookshelf_New
,mst.Main_Bookshelf_New_Mirror
,mst.Scndry_Bookshelf_Old
,mst.Scndry_Bookshelf_New
,mst.Scndry_Bookshelf_New_Mirror
,mst.Book_Name_Old
,mst.Book_Name_New
,mst.Book_Name_New_Mirror
,mst.Scndry_Book_Name_Old
,mst.Scndry_Book_Name_New
,mst.Scndry_Book_Name_New_Mirror
,mst.Tertiary_Book_Old
,mst.Tertiary_Book_New
,mst.Amb_Loc_Old
,mst.Amb_Loc_New
,mst.Rsrc_Old
,mst.Rsrc_New
,mst.Rsrc_New_Mirror
,eus.Clinic_Type
,mst.Protocol_Old
,mst.Protocol_New
,mst.Appt_Type_Old
,mst.Appt_Type_New
,cmn.Accept_Format_Name
,atr.Slot_Name
,NULL AS Orders
,rm.Service_Provider_Type
,mst.Multi_Rsrc_Clin
,mst.Known_Addit_Providers
,mst.Wait_list_Old
,ats.RL_Queue AS Wait_List_New
,cmn.MO_Tier2_Code
,cmn.MO_Tier2_Name
,cmn.OHP_Tier2_Code
,cmn.OHP_Tier2_Name
,cmn.CCC
,cmn.CCC_Name
,cmn.Delivery_Method
,cmn.Patient_Visit_Type
,cmn.Session_Type
,cmn.Care_Type
,eus.Delivery_Setting
,cmn.Appt_Class
,cmn.OOS_Inc
FROM @MS_Table_Name mst
INNER JOIN dbo.REF_DCW_CMN_Appt_Setting_Map cmn
ON mst.Appt_Type_New = cmn.Appt_Type_Name
LEFT JOIN @Rsrc_Mapping rm
ON mst.Rsrc_New = rm.Rsrc_Mnemonic
INNER JOIN @Appt_Types_Rsrc atr
ON mst.Appt_Type_New = atr.Appt_Type_Name
AND mst.Amb_Loc_New = atr.Amb_Loc
AND mst.Rsrc_New = atr.Rsrc_Available
INNER JOIN dbo.REF_EUS_Appt_Info eus
ON mst.Appt_Type_Old = eus.Appt_Type
LEFT JOIN @Appt_Type_Settings ats
ON mst.Appt_Type_New = ats.Appt_Type_Name
AND mst.Amb_Loc_New = ats.Amb_Loc
RETURN
GO
但是我收到了这些错误。我正在声明变量,所以我不明白为什么它说我需要声明它们,两个是输入变量。
消息1087,级别16,状态1,过程sp_MS_Field_Mapping,第73行
必须声明表变量“@MS_Table_Name”。消息1087,级别16,状态1,过程sp_MS_Field_Mapping,第76行
必须声明表变量“@Rsrc_Mapping”。消息1087,级别16,状态1,过程sp_MS_Field_Mapping,第78行
必须声明表变量“@Appt_Types_Rsrc”。Msg 1087,Level 16,State 1,Procedure sp_MS_Field_Mapping,Line 84
必须声明表变量“@Appt_Type_Settings”。
有人可以帮忙吗?
提前致谢。
答案 0 :(得分:2)
您需要使用动态SQL在变量中使用表名执行。使用以下代码
CREATE PROCEDURE sp_MS_Field_Mapping(
@MS_Table_Name VARCHAR(100),
@DCW_No VARCHAR(10)
)
AS
SET NOCOUNT ON;
DECLARE @Rsrc_Mapping varchar(100);
DECLARE @Appt_Types_Rsrc varchar(100);
DECLARE @Appt_Type_Settings varchar(100);
SET @Rsrc_Mapping = 'REF_'+@DCW_No+'_Rsrc_Mapping'
SET @Appt_Types_Rsrc = 'REF_'+@DCW_No+'_Appt_Types_Rsrc'
SET @Appt_Type_Settings = 'REF_'+@DCW_No+'_Appt_Type_Settings'
PRINT @Rsrc_Mapping
PRINT @Appt_Types_Rsrc
PRINT @Appt_Type_Settings
DECLARE @QRY NVARCHAR(MAX)
SET @QRY ='
SELECT mst.Main_Bookshelf_Old
,mst.Main_Bookshelf_New
,mst.Main_Bookshelf_New_Mirror
,mst.Scndry_Bookshelf_Old
,mst.Scndry_Bookshelf_New
,mst.Scndry_Bookshelf_New_Mirror
,mst.Book_Name_Old
,mst.Book_Name_New
,mst.Book_Name_New_Mirror
,mst.Scndry_Book_Name_Old
,mst.Scndry_Book_Name_New
,mst.Scndry_Book_Name_New_Mirror
,mst.Tertiary_Book_Old
,mst.Tertiary_Book_New
,mst.Amb_Loc_Old
,mst.Amb_Loc_New
,mst.Rsrc_Old
,mst.Rsrc_New
,mst.Rsrc_New_Mirror
,eus.Clinic_Type
,mst.Protocol_Old
,mst.Protocol_New
,mst.Appt_Type_Old
,mst.Appt_Type_New
,cmn.Accept_Format_Name
,atr.Slot_Name
,NULL AS Orders
,rm.Service_Provider_Type
,mst.Multi_Rsrc_Clin
,mst.Known_Addit_Providers
,mst.Wait_list_Old
,ats.RL_Queue AS Wait_List_New
,cmn.MO_Tier2_Code
,cmn.MO_Tier2_Name
,cmn.OHP_Tier2_Code
,cmn.OHP_Tier2_Name
,cmn.CCC
,cmn.CCC_Name
,cmn.Delivery_Method
,cmn.Patient_Visit_Type
,cmn.Session_Type
,cmn.Care_Type
,eus.Delivery_Setting
,cmn.Appt_Class
,cmn.OOS_Inc
FROM '+@MS_Table_Name+' mst
INNER JOIN dbo.REF_DCW_CMN_Appt_Setting_Map cmn
ON mst.Appt_Type_New = cmn.Appt_Type_Name
LEFT JOIN '+@Rsrc_Mapping+' rm
ON mst.Rsrc_New = rm.Rsrc_Mnemonic
INNER JOIN '+@Appt_Types_Rsrc+' atr
ON mst.Appt_Type_New = atr.Appt_Type_Name
AND mst.Amb_Loc_New = atr.Amb_Loc
AND mst.Rsrc_New = atr.Rsrc_Available
INNER JOIN dbo.REF_EUS_Appt_Info eus
ON mst.Appt_Type_Old = eus.Appt_Type
LEFT JOIN '+@Appt_Type_Settings+' ats
ON mst.Appt_Type_New = ats.Appt_Type_Name
AND mst.Amb_Loc_New = ats.Amb_Loc
'
EXEC SP_EXECUTESQL @QRY
RETURN
GO