1
私は、動的クエリからストアドプロシージャを作成することは不可能だと読んだことがあります。スカラー変数 "@query"をストアドプロシージャの動的tsqlとして宣言しなければならない
スカラー変数を宣言していないというエラーがあります。ここで
が働いている私のTSQL動的なレポートです:
DECLARE
@cols AS NVARCHAR(MAX),
@selcols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@startdate datetime,
@enddate datetime,
@paramdef nvarchar(max)
SET @startdate = '2013-02-01'
SET @enddate = '2013-05-10';
SET @paramdef = '@startdate datetime, @enddate datetime';
SELECT
@selcols = STUFF((SELECT ','+'ISNULL(' + QUOTENAME(convert(varchar(10), [Date], 120)) + ', 0) AS ' + QUOTENAME(convert(varchar(10), [Date], 120))
FROM AdeccoView
--where datein > @startdate
-- and datein <= @enddate
GROUP BY [Date]
ORDER BY [Date]
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
SELECT
@cols = STUFF((SELECT ',' + QUOTENAME(convert(varchar(10), [Date], 120))
FROM AdeccoView
--where datein > @startdate
-- and datein <= @enddate
GROUP BY [Date]
ORDER BY [Date]
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
SET @query = N'SELECT CreatedBy,Name, Surname,EventTypeID,Event, '+ @selcols + ',Total
from
(
select av.EventTypeID,av.CreatedBy,emp.Name,emp.Surname,Event,
convert(varchar(10), [Date], 120) [Date],
row_number() over(PARTITION BY [date],av.EventTypeID,av.CreatedBy order BY av.EventTypeID,av.CreatedBy) m,
count(*) over(partition by av.CreatedBy,av.EventTypeId) Total
from AdeccoView av
left join EventType et on et.EventTypeID = av.EventTypeID
left join Employee emp on av.CreatedBy = emp.EmployeeD
) x
pivot
(
max(m)
for [Date] in ('[email protected]+')
) p '
exec sp_executesql @query, @paramdef, @startdate = @startdate, @enddate [email protected];
そして今、
私は、多分、エンティティフレームワーク6に実装する後で簡単です(ストアドプロシージャやビューを介してそれをしたいですasp.net)。だから私のストアドプロシージャは、次のようになります。
CREATE PROCEDURE GetEmployees
(@startDateInput datetime = NULL, @endDateInput datetime = NULL)
AS
SET NOCOUNT ON;
DECLARE
@cols AS NVARCHAR(MAX),
@selcols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@startdate datetime,
@enddate datetime,
@paramdef nvarchar(max)
SET @startdate = @startDateInput
SET @enddate = @endDateInput
SET @paramdef = '@startdate datetime, @enddate datetime';
SELECT
@selcols = STUFF((SELECT ','+'ISNULL(' + QUOTENAME(convert(varchar(10), [Date], 120)) + ', 0) AS ' + QUOTENAME(convert(varchar(10), [Date], 120))
FROM AdeccoView
--where datein > @startdate
-- and datein <= @enddate
GROUP BY [Date]
ORDER BY [Date]
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
SELECT
@cols = STUFF((SELECT ',' + QUOTENAME(convert(varchar(10), [Date], 120))
FROM AdeccoView
--where datein > @startdate
-- and datein <= @enddate
GROUP BY [Date]
ORDER BY [Date]
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
SET @query = N'SELECT CreatedBy,Name, Surname,EventTypeID,Event, '+ @selcols + ',Total
from
(
select av.EventTypeID,av.CreatedBy,emp.Name,emp.Surname,Event,
convert(varchar(10), [Date], 120) [Date],
row_number() over(PARTITION BY [date],av.EventTypeID,av.CreatedBy order BY av.EventTypeID,av.CreatedBy) m,
count(*) over(partition by av.CreatedBy,av.EventTypeId) Total
from AdeccoView av
left join EventType et on et.EventTypeID = av.EventTypeID
left join Employee emp on av.CreatedBy = emp.EmployeeD
) x
pivot
(
max(m)
for [Date] in ('[email protected]+')
) p '
GO
exec sp_executesql @query, @paramdef, @startdate = @startDateInput, @enddate [email protected];
EDIT:
ここでは私のBEGINとENDで更新されたコード、取り外しGO文の
CREATE PROCEDURE GetEmployees
(@startDateInput datetime = NULL, @endDateInput datetime = NULL)
AS
SET NOCOUNT ON;
BEGIN
DECLARE
@cols AS NVARCHAR(MAX),
@selcols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@startdate datetime,
@enddate datetime,
@paramdef nvarchar(max)
SET @startdate = @startDateInput
SET @enddate = @endDateInput
SET @paramdef = '@startdate datetime, @enddate datetime';
SELECT
@selcols = STUFF((SELECT ','+'ISNULL(' + QUOTENAME(convert(varchar(10), [Date], 120)) + ', 0) AS ' + QUOTENAME(convert(varchar(10), [Date], 120))
FROM AdeccoView
--where datein > @startdate
-- and datein <= @enddate
GROUP BY [Date]
ORDER BY [Date]
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
SELECT
@cols = STUFF((SELECT ',' + QUOTENAME(convert(varchar(10), [Date], 120))
FROM AdeccoView
--where datein > @startdate
-- and datein <= @enddate
GROUP BY [Date]
ORDER BY [Date]
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
SET @query = N'SELECT CreatedBy,Name, Surname,EventTypeID,Event, '+ @selcols + ',Total
from
(
select av.EventTypeID,av.CreatedBy,emp.Name,emp.Surname,Event,
convert(varchar(10), [Date], 120) [Date],
row_number() over(PARTITION BY [date],av.EventTypeID,av.CreatedBy order BY av.EventTypeID,av.CreatedBy) m,
count(*) over(partition by av.CreatedBy,av.EventTypeId) Total
from AdeccoView av
left join EventType et on et.EventTypeID = av.EventTypeID
left join Employee emp on av.CreatedBy = emp.EmployeeD
) x
pivot
(
max(m)
for [Date] in ('[email protected]+')
) p '
END
exec sp_executesql @query, @paramdef, @startdate = @startDateInput, @enddate [email protected];
添付の私の編集コードをご覧ください。今はOKだと思いますか?ストアドプロシージャが正常に完了しました。あなたの答えは解決だと思います。ありがとう! – Stefan0309
@ Stefan0309。 。 。あなたのコードは非常に合理的です(私は体がインデントされるべきだと思うが)。 –