...
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..##Temp2_2_2') and type='U')
drop table ##Temp2_2_2
...if @MonthCount=1
begin
SELECT B.车队,
sum(经济损失)/10000 as 经损
into ##Temp2_2_2
FROM [事故记录表] A left join 车辆信息表 B on A.车号=B.车号
WHERE YEAR(A.报赔日)=@Year AND MONTH(A.报赔日)>=@FromMonth And MONTH(A.报赔日)<=@ToMonth
and (A.车损>0 or A.第三者损失>2000)
group by B.车队
...
endelse
beginSELECT B.车队,
sum(经济损失)/10000 as 经损
into ##Temp2_2_2
FROM [事故记录表] A left join 车辆信息表 B on A.车号=B.车号
WHERE YEAR(A.报赔日)=@Year AND MONTH(A.报赔日)>=@FromMonth And MONTH(A.报赔日)<=@ToMonth
--- and (A.车损>0 or A.第三者损失>2000) 就这句没有,其他与上面语句块一模一样
group by B.车队
...
end
执行时出错:
服务器: 消息 2714,级别 16,状态 1,行 176
数据库中已存在名为 '##Temp2_2_2' 的对象。
好像if语句块没有设一样,好像两个块都执行了。郁闷
查了帮助:
Transact-SQL 语句或用语句块定义的语句分组。除非使用语句块,否则 IF 或 ELSE 条件只能影响一个 Transact-SQL 语句的性能。若要定义语句块,请使用控制流关键字 BEGIN 和 END。如果在 IF...ELSE 块的 IF 区和 ELSE 区都使用了 CREATE TABLE 语句或 SELECT INTO 语句,那么 CREATE TABLE 语句或 SELECT INTO 语句必须指向是相同的表名。
这句真无法理解
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..##Temp2_2_2') and type='U')
drop table ##Temp2_2_2
...if @MonthCount=1
begin
SELECT B.车队,
sum(经济损失)/10000 as 经损
into ##Temp2_2_2
FROM [事故记录表] A left join 车辆信息表 B on A.车号=B.车号
WHERE YEAR(A.报赔日)=@Year AND MONTH(A.报赔日)>=@FromMonth And MONTH(A.报赔日)<=@ToMonth
and (A.车损>0 or A.第三者损失>2000)
group by B.车队
...
endelse
beginSELECT B.车队,
sum(经济损失)/10000 as 经损
into ##Temp2_2_2
FROM [事故记录表] A left join 车辆信息表 B on A.车号=B.车号
WHERE YEAR(A.报赔日)=@Year AND MONTH(A.报赔日)>=@FromMonth And MONTH(A.报赔日)<=@ToMonth
--- and (A.车损>0 or A.第三者损失>2000) 就这句没有,其他与上面语句块一模一样
group by B.车队
...
end
执行时出错:
服务器: 消息 2714,级别 16,状态 1,行 176
数据库中已存在名为 '##Temp2_2_2' 的对象。
好像if语句块没有设一样,好像两个块都执行了。郁闷
查了帮助:
Transact-SQL 语句或用语句块定义的语句分组。除非使用语句块,否则 IF 或 ELSE 条件只能影响一个 Transact-SQL 语句的性能。若要定义语句块,请使用控制流关键字 BEGIN 和 END。如果在 IF...ELSE 块的 IF 区和 ELSE 区都使用了 CREATE TABLE 语句或 SELECT INTO 语句,那么 CREATE TABLE 语句或 SELECT INTO 语句必须指向是相同的表名。
这句真无法理解
drop table tempdb..##Temp2_2_2
go
exec ('select * into #t from (select 1 as ggg)t')
exec ('select * into #t from (select 1 as ggg)t')
exec ('select * into #t from (select 1 as ggg)t')
exec ('select * into #t from (select 1 as ggg)t')
exec ('select * into #t from (select 1 as ggg)t')不会冲突的,你不需要在开始的时候drop
drop table tempdb..##Temp2_2_2
go论坛卡死了
楼主被卡贴了
-- =============================================
-- Create table basic template
-- =============================================
IF EXISTS(SELECT name
FROM sysobjects
WHERE name = N'<table_name, sysname, test_table>'
AND type = 'U')
DROP TABLE <table_name, sysname, test_table>
GOCREATE TABLE <table_name, sysname, test_table> (
<column_1, sysname, c1> <datatype_for_column_1, , int> NULL,
<column_2, sysname, c2> <datatype_for_column_2, , int> NOT NULL)
GO这是模版类型,
Transact-SQL 语句或用语句块定义的语句分组。除非使用语句块,否则 IF 或 ELSE 条件只能影响一个 Transact-SQL 语句的性能。若要定义语句块,请使用控制流关键字 BEGIN 和 END。如果在 IF...ELSE 块的 IF 区和 ELSE 区都使用了 CREATE TABLE 语句或 SELECT INTO 语句,那么 CREATE TABLE 语句或 SELECT INTO 语句必须指向是相同的表名。你改成INSERT ##Temp2_2_2 SELECT ....试试
if exists (select * from tempdb..sysobjects where name='##Temp2_2_2'and type='U')
drop table ##Temp2_2_2
2、在分支中不能再次向同一个临时表插入(into)数据,否则会报错
3、建议楼主,先create好所需表结构,然后用insert into表进行插入,这样应该就不会报错了!
use dbcreate table tb(id int)insert into tb
select 1select * into #tb1 from tb
go
select * from tempdb.dbo.sysobjects where xtype='U'
select name from tempdb.dbo.sysobjects where xtype='U'
if exists (select * from tempdb.dbo.sysobjects where xtype='U' and id=object_id('#tb1'))
begin
select '----'
drop table #tb1
end
else
select * from #tb1
drop table #tb1
drop table tb/*
#15BB0E23 364580387 U 1 0 0 0 0 0 2009-08-14 13:12:14.077 0 0
#tb1________________________________________________________________________________________________________________000000000023 1052582838 U 1 0 0 0 0 0 2009-08-14 13:12:40.343 0 0
#614745E4 1632060900 U 1 0 0 0 0 0 2009-08-14 13:03:18.560 0 0#15BB0E23
#tb1________________________________________________________________________________________________________________000000000023
#614745E4`*/
表名都不一样!