目前我有在SQL Server在存儲過程中定義的靜態支點SQL查詢:定義動態PIVOT SQL查詢的SQL Server
ALTER PROCEDURE [dbo].[MonthRepo]
-- Add the parameters for the stored procedure here
@from datetime,
@to datetime
AS
BEGIN
DECLARE @cols nvarchar(12)
DECLARE @query nvarchar(max)
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT *
FROM (
SELECT ROUND(ds.ct_quot_rate,0) AS Quote,
ROUND(ds.ct_quot_rate,0) AS Quote_Out,
ds.isin
FROM ds
WHERE ds.datum >= @from AND ds.datum <= @to
) tbl
PIVOT (
COUNT(Quote)
FOR isin IN(AB000001,
AB000002,
AB000003,
AB000004,
AB000005)
) piv
END
我如何定義動態查詢這個靜態代碼?我已經宣佈了2個變量。
可能dublicate的:http://stackoverflow.com/questions/30295640/how-to-return-dynamic-columns-from -grouped-values-in-sql-server-stored-procedures/30297102#30297102 – CeOnSql
@CeOnSql我得到這個錯誤:轉換日期和/或時間從字符串轉換失敗。 – yuro