求教sql server 里查询动态表名
使用的是 sql server
数据库里面有这些表名:
GoodsSale201319
GoodsSale201310
GoodsSale201311
GoodsSale201312
后面是年月
这个月是2013年11月,那么我语句中要查询这个GoodsSale201311表;到下个月12号查询GoodsSale201312.
现在的语句是:
select GoodsSale201310.PluCode,goods.pluname,goods.barcode,vendor.vendorcode,vendor.vendorname,
subshop.orgcode,subshop.orgname,GoodsSale201310.Counts,cast(GoodsSale201310.AccDate as datetime) as AccDate,GoodsSale201310.Amount,
GoodsSale201310.PriceDsc,GoodsSale201310.Cost from GoodsSale201310
inner join goods on GoodsSale201310.plucode = goods.plucode left outer join subshop on GoodsSale201310.orgcode = subshop.orgcode
left outer join vendor on vendor.vendorcode = GoodsSale201310.vendorcode
declare @sql nvarchar(4000)
declare @str nvarchar(6)
set @str = convert(nvarchar(6),getdate(),112)
set @sql = '
select GoodsSale201310.PluCode,goods.pluname,goods.barcode,
vendor.vendorcode,vendor.vendorname,
subshop.orgcode,subshop.orgname,
GoodsSale201310.Counts,
cast(GoodsSale201310.AccDate as datetime) as AccDate,
GoodsSale201310.Amount,
GoodsSale201310.PriceDsc,GoodsSale201310.Cost
from GoodsSale201310
inner join goods
on GoodsSale201310.plucode = goods.plucode
left outer join subshop
on GoodsSale201310.orgcode = subshop.orgcode
left outer join vendor
on vendor.vendorcode = GoodsSale201310.vendorcode'
set @sql = replace(@sql,'201310',@str)
--select @sql
exec(@sql)