带变量的存储过程

明珠佩佩 2008-12-31 10:09:51
可否帮我检查下原因,为什么这样写不行呢?

declare @sql varchar(8000)
declare @sqlCondition varchar(8000)

set @sqlCondition = 'ProjectTypeCode=''03'' and '

set @sql = 'select a.DrawingName, a.DrawingNum '
select @sql = @sql + ', max(case b.ProjectName when ''' + ProjectName + ''' then PlanSendDate end) [' + ProjectName + '计划下发]'
+ ', max(case b.ProjectName when ''' + ProjectName + ''' then InfactSendDate end) [' + ProjectName + '实际下发]'
from (select ProjectName from BM_Project where @sqlCondition LogoutFlag='0') as t
set @sql = @sql + ' from VIEW_BM_DrawingContentToPlanTrack a , BM_Project b '
set @sql = @sql + ' where a.ProjectTypeCode=''03'' and a.ShipOwnerCode=''05'' and a.ProjectCode=''01'' and a.ProjectTypeCode=b.ProjectTypeCode and a.ProjectCode = b.ProjectCode group by a.DrawingName, a.DrawingNum'
print(@sql)

变量代入后没有什么反映啊,也不报错
...全文
83 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
chuifengde 2008-12-31
  • 打赏
  • 举报
回复
set @sql = @sql + ' ,自增字段=identity(int,1,1) into 临时表 from VIEW_BM_DrawingContentToPlanTrack a , BM_Project b '
set @sql = @sql + ' where a.ProjectTypeCode=''03'' and a.ShipOwnerCode=''05'' and a.ProjectCode=''01'' and a.ProjectTypeCode=b.ProjectTypeCode and a.ProjectCode = b.ProjectCode group by a.DrawingName, a.DrawingNum'
print(@sql)

select * from 临时表

drop table 临时表
明珠佩佩 2008-12-31
  • 打赏
  • 举报
回复
如果我需要在结果中增加一列自增字段呢,应该怎么改啊
chuifengde 2008-12-31
  • 打赏
  • 举报
回复
declare @sql varchar(8000) 
declare @sqlCondition varchar(8000)

CREATE TABLE #(ProjectName varchar(100))

set @sqlCondition = 'ProjectTypeCode=''03'' and '

INSERT # exec('select ProjectName from BM_Project where '+@sqlCondition+' LogoutFlag=''0''')

set @sql = 'select a.DrawingName, a.DrawingNum '
select @sql = @sql + ', max(case b.ProjectName when ''' + ProjectName + ''' then PlanSendDate end) [' + ProjectName + '计划下发]'
+ ', max(case b.ProjectName when ''' + ProjectName + ''' then InfactSendDate end) [' + ProjectName + '实际下发]'
from # as t
set @sql = @sql + ' from VIEW_BM_DrawingContentToPlanTrack a , BM_Project b '
set @sql = @sql + ' where a.ProjectTypeCode=''03'' and a.ShipOwnerCode=''05'' and a.ProjectCode=''01'' and a.ProjectTypeCode=b.ProjectTypeCode and a.ProjectCode = b.ProjectCode group by a.DrawingName, a.DrawingNum'
print(@sql)

DROP TABLE #

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