2015-07-21 95 views
2

目前我有在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個變量。

+0

可能dublicate的:http://stackoverflow.com/questions/30295640/how-to-return-dynamic-columns-from -grouped-values-in-sql-server-stored-procedures/30297102#30297102 – CeOnSql

+0

@CeOnSql我得到這個錯誤:轉換日期和/或時間從字符串轉換失敗。 – yuro

回答

4

我覺得你這樣的事情之後是:

ALTER PROCEDURE [dbo].[MonthRepo] 
     -- Add the parameters for the stored procedure here 
     @from datetime, 
     @to datetime 
    AS 
    BEGIN 

     DECLARE @cols nvarchar(max) 
     DECLARE @query nvarchar(max) 

     SET NOCOUNT ON; 

     WITH vals AS (
      SELECT DISTINCT isin 
      FROM ds 
     ) 
     SELECT @cols = COALESCE(@cols + ',','') + '[' + isin + ']' 
     FROM vals 

     SET @query = ' 
      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_param AND ds.datum <= @to_param 
      ) tbl 
      PIVOT (
       COUNT(Quote) 
       FOR isin IN(' + @cols + ') 
      ) piv' 

     EXECUTE sp_executesql @query, N'@from_param DATETIME, @to_param DATETIME', @from_param = @from, @to_param = @to 
    END 
+0

您定義的@cols '是靜態的。當我在ds表中添加一個新的isin時,我需要一個自動添加到存儲過程中的isins的定義。 – yuro

+0

我剛剛添加了動態獲取的值,因爲它們被添加到ds表中 - 請再次查看上面的內容。 – Horia

+0

我得到一個錯誤'字符串'DE0))piv' – yuro

0
CREATE PROCEDURE [dbo].[MonthRepo] 
     -- Add the parameters for the stored procedure here 
     @from VARCHAR(20), 
     @to VARCHAR(20) 
    AS 
    BEGIN 
    IF OBJECT_ID('tempdb..#T') IS NOT NULL 
DROP TABLE #T 

create table #T 
(
    datnum varchar(20), 
    quote INT, 
    isin VARCHAR(20) 
) 
insert into #T (datnum,quote,isin)values ('2015-01-01',100,'AB000001'), 
('2015-01-01',100,'AB000002'), 
('2015-01-02',98,'AB000003'), 
('2015-01-02',70,'AB000001'), 
('2015-01-03',100,'AB000001') 
    DECLARE @statement NVARCHAR(max) 
    ,@columns NVARCHAR(max) 



SELECT @columns = Isnull(@columns + ', ', '') + N'[' + tbl.isin+ ']' 
FROM (SELECT DISTINCT isin 
     FROM #T) AS tbl 
--SELECT @columns 
SELECT @statement = ' SELECT * 
    FROM (
     SELECT ROUND(ds.quote,0) AS Quote, 
       ROUND(ds.quote,0) AS Quote_Out, 
       ds.isin 
     FROM #T ds 
     WHERE ds.datnum >= CONVERT(datetime,'+''''[email protected]+''' ,105) AND ds.datnum <= CONVERT(datetime,'''[email protected]+''' ,105) 

    ) tbl 
    PIVOT (
     COUNT(Quote) 
     FOR isin IN(' + @columns+ ')) as PVT 

' 

PRINT @statement 
EXEC sp_executesql @statement = @statement 


    END 


EXEC [MonthRepo] @from = '2015-01-01',@to = '2015-01-10' 
+0

我得到這個錯誤信息:轉換日期和/或時間從字符串轉換失敗。 – yuro

+0

@yuro抱歉,現在晚了,只有我看到了評論 – mohan111