如何使用临时表

野鼻孔 2016-04-13 11:14:55
大神们好,我想咨询下如何使用临时表,我的逻辑是要更新临时表中的一些字段,做报表,然后使用完再清楚临时表,不知道如何操作。

我的理解如果新建了个临时表之后,用了select语句,再drop临时表,那么select语句显示的数据还会有吗?
求更正代码: 我用了With取了一些数据,然后select with的数据


select * into #Temp_DiaryNotes from A_Diary_Notes
update #Temp_DiaryNotes set Diary_Note = REPLACE(Diary_Note,'"','''''')
WITH TA14 as (select Application_Number,Max(diary_date + Diary_Time) as detailtime,Diary_Note from #Temp_DiaryNotes(nolock)
where Diary_Type='M' and Diary_Note not like 'Reassign application%' and (User_Id in (select User_Id from Security where Profile_Name='FRAUD_REVIEWER')
or User_Id in (select User_Id from Security where Profile_Name='FRAUD_MANAGER')) group by Application_Number,Diary_Note)
select * from TA14
drop table #Temp_DiaryNotes

WIth的数据为
Application_Number detailtime Diary_Note
1000177264 2016-04-12 00:00:40.000 看看你
1000182136 2016-04-12 23:26:25.000 看看我
1000182136 2016-04-12 23:25:01.000 不让看
1000177264 2016-04-12 00:01:49.000 看看他

求大神们帮帮忙
...全文
285 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongsheng10101 2016-05-18
  • 打赏
  • 举报
回复
首先:select语句之后,再drop临时表,之前select数据集还是存在的。 其次说说个人使用临时表的思路: 1、进入模块时:先创建临时表; 2、在模块中,设置好查询条件等,点“查询”等相关按扭时, A、先判断临时表是否存在,不存在就创建;存在就清空临时表内的数据。 B、然后把你所要的数据都插入这个临时表中,当然中间你还可以对这个临时表中的数据进行一些操作update、delete等动作。 C、最后把你想要的数据,通过select语句把数据集返回前台就可以。 3、退出模块时:删除此临时表。
jmcwowprime 2016-05-17
  • 打赏
  • 举报
回复
先sqlect 然后drop掉临时表就OK了
野鼻孔 2016-04-15
  • 打赏
  • 举报
回复
引用 7 楼 ap0405140 的回复:
请问为什么需要用到临时表和动态SQL? 直接update不就行了.

update A_Diary_Notes set Diary_Note=replace('"', '''''')
生产服务器上不能直接update,需要建临时表。。
唐诗三百首 2016-04-15
  • 打赏
  • 举报
回复
引用 8 楼 yebikong222 的回复:
生产服务器上不能直接update,需要建临时表。。
即使用了临时表,最终还是要update的是吧,请问为何需要用到动态SQL? 建议不要把问题复杂化了,其实1行SQL就可以搞定的,就不要搞一大堆代码了.
xiaoxiangqing 2016-04-15
  • 打赏
  • 举报
回复
生成报表最适合用临时表,因为临时表没有并发的问题
LongRui888 2016-04-15
  • 打赏
  • 举报
回复
一般来说之所以要用临时表,主要的问题是 sql比较复杂,然后拆分sql,把每个sql的查询结果插入到临时表,最后在关联所有这些临时表 select出数据,可以提高查询的速度。
唐诗三百首 2016-04-14
  • 打赏
  • 举报
回复
请问为什么需要用到临时表和动态SQL? 直接update不就行了.

update A_Diary_Notes set Diary_Note=replace('"', '''''')
野鼻孔 2016-04-14
  • 打赏
  • 举报
回复
引用 3 楼 wmxcn2000 的回复:
如果你是先select 操作,再drop ,原来的数据还有的; 你可以给出一些具体的数据,好方便给你调试,最好是脚本形式的;
提供测试数据,表 A_Diary_Notes 只有一列 为 Diary_Note Diary_Note Reassign application ''1000182129'' to team "FRAUDGROUP" (Even Distribution) Reassign application ''0728935'' to user ''user1'' Reassign application ''1000177264'' to user ''430024'' Reassign application ''1000182142'' to team ''FRAUDGROUP'' Reassign application ''1000182176'' to team '‘FRAUDGROUP'' (Random Distribution) Reassign application ''1000182104'' to team ''FRAUDGROUP'' Reassign application ''1000182110'' to team ''FRAUDGROUP'' Reassign application ''1000182117'' to team "FRAUDGROUP" (Even Distribution) 我要把Diary_Note列种所有的" 符号,英文双引号,替换成 两个英文单引号 ' ' declare @sql nvarchar(max) declare @where nvarchar(max) set @where='select * into #Temp_DiaryNotes from A_Diary_Notes update #Temp_DiaryNotes set Diary_Note = REPLACE(Diary_Note,'"','''''')';----此处 Replace为 diary_Note, ' " ', ' ' ' ' ' ' set @sql = 'WITH TA14 as (select Diary_Note from #Temp_DiaryNotes where Diary_Note like 'Reassign%') select * from TA14 drop table #Temp_DiaryNotes' set @sql = @sql + @where exec(@sql) 求大神们帮助,卡了1天多了。这个双引号替换为单引号的问题。
野鼻孔 2016-04-14
  • 打赏
  • 举报
回复
declare @sql nvarchar(max) declare @where nvarchar(max) declare @where1 nvarchar(max) set @where='select * into #Temp_DiaryNotes from A_Diary_Notes'; set @sql='update #Temp_DiaryNotes set Diary_Note = REPLACE(Diary_Note,'"','''''')'; set @sql = @sql + @where exec(@sql) 大神们好,我现在想update 表中的" 为 有个英文的冒号,我想替换为英文的' ' 两个单引号,求大神们帮助,可以新建个表,里面输入 " 英文冒号。。。把他替换为两个单引号' ' 求帮助啊
卖水果的net 2016-04-14
  • 打赏
  • 举报
回复
如果你是先select 操作,再drop ,原来的数据还有的; 你可以给出一些具体的数据,好方便给你调试,最好是脚本形式的;
Ginnnnnnnn 2016-04-14
  • 打赏
  • 举报
回复
你把临时表drop 掉了临时表就肯定消失嘛。其它的都没有影响
野鼻孔 2016-04-13
  • 打赏
  • 举报
回复
我用declare还是有问题' " ‘ 想update把" 变成' ' 但是即使变成' ' " ' '依旧会报错 declare @sql nvarchar(max) declare @where nvarchar(max) set @where='select * into #Temp_DiaryNotes from A_Diary_Notes update #Temp_DiaryNotes set Diary_Note = REPLACE(Diary_Note,'"','''''')'; set @sql= ' WITH TA14 as (select Application_Number,Max(diary_date + Diary_Time) as detailtime,Diary_Note from #Temp_DiaryNotes(nolock) where Diary_Type=''M'' and Diary_Note not like ''Reassign application%'' and (User_Id in (select User_Id from Security where Profile_Name=''FRAUD_REVIEWER'') or User_Id in (select User_Id from Security where Profile_Name=''FRAUD_MANAGER'')) group by Application_Number,Diary_Note) select * from TA14 drop table #Temp_DiaryNotes '; set @sql = @sql + @where exec(@sql)

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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