使用动态列名创建存储过程

时间:2017-11-02 07:26:29

标签: sql-server stored-procedures

我尝试在sql server中创建一个接受参数作为列名的过程。 (@luna) 这是代码:

CREATE PROCEDURE clientBrandM (@luna NVARCHAR(10), @zona NVARCHAR(50)) AS

SELECT client, brand, SUM(anterior) anterior, SUM(curent) curent
FROM (
    SELECT CASE WHEN t1.zona IS NULL THEN t2.zona ELSE t1.zona END AS zona,
    CASE WHEN t1.client IS NULL THEN t2.client ELSE t1.client END AS client,
    CASE WHEN t1.brand IS NULL THEN t2.brand ELSE t1.brand END AS brand,
    CASE WHEN t1.@luna IS NULL THEN 0 ELSE t1.@luna END AS anterior,
    CASE WHEN t2.@luna IS NULL THEN 0 ELSE t2.@luna END AS curent

    FROM cbm2016 t1

    FULL OUTER JOIN cbm2017 t2 ON t1.zona = t2.zona
    AND t1.client = t2.client
    AND t1.brand = t2.brand ) t3

WHERE zona = @zona
GROUP BY client, brand



EXEC clientBrandM @luna = 'luna5', @zona = 'Ardeal'

创建过程后,我收到此错误:

Msg 102, Level 15, State 1, Procedure clientBrandM, Line 8 Incorrect syntax near '@luna'.

如何让@luna(以月份为单位)改变dinamicaly?

@Marian Nasry,这是代码:

EXECUTE sp_executesql   
          N'SELECT client, brand, SUM(anterior) anterior, SUM(curent) curent
FROM (
    SELECT CASE WHEN t1.zona IS NULL THEN t2.zona ELSE t1.zona END AS zona,
    CASE WHEN t1.client IS NULL THEN t2.client ELSE t1.client END AS client,
    CASE WHEN t1.brand IS NULL THEN t2.brand ELSE t1.brand END AS brand,
    CASE WHEN t1.@luna IS NULL THEN 0 ELSE t1.@luna END AS anterior,
    CASE WHEN t2.@luna IS NULL THEN 0 ELSE t2.@luna END AS curent

    FROM cbm2016 t1

    FULL OUTER JOIN cbm2017 t2 ON t1.zona = t2.zona
    AND t1.client = t2.client
    AND t1.brand = t2.brand ) t3

WHERE zona = @zona
GROUP BY client, brand',  
          N'@luna nvarchar(10)', 
          N'@zona nvarchar(50)',
          @luna = 'luna5',  
          @zona = 'Ardeal';

3 个答案:

答案 0 :(得分:1)

使用SELECT *, 这是sp-executesql documentation

sp-executesql

答案 1 :(得分:1)

对于存储过程中的动态查询,请使用 sp-execuql

SQL查询:

ALTER PROCEDURE clientBrandM (@luna NVARCHAR(10), @zona NVARCHAR(50))
 AS BEGIN

    Declare @SQLQuery AS NVarchar(4000)

    SET @SQLQuery =N'SELECT client, brand, SUM(anterior) anterior, SUM(curent) curent
        FROM (
            SELECT CASE WHEN t1.zona IS NULL THEN t2.zona ELSE t1.zona END AS zona,
            CASE WHEN t1.client IS NULL THEN t2.client ELSE t1.client END AS client,
            CASE WHEN t1.brand IS NULL THEN t2.brand ELSE t1.brand END AS brand,
            CASE WHEN t1.'+@luna+' IS NULL THEN 0 ELSE t1.'+@luna+' END AS anterior,
            CASE WHEN t2.'+@luna+' IS NULL THEN 0 ELSE t2.'+@luna+' END AS curent
            FROM cbm2016 t1
            FULL OUTER JOIN cbm2017 t2 ON t1.zona = t2.zona
            AND t1.client = t2.client
            AND t1.brand = t2.brand ) t3
        WHERE zona = '+@zona+'
        GROUP BY client, brand'

      EXECUTE sp_executesql @SQLQuery

    END

答案 2 :(得分:1)

您可以使用以下代码

CREATE PROCEDURE clientBrandM (@luna NVARCHAR(10), @zona NVARCHAR(50)) 
AS
BEGIN
DECLARE @Sql NVARCHAR(MAX);

SET @Sql = 'SELECT client, brand, SUM(anterior) anterior, SUM(curent) curent
FROM (
    SELECT CASE WHEN t1.zona IS NULL THEN t2.zona ELSE t1.zona END AS zona,
    CASE WHEN t1.client IS NULL THEN t2.client ELSE t1.client END AS client,
    CASE WHEN t1.brand IS NULL THEN t2.brand ELSE t1.brand END AS brand,
    CASE WHEN t1.'+@luna+' IS NULL THEN 0 ELSE t1.'+@luna+' END AS anterior,
    CASE WHEN t2.'+@luna+' IS NULL THEN 0 ELSE t2.'+@luna+' END AS curent
    FROM cbm2016 t1
    FULL OUTER JOIN cbm2017 t2 ON t1.zona = t2.zona
    AND t1.client = t2.client
    AND t1.brand = t2.brand ) t3
WHERE zona = '+''''+@zona+''''+'
GROUP BY client, brand'

PRINT @Sql
EXEC SP_EXECUTESQL @Sql

END