SQL Server 2005ÖеÄT SQLÔöÇ¿A
TOP ÔöÇ¿¡£¿ÉÒÔÖ¸¶¨Ò»¸öÊý×Ö±í´ïʽ£¬ÒÔ·µ»ØÒªÍ¨¹ý²éѯӰÏìµÄÐÐÊý»ò°Ù·Ö±È£¬»¹¿ÉÒÔ¸ù¾ÝÇé¿öʹÓñäÁ¿»ò×Ó²éѯ¡£
¿ÉÒÔÔÚDELETE¡¢UPDATEºÍINSERT²éѯÖÐʹÓÃTOPÑ¡Ïî¡£
2¡¢¸üºÃµØÌæ»»SET ROWCOUNTÑ¡Ïʹ֮¸üΪÓÐЧ¡£
OUTPUT
1¡¢SQL Server 2005ÒýÈëÒ»¸öеÄOUTPUT×Ӿ䣬ÒÔʹÄú¿ÉÒÔ³åÐÞ¸ÄÓï¾ä(INSERT¡¢UPDATE¡¢DELETE)Öн«Êý¾Ý·µ»Øµ½±í±äÁ¿ÖС£
2¡¢ÐµÄOUTPUT×Ó¾ÖµÄÓ﷨Ϊ£º
OUTPUT INTO @table_variable
¿ÉÒÔͨ¹ýÒýÓòåÈëµÄ±í»òɾ³ýµÄ±íÀ´·ÃÎʱ»Ð޸ĵÄÐеľÉ/ÐÂÓ°Ïó£¬Æä·½Ê½Óë·ÃÎÊ´¥·¢Æ÷ÀàËÆ¡£ÔÚINSERTÓï¾äÖУ¬Ö»ÄÜ·ÃÎʲåÈëµÄ±í¡£ÔÚDELETEÓï¾äÖУ¬Ö»ÄÜ·ÃÎÊɾ³ýµÄ±í¡£ÔÚUPDATEÓï¾äÖУ¬¿ÉÒÔ·ÃÎʲåÈëµÄ±íºÍɾ³ýµÄ±í¡£
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£©Ìí¼Ó/ɾ³ý³ÌÐòÖг¹µ×ɾ³ýsql server¡£
2£©½«Ã»ÓÐɾ³ýµÄsql serverĿ¼Ҳɾ³ýµô¡£
3£©´ò¿ª×¢²á±í±à¼Æ÷£¬ÔÚHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session ManagerÖÐÕÒµ½PendingFileRenameOperationsÏîÄ¿£¬²¢É¾³ýËü¡£ÕâÑù¾Í¿ÉÒÔÇå³ý°²×°ÔݹÒÏîÄ¿¡£
4£©É¾³ý×¢²á±íÖиúsql serverÏà¹ØµÄ¼ü¡ ......
Ò»¡¢ÒòÇéÖÆÒË£¬½¨Á¢“Êʵ±”µÄË÷Òý
½¨Á¢“Êʵ±”µÄË÷ÒýÊÇʵÏÖ²éѯÓÅ»¯µÄÊ×ҪǰÌá¡£
Ë÷Òý£¨index£©Êdzý±íÖ®ÍâÁíÒ»ÖØÒªµÄ¡¢Óû§¶¨ÒåµÄ´æ´¢ÔÚÎïÀí½éÖÊÉϵÄÊý¾Ý½á¹¹¡£µ±¸ù¾ÝË÷ÒýÂëµÄÖµËÑË÷Êý¾Ýʱ£¬Ë÷ÒýÌṩÁ˶ÔÊý¾ÝµÄ¿ìËÙ·ÃÎÊ¡£ÊÂʵÉÏ£¬Ã»ÓÐË÷Òý,Êý¾Ý¿âÒ²Äܸù¾ÝSELECTÓï¾ä³É¹¦µØ¼ìË÷µ½½á¹û£¬µ«Ëæ×űí±ä ......
SQL³£ÓÃÓï¾ä
SQLÓï¾äÏÈǰдµÄʱºò£¬ºÜÈÝÒ×°ÑÒ»Ð©ÌØÊâµÄÓ÷¨Íü¼Ç£¬ÎÒÌØ´ËÕûÀíÁËÒ»ÏÂSQLÓï¾ä²Ù×÷¡£
Ò»¡¢»ù´¡
1¡¢ËµÃ÷£º´´½¨Êý¾Ý¿â
CREATE DATABASE database-name
2¡¢ËµÃ÷£ºÉ¾³ýÊý¾Ý¿â
drop database dbname
3¡¢ËµÃ÷£º±¸·Ýsql server
--- ´´½¨ ±¸·ÝÊý¾ÝµÄ device
USE master
EXEC sp_addumpdevice 'disk ......
-- ²é¿´ORACLE Êý¾Ý¿âÖб¾Óû§ÏµÄËùÓбí
SELECT table_name from user_tables;
-- ²é¿´ORACLE Êý¾Ý¿âÖÐËùÓÐÓû§ÏµÄËùÓбí
select user,table_name from all_tables;
-- ²é¿´ORACLE Êý¾Ý¿âÖб¾Óû§ÏµÄËùÓÐÁÐ
select table_name,column_name from user_tab_columns;
-- ²é¿´ORACLE Êý¾Ý¿âÖб¾Óû§ÏµÄËùÓÐÁÐ
se ......
¿âλ »õÎï±àºÅ ¿â´æÊý
1 0101 50
1 0102 60
1 0103 50
2 0101 90
2 0103 100
2 0111 30
3 0101 12 ......