난 내가 쿼리 실행 동적 SQLSQL Server 저장 프로 시저의 동적 선택
내 저장된 프로 시저에 의해 확인하고자하는 다음과 같은 형식
create PROCEDURE [dbo].[test proc]
@identifier varchar(20),
@issuerName varchar(max),
@max_records int=1000
AS
BEGIN
declare @select nvarchar(30)
SELECT @identifier as '@identifier'
, (
SELECT
MoodysOrgID as '@MoodysOrgID'
,ReportDate as '@ReportDate'
,m.UpdateTime as '@UpdateTime'
,m.FileCreationDate as '@FileCreationDate'
from mfm_financial_ratios m
inner join mfm_financial_ratios_coa c on c.AcctNo = m.AcctNo
where ReportDate in (select distinct top (@max_records) reportdate from mfm_financial_ratios where MoodysOrgID = m.MoodysOrgID)
and m.MoodysOrgID=(select top 1 IssuerID_Moodys as id from loans where [email protected] or [email protected]
and [email protected] and IssuerID_Moodys is not null)
order by ReportDate desc
FOR XML PATH('FinRatios'), TYPE
)
FOR XML PATH('FinRatiosHistory')
END
만에 저장된 프로 시저 보이는 한
같은create PROCEDURE [dbo].[test proc]
@identifier varchar(20),
@issuerName varchar(max),
@max_records int=1000
AS
BEGIN
declare @select nvarchar(30)
set @select = N'SELECT @identifier as '@identifier'
, (
SELECT
MoodysOrgID as '@MoodysOrgID'
,ReportDate as '@ReportDate'
,m.UpdateTime as '@UpdateTime'
,m.FileCreationDate as '@FileCreationDate'
from mfm_financial_ratios m
inner join mfm_financial_ratios_coa c on c.AcctNo = m.AcctNo
where ReportDate in (select distinct top (@max_records) reportdate from mfm_financial_ratios where MoodysOrgID = m.MoodysOrgID)
and m.MoodysOrgID=(select top 1 IssuerID_Moodys as id from loans where [email protected] or [email protected]
and [email protected] and IssuerID_Moodys is not null)
order by ReportDate desc
FOR XML PATH('FinRatios'), TYPE
)
FOR XML PATH('FinRatiosHistory')'
exec @select
END
다음 저장된 프로 시저가 사용 된 쉼표 때문에 문제가 발생합니다. 누군가 올바른 방법을 알려줄 수 있습니까?
당신은 두 가지 문제가 큰 따옴표로 탈출한다 ... 첫째, 당신이 필요 각각의 작은 따옴표 (''')를 작은 따옴표 ('''' ')로 변경하여 동적 쿼리의 따옴표를 이스케이프 처리하십시오. 다음 문제는 동적 SQL 내의 변수가 범위를 벗어 났으며'sp_ExecuteSQL'을 사용하여 전달해야합니다. 그러나 동적 SQL에 의해 * 이것을 원할 이유는 없습니다. 왜이 일을하려고하는지 물어봐도 될까요? –
나는 여러 샘플을 다른 샘플로 저장하지 않으므로 다른 이유로 여러 부분으로 질의를 수행 할 수있다. – user3290807