WEB开发网
开发学院数据库MSSQL Server 谈SQL Server 2005中的T-SQL增强 阅读

谈SQL Server 2005中的T-SQL增强

 2007-05-18 09:39:14 来源:WEB开发网   
核心提示: 3、代码演示USE demoGOCREATE TABLE tt(id INT IDENTITY,c1 VARCHAR(15))GOINSERT INTO tt VALUES ('r1')INSERT INTO tt VALUES ('r2')INSERT I

3、代码演示

USE demo
GO
CREATE TABLE tt
(
id INT IDENTITY,
c1 VARCHAR(15)
)
GO
INSERT INTO tt VALUES ('r1')
INSERT INTO tt VALUES ('r2')
INSERT INTO tt VALUES ('r5')
INSERT INTO tt VALUES ('r6')
INSERT INTO tt VALUES ('r7')
INSERT INTO tt VALUES ('r8')
INSERT INTO tt VALUES ('r9')
INSERT INTO tt VALUES ('r10')
DECLARE @del AS TABLE (deletedId INT, deletedValue VARCHAR(15))
DELETE tt
OUTPUT DELETED.id, DELETED.c1 INTO @del
WHERE id < 3
SELECT * FROM @del
GO
-----------------------------------------------
USE demo
GO
CREATE TABLE toptest (column1 VARCHAR(150))
GO
INSERT INTO toptest VALUES('t1')
INSERT INTO toptest VALUES('t2')
INSERT INTO toptest VALUES('t3')
INSERT INTO toptest VALUES('t4')
INSERT INTO toptest VALUES('t5')
INSERT INTO toptest VALUES('t6')
INSERT INTO toptest VALUES('t7')
INSERT INTO toptest VALUES('t8')
SELECT * FROM toptest
GO
CREATE TABLE toptest2 (column2 VARCHAR(150))
GO
INSERT INTO toptest2 VALUES('c1')
INSERT INTO toptest2 VALUES('c2')
--声明3个变量
DECLARE @a INT
DECLARE @b INT
DECLARE @c INT
--赋值
SET @a = 10
SET @b = 5
SELECT @c = @a/@b
--使用计算表达式
SELECT TOP(@c) * FROM toptest
--使用SELECT语句作为条件
SELECT TOP(SELECT COUNT(*) FROM toptest2) *
FROM toptest
--指出top
DELETE TOP(2) toptest where column1>'t6'
--更新top
UPDATE TOP(2) toptest SET column1 = 'hi' where column1<='t2'
SELECT * FROM toptest

上一页  1 2 3 4 5 6 7  下一页

Tags:SQL Server SQL

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接