首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

SQL 有关问题

2012-01-08 
SQL 问题! CREATEprocMainProductInfo(@UserNamevarchar(50),@U_typevarchar(10))asdeclare@Pricevarchar(

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)

热点排行