SQL Server存储过程中的SELECT列AS @ParameterName

时间:2016-11-29 16:22:13

标签: sql sql-server sql-server-2008

我有一个存储过程,我需要传入一个列别名作为参数,我该如何使其工作?

这是存储过程的一行给我带来麻烦:

ManufacturerPriceListQty.Price As @PriceLevelAlias

这是存储过程:

ALTER PROCEDURE [dbo].[Export_Products]
    @PriceLevelAlias AS VARCHAR(25),
    @PriceListCodes AS VARCHAR(250) --Exmaple: 'Des', 'Designer', 'Non-Stocking', 'NonStocking'
AS
BEGIN
    SET NOCOUNT ON;
    --PRINT @PriceListCodes
    --SELECT * FROM dbo.Split(@PriceListCodes,',')
    -- Insert statements for procedure here

    SELECT  
        CAST(p.ManufacturerID as varchar(2))+'-'+p.ProductNumber  AS ItemID,
        SUBSTRING(p.ProductName,0,100) as ItemName,
        p.ProductName AS [Description],
        ManufacturerPriceListQty.Price As @PriceLevelAlias,
        ManufacturerPriceListQty.Qty as OnHandQuantity,
        ManufacturerPriceListQty.MultipleQty as OrderMinimumQuantity,
        ManufacturerPriceListQty.MultipleQty as OrderMultipleQuantity,
        Manufacturer.CompanyName AS CatalogName,
        Manufacturer.CompanyName AS CatalogCode,
        p.ProductNumber as UDF1,
        CAST(p.ManufacturerID as varchar(2)) AS UDF2,
        '%'+CAST(p.ProductID as varchar(10)) as UDF5,
        CASE 
           WHEN P.Active ='1' THEN 'FALSE'  
           ELSE 'TRUE'  
        END AS IsDeleted,
        @PriceLevelAlias AS PriceLevel,
        ManufacturerPriceList.PriceListCode,
        ManufacturerPriceListProduct.PriceListID
    FROM         
        ManufacturerPriceListProduct
    INNER JOIN
        ManufacturerPriceList ON ManufacturerPriceListProduct.PriceListID = ManufacturerPriceList.PriceListID 
    INNER JOIN
        Manufacturer ON ManufacturerPriceList.ManufacturerID = Manufacturer.ManufacturerID 
    INNER JOIN
        ManufacturerPriceListQty ON ManufacturerPriceListProduct.PriceListProductID = ManufacturerPriceListQty.PriceListProductID 
    INNER JOIN
        Product p ON ManufacturerPriceListProduct.ProductID = p.ProductID
    WHERE     
        (Manufacturer.Active = 1)
        AND p.Discontinued = 0
        AND PriceListCode IN (SELECT * FROM dbo.Split(@PriceListCodes, ',')) 
END

3 个答案:

答案 0 :(得分:0)

而不是直接脚本,请尝试将最终的select语句用作Query字符串。试试以下内容:

ALTER PROCEDURE [dbo].[Export_Products]
    @PriceLevelAlias AS VARCHAR(25),
    @PriceListCodes AS VARCHAR(250) --Exmaple: 'Des', 'Designer', 'Non-Stocking', 'NonStocking'
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    --PRINT @PriceListCodes
    --SELECT * FROM dbo.Split(@PriceListCodes,',')
        -- Insert statements for procedure here
    DECLARE @v_Qry VARCHAR(MAX)

    SELECT
        @v_Qry = '
                SELECT  
                CAST(p.ManufacturerID as varchar(2))+''-''+p.ProductNumber  AS ItemID
                ,SUBSTRING(p.ProductName,0,100) as ItemName
                ,p.ProductName AS [Description]
                ,ManufacturerPriceListQty.Price As '+@PriceLevelAlias+'
                ,ManufacturerPriceListQty.Qty as OnHandQuantity
                ,ManufacturerPriceListQty.MultipleQty as OrderMinimumQuantity
                ,ManufacturerPriceListQty.MultipleQty as OrderMultipleQuantity
                ,Manufacturer.CompanyName AS CatalogName
                ,Manufacturer.CompanyName AS CatalogCode
                ,p.ProductNumber as UDF1
                ,CAST(p.ManufacturerID as varchar(2)) AS UDF2
                ,''%''+CAST(p.ProductID as varchar(10)) as UDF5
                ,CASE 
                    WHEN P.Active =''1'' THEN ''FALSE'' 
                    ELSE ''TRUE'' 
                END AS IsDeleted
                ,@PriceLevelAlias AS PriceLevel
                ,ManufacturerPriceList.PriceListCode
                ,ManufacturerPriceListProduct.PriceListID
                FROM         ManufacturerPriceListProduct INNER JOIN
                                      ManufacturerPriceList ON ManufacturerPriceListProduct.PriceListID = ManufacturerPriceList.PriceListID INNER JOIN
                                      Manufacturer ON ManufacturerPriceList.ManufacturerID = Manufacturer.ManufacturerID INNER JOIN
                                      ManufacturerPriceListQty ON ManufacturerPriceListProduct.PriceListProductID = ManufacturerPriceListQty.PriceListProductID INNER JOIN
                                      Product p ON ManufacturerPriceListProduct.ProductID = p.ProductID
                WHERE     (Manufacturer.Active = 1)
                AND p.Discontinued=0
                AND PriceListCode 
                IN(SELECT * FROM dbo.Split(@PriceListCodes,'',''))'


    EXEC(@v_Qry)

