--创建表 CREATE TABLE TreeData (id INT IDENTITY(1,1) PRIMARY KEY , pid INT ) --为表添加列 ALTER TABLE treedata ADD createdate DATETIME DEFAULT GETDATE() --修改列名 EXEC sys.sp_rename @objname = N'treedata.NAME', -- nvarchar(1035) @newname = N'name', -- sysname @objtype = 'column' -- varchar(13)
SELECT * FROM treedata --插入值 --1 INSERT treedata(pid,name) VALUES (7,'test') --2 insert into 表(字段1,字段2) select 字段1,字段2 from 表 要求两个表都存在 INSERT INTO treedata(pid,name) SELECT pid,name FROM treedata --3 select 字段 into 表(不存在) from 表 SELECT * INTO TABLE3 FROM treedata --递归查询 WITH t AS ( SELECT id,pid,name FROM treedata WHERE id=1 UNION ALL SELECT tt.id,tt.pid,tt.name FROM treedata tt JOIN t on tt.pid=t.id) SELECT * FROM t
--while 循环语句的使用 DECLARE @day INT SET @day=10 WHILE (@day>0) BEGIN SET @day =@day-1 INSERT treedata VALUES (2,'wangyexin',GETDATE()) END
--临时表,前加#号 CREATE TABLE #test (id INT IDENTITY(1,1) PRIMARY KEY)
DROP TABLE #test
--表变量
DECLARE @test TABLE (id INT IDENTITY(1,1) PRIMARY KEY)