SQL Select N to M Records (single Table)
È¡±íÀïnµ½mÌõ¼Í¼µÄ¼¸ÖÖ·½·¨:
1. Ö»ÐèÒª²éѯǰMÌõÊý¾Ý(0 to M),
1.1 ʹÓà top(M) ·½·¨:
select top(3) * from [tablename]
1.2 ʹÓà set rowcount ·½·¨:
http://msdn.microsoft.com/zh-cn/library/ms188774(SQL.90).aspx
set rowcount M
select * from [tablename]
set rowcount 0
ȨÏÞ ÒªÇó¾ßÓÐ public ½ÇÉ«³ÉÔ±×ʸñ¡£
ÒªÖ´ÐÐset rowcount 0, ·ñÔòÓ°ÏìÒÔºó²éѯµÈ.
2.²éѯNµ½MÌõÊý¾Ý(N to M),
2.1 ±íÀïÃæÓбêʶÁÐ
2.1.1
select top (M-N+1) * from [tablename] where [columnname] not in (select top (N) [columnname] from [tablename])
2.1.2 ÄæÐòÏÔʾ
select top N * from (select top M * from [tablename] order by [columnname]) temp order by [columnname] desc
2.1.3 ˳ÐòÏÔʾ
select * from (select top N * from (select top M * from [tablename] order by [columnname]) temp1 order by [columnname] desc) temp2 order by [columnname]
2.2 ±íÀïÓÐidentityÊôÐÔ
select * from [tablename] where identitycol between N and M
Èç[columnname]ΪidentityÊôÐÔ,Ôò¿ÉÒÔд³É:
select * from [tablename] where [columnname] between N and M
2.3 ±íÀïÃæÓбêʶÁÐ, ÀûÓÃÁÙʱ±í
IF Exists(Select 1 from sysObjects Where Name ='temptable' And Type In ('temptable','U'))
begin
drop table [temptable]
end
select top M * into [temptable] from [tablename] order by [columnname]
set rowcount N
select * from [temptable] order by [columnname] desc
set rowcount 0
drop table [temptable]
2.4 Èç¹ûtablenameÀïûÓÐÆäËûidentityÁУ¬ÄÇô£º
exec sp_dboption [DataBaseName] ,'select into/bulkcopy',true
IF Exists(Select 1 from sysObjects Where Name ='temptable' And Type In ('temptable','U'))
begin
drop table temptable
end
select identity(int) id0,* into [temptable] from [tablename]
select * from temp where id0 >= N and id0 <= M
drop table temptable
Èç¹ûÄãÔÚÖ´ÐÐselect identity(int) id0,* into [temp
Ïà¹ØÎĵµ£º
Äãд¹ýÒ»ÌõsqlÓï¾äÀ´ÐÞ¸ÄÁ½¸ö±íµÄÊý¾ÝÂð£¿
UPDATE test.table1 t1,test.table2 t2 SET t1.aa='a',t1.bb='b',t2.cc='c',WHERE t1.u_id=t2.u_id AND t1.u_id='1' £»
table1µÄu_idºÍtable2µÄu_idÊÇÖ÷Íâ¼ü¹ØÏµ ......
Tree±íÈçÏÂ:
NodeId ParentId NodeName
0 -1 &nb ......
I'm continually trying to track down what service packs are installed on various SQL Servers I support. I can never find the right support page on Microsoft's site. So here's an article with all the SQL Server version information I can track down. If you know of any older versions or can help me fil ......
ÔÚSQL ServerÖУ¬Èç¹û°Ñ±íµÄÖ÷¼üÉèΪidentityÀàÐÍ£¬Êý¾Ý¿â¾Í»á×Ô¶¯ÎªÖ÷¼ü¸³Öµ¡£ÀýÈ磺
create table customers (
id int identity(1,1) primary key not null,
name varchar(15)
);
insert into customers(name) values("name1"),("name2");
select id from customers;
²éѯ½á¹ûΪ£º
id
---
1
2
ÓÉ´Ë¿ ......
×î½üÕûÀí³öÀ´µÄ.Èç¹û²»ÍêÈ«µÄ»°Ï£Íû´ó¼Ò²¹³ä.
ÔÚaccessÖУ¬×ª»»Îª´óдµÄsqlº¯ÊýÊÇucase£¬ÔÚsqlserverÖУ¬×ª»»Îª´óдµÄº¯ÊýÊÇupper£»ÔÚaccessÖУ¬×ª»»ÎªÐ¡Ð´µÄº¯ÊýÊÇlcase£¬ÔÚsqlserverÖУ¬×ª»»ÎªÐ¡Ð´µÄº¯ÊýÊÇlower£»ÔÚaccessÖУ¬È¡µ±Ç°Ê±¼äµÄº¯ÊýÊÇnow£¬ÁíÍ⻹ÓÐÒ»¸öÈ¡ÈÕÆÚº¯Êýdate£¬ÔÚsqlserverÖУ¬È¡µ±Ç°µÄº¯ÊýÊÇgetdate ......