END

答案 1 :(得分:0)

不可能动态设置列的别名。实现这一目标的唯一方法是动态创建完整语句(已有答案)。

使用一组封闭的可能值,您可以使用IF的结构来定义所有可能组合的语句。

如果您有一组封闭的值且调用代码知道要使用哪个列名,以下内容可能会对您有所帮助。

当您的SP返回结果集时,您可能包含所有可能名称的相同值。看看这个例子

SELECT *
      ,type_desc AS Name1 
      ,type_desc AS Name2 
      ,type_desc AS Name3
      --add all possible types here
FROM sys.objects

调用后使用哪一列,将是调用代码的决定......

答案 2 :(得分:0)

还需要在字符串之外采取另外两个变量:

`ALTER PROCEDURE [dbo]。[Export_Products]     @PriceLevelAlias AS VARCHAR(25),     @PriceListCodes AS VARCHAR(250) - 例如:' Des',' Designer',' Non-Stocking'' NonStocking' 如 开始      - 添加SET NOCOUNT ON以防止出现额外的结果集      - 干扰SELECT语句。     设置NOCOUNT ON;     --PRINT @PriceListCodes     --SELECT * FROM dbo.Split(@PriceListCodes,',')          - 在此插入程序声明     DECLARE @v_Qry VARCHAR(MAX)

SELECT
    @v_Qry = '
            SELECT  
            CAST(p.ManufacturerID as varchar(2))+''-''+p.ProductNumber  AS ItemID
            ,SUBSTRING(p.ProductName,0,100) as ItemName
            ,p.ProductName AS [Description]
            ,ManufacturerPriceListQty.Price As '+@PriceLevelAlias+'
            ,ManufacturerPriceListQty.Qty as OnHandQuantity
            ,ManufacturerPriceListQty.MultipleQty as OrderMinimumQuantity
            ,ManufacturerPriceListQty.MultipleQty as OrderMultipleQuantity
            ,Manufacturer.CompanyName AS CatalogName
            ,Manufacturer.CompanyName AS CatalogCode
            ,p.ProductNumber as UDF1
            ,CAST(p.ManufacturerID as varchar(2)) AS UDF2
            ,''%''+CAST(p.ProductID as varchar(10)) as UDF5
            ,CASE 
                WHEN P.Active =''1'' THEN ''FALSE'' 
                ELSE ''TRUE'' 
            END AS IsDeleted
            ,''' + @PriceLevelAlias + ''' AS PriceLevel
            ,ManufacturerPriceList.PriceListCode
            ,ManufacturerPriceListProduct.PriceListID
            FROM         ManufacturerPriceListProduct INNER JOIN
                                  ManufacturerPriceList ON ManufacturerPriceListProduct.PriceListID = ManufacturerPriceList.PriceListID INNER JOIN
                                  Manufacturer ON ManufacturerPriceList.ManufacturerID = Manufacturer.ManufacturerID INNER JOIN
                                  ManufacturerPriceListQty ON ManufacturerPriceListProduct.PriceListProductID = ManufacturerPriceListQty.PriceListProductID INNER JOIN
                                  Product p ON ManufacturerPriceListProduct.ProductID = p.ProductID
            WHERE     (Manufacturer.Active = 1)
            AND p.Discontinued=0
            AND PriceListCode 
            IN(SELECT * FROM dbo.Split(''' + @PriceListCodes + ''','',''))'


EXEC(@v_Qry)

END`