SQL SERVER创建临时表的方式:
create table #tmp(name varchar(255),id int)
注意两点:
1.int不能写成int(10)来限制长度,否则就会报错。(不确定原因)
2.临时表的名称必须以#开头
3.IF OBJECT_ID(‘tempdb…#student’ ) IS NOT NULL DROP TABLE #student;
必须写,否则下次运行则报错,因为库里已经存在一个临时表 不能继续创建。
相当于mysql 中的 drop table if exsits student
IF OBJECT_ID('tempdb..#student' ) IS NOT NULL DROP TABLE #student;
CREATE TABLE ##student(xh INT,bj CHAR(10),cj INT);
INSERT INTO #student VALUES (20001 ,'一班', 510);
INSERT INTO #student VALUES (20002 ,'二班', 630);
INSERT INTO #student VALUES (20003 ,'二班', 200);
INSERT INTO #student VALUES (20004 ,'三班', 350);
INSERT INTO #student VALUES (20005 ,'一班', 500);
INSERT INTO #student VALUES (20006 ,'二班', 150);
INSERT INTO #student VALUES (20007 ,'三班', 140);
INSERT INTO #student VALUES (20008 ,'三班', 140);
INSERT INTO #student VALUES (20009 ,'三班', 120);
select xh,bj,cj from #student