Ошибка Sql-getting должна объявить скалярную переменную "@from_DATE".
DECLARE @Columns NVARCHAR(MAX) = '' DECLARE @FROM_DATE date DECLARE @TO_DATE date set @FROM_DATE = '10/11/2017' set @TO_DATE ='10/11/2018' SELECT @Columns = @Columns + QUOTENAME(marriage_type) + ',' FROM (SELECT top 4 marriage_type from marriages m,DOCUMENT_TYPES d where d.recordable_document='Y' and m.marriage_type=d.document_type and d.MODULE_ID = 'MARRIAGE' group by marriage_type) t SELECT @Columns = SUBSTRING(@Columns, 0, LEN(@Columns)) DECLARE @Query NVARCHAR(MAX) = 'SELECT location,ISNULL(Year(DATE_OF_APP),null) as Year , DATENAME(month, 2018/str(Month(DATE_OF_APP))/2) as MONTH,' + @Columns + ' FROM (SELECT DATE_OF_APP, location,marriage_type FROM marriages m where DATE_OF_APP is not null and m.DATE_OF_APP >= @FROM_DATE AND m.DATE_OF_APP <= @TO_DATE) SourceTable PIVOT ( count(marriage_type) FOR marriage_type IN (' + @Columns + ') ) AS PivotTable ' EXECUTE(@query)
Что я уже пробовал:
DECLARE @Columns NVARCHAR(MAX) = '' DECLARE @FROM_DATE date DECLARE @TO_DATE date set @FROM_DATE = '10/11/2017' set @TO_DATE ='10/11/2018' SELECT @Columns = @Columns + QUOTENAME(marriage_type) + ',' FROM (SELECT top 4 marriage_type from marriages m,DOCUMENT_TYPES d where d.recordable_document='Y' and m.marriage_type=d.document_type and d.MODULE_ID = 'MARRIAGE' group by marriage_type) t SELECT @Columns = SUBSTRING(@Columns, 0, LEN(@Columns)) DECLARE @Query NVARCHAR(MAX) = 'SELECT location,ISNULL(Year(DATE_OF_APP),null) as Year , DATENAME(month, 2018/str(Month(DATE_OF_APP))/2) as MONTH,' + @Columns + ' FROM (SELECT DATE_OF_APP, location,marriage_type FROM marriages m where DATE_OF_APP is not null and m.DATE_OF_APP >= @FROM_DATE AND m.DATE_OF_APP <= @TO_DATE) SourceTable PIVOT ( count(marriage_type) FOR marriage_type IN (' + @Columns + ') ) AS PivotTable ' EXECUTE(@query)
Я получаю ошибку :
Msg 137, Уровень 15, Состояние 2, Строка 2
Необходимо объявить скалярную переменную "@FROM_DATE".