日期:2014-05-19  浏览次数:20478 次

SQL 问题!

CREATE         proc   MainProductInfo(   @UserName   varchar(50),@U_type   varchar(10))
as
declare   @Price   varchar(50)
begin
                select   @Price=   DefPriceType   from   PUBCustomer   where   CustomerID   in
                (select   CustomerID   from   user_for_web   where   U_user_name=@UserName)  

                select   distinct   PM.ItemNo,PIP.@Price   as   Price07   from     PUBItemMaster   as   PM
      inner   join   PUBItemPrice   as   PIP   on   PM.ItemNo=PIP.ItemNO
end  
@Price   的值是PIP表中的一个列名称,现在怎么样让动态的@Price   和表名称PIP连接一起?比如PIP   表有字段Price     我们关联查询可以
写PIP.Price   现在PIP和@Price   联起来写?知道的指点一下。

------解决方案--------------------
可以声明一个字符串:declare @strsql varchar(200)
然后拼接出那个sql语句的字符串,最后exec(@strsql)执行以下即可

------解决方案--------------------
@strsql = "select distinct PM.ItemNo,PIP. " + @Price + " as Price07 from PUBItemMaster as PM
inner join PUBItemPrice as PIP on PM.ItemNo=PIP.ItemNO "

exec(@strsql)