USE [XXX] -----数据库的表名
GO
/****** Object: StoredProcedure [dbo].[spGetMonthlyData] Script Date: 1/23/2019 5:15:48 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
**************************************************/
CREATE PROCEDURE [dbo].[spGetMonthlyData]
@yearMonth varchar(50) --format like 201901
AS
BEGIN
BEGIN TRY
--Create views for DownloadURICountryMonthly
Declare @URICountrysqlStr varchar(2000) -----声明
Set @URICountrysqlStr ='insert into
table(value1,value2,value3,value4,value5)
SELECT SUBSTRING([value1],1,6) ,[value2],[value3],sum(value4) as Completed ,sum(value5) as ActivityCount
FROM
table2 WITH(NOLOCK) where SUBSTRING([value1],1,6)= '+@yearMonth+'
GROUP BY SUBSTRING([value1],1,6),[value2],[value3]'
EXEC(@URICountrysqlStr)
print 'success table1'+ @yearMonth +'completed!'
END TRY
BEGIN CATCH
PRINT 'ErrorNumber: ' + CONVERT(varchar(20), ERROR_NUMBER()) + ', ErrorMessage:' + CONVERT(varchar(4000), ERROR_MESSAGE())
END CATCH
END