ʹÓÃTranact-SQL ±àд´úÂëÀ´´´½¨Ò»¸öÐÂ±í£º
USE [OnlineJudge]
GO
/****** Object: Table [dbo].[User1] Script Date: 05/17/2010 14:05:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[User2](/*notice convert the tablename*/
[num] [int]IDENTITY(1,1) NOT NULL,
[userid] [nchar](10) NOT NULL,
[password] [nchar](10) NOT NULL,
[authority] [int] NOT NULL,
[logintime] [datetime] NOT NULL,
CONSTRAINT [PK_User2] PRIMARY KEY CLUSTERED /*notice convert the tablename*/
(
[num] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
......
µÚÒ»ÖÖ£º
select b.* from
( select a.*, rownum row_num from
(select t.* from A05_ORGANIZATION t order by org_name_en asc) a
) b
where b.row_num between 1 and 5 order by b.row_num asc
µÚ¶þÖÖ£¨¸ü¸ßЧ£©£º
select b.* from
( select a.*, rownum row_num from
(select t.* from A05_ORGANIZATION t order by org_name_en asc) a
where rownum <=5
) b
where b.row_num >=1 order by b.row_num asc ......
µÚÒ»ÖÖ£º
select b.* from
( select a.*, rownum row_num from
(select t.* from A05_ORGANIZATION t order by org_name_en asc) a
) b
where b.row_num between 1 and 5 order by b.row_num asc
µÚ¶þÖÖ£¨¸ü¸ßЧ£©£º
select b.* from
( select a.*, rownum row_num from
(select t.* from A05_ORGANIZATION t order by org_name_en asc) a
where rownum <=5
) b
where b.row_num >=1 order by b.row_num asc ......
Èç¹ûÔÚSQL Server ÀïÐèÒª¶¨Ê±»òÕßÿ¸ôÒ»¶Îʱ¼äÖ´ÐÐij¸ö´æ´¢¹ý³Ì»ò3200×Ö·ûÒÔÄÚµÄSQLÓï¾äʱ, ¿ÉÒÔÓùÜÀí->SQL Server´úÀí->×÷ÒµÀ´ÊµÏÖ¡£
¡¡¡¡1¡¢¹ÜÀí->SQL Server´úÀí->×÷Òµ(°´Êó±êÓÒ¼ü)->н¨×÷Òµ
¡¡¡¡2¡¢Ð½¨×÷ÒµÊôÐÔ(³£¹æ)->Ãû³Æ[×Ô¶¨Òå±¾´Î×÷ÒµµÄÃû³Æ]->ÆôÓõķ½¿òÄÚÊǹ´ºÅ->
·ÖÀà´¦¿ÉÑ¡ÔñÒ²¿ÉÓÃĬÈϵÄ[δ·ÖÀà(±¾µØ)]->ËùÓÐÕßĬÈÏΪµÇ¼SQL ServerÓû§[Ò²¿ÉÑ¡ÆäËüµÄµÇ¼]->ÃèÊö[Ìîд±¾´Î¹¤×÷ÏêϸÃèÊöÄÚÈÝ]£»
[ ´´½¨×÷Òµ·ÖÀàµÄ²½Öè: SQL Server´úÀí->×÷Òµ->ÓÒ¼üÑ¡ËùÓÐÈÎÎñ->Ìí¼Ó¡¢Ð޸ġ¢É¾³ý ]
¡¡¡¡3¡¢Ð½¨×÷ÒµÊôÐÔ(²½Öè)->н¨->²½ÖèÃû[×Ô¶¨ÒåµÚÒ»²½ÖèÃû³Æ]->ÀàÐÍ[Transact-SQL(TSQL)½Å±¾]->Êý¾Ý¿â[Òª²Ù×÷µÄÊý¾Ý¿â]->ÃüÁî[ Èç¹ûÊǼòµ¥µÄSQLÖ±½Óд½øÈ¥¼´¿É£¬Ò²¿ÉÓôò¿ª°´Å¥ÊäÈëÒ»¸öÒÑдºÃµÄ*.sqlÎļþ£»Èç¹ûÒªÖ´Ðд洢¹ý³Ì£¬Ìî
exec p_procedure_name v_parameter1,[ v_parameter2…v_parameterN]¡¡]
->È·¶¨(Èç¹ûÓжà¸ö²½Ö裬¿ÉÒÔÔٴε÷ÓÃÏÂÃæµÄн¨°´Å¥£»Ò²¿ÉÒÔ¶ÔÒÑÓеĶà¸ö²½Öè²åÈë¡¢±à¼¡¢É¾³ý)£»
¡¡¡¡4¡¢½¨×÷ÒµÊôÐÔ(µ÷¶È)->н¨µ÷¶È->Ãû³Æ[×Ô¶¨Òåµ÷¶ÈÃû³Æ]->ÆôÓõķ½¿òÄÚÊǹ´ºÅ ......
´´½¨´æ´¢¹ý³Ì֮ǰҪÏÈÓÃuseÓï¾äÉùÃ÷Òª½«´æ´¢¹ý³Ì´æ´¢ÔÚÄĸöÊý¾Ý¿âÖÐ. e.g use company; Ö´ÐÐÖ®.Ö®ºó¾Í¿ÉÒÔÉùÃ÷´æ´¢¹ý³ÌÁË. e.g create procedure *** @id varchar(50) , @name int output as select @name=name from table where id=@id È»ºó¿ÉÒÔÓÃalter¶Ô´æ´¢¹ý³Ì½øÐÐÐÞ¸Ä. declare¿ÉÒÔÉùÃ÷Ò»¸ö±äÁ¿ ÓÃset¶Ô±äÁ¿¸³Öµ e.g alter proc getempp @ID int
as
declare @Department int
select @Department=a.Department
from company.dbo.employee a
inner join company.dbo.department b
on a.Department=b.ID
where a.ID=@ID
if @@ROWCOUNT>0
begin
select *
from company.dbo.department
where ID=@Department
select * from company.dbo.employee
where Department=@Department
end
else
begin
RAISERROR('No record found',10,1)
end ÉÏÀýÖеÄ@DepartmentÊDZ»Ñ¡Ôñ³öÀ´µÄÖµ,È»ºóÓÃÀ´ÎªÏÂÃæµÄifÓï¾ä¿éÖеIJéѯ·þÎñµÄ. ......
1. GROUP BY ÊÇ·Ö×é²éѯ, Ò»°ã GROUP BY ÊǺ;ۺϺ¯ÊýÅäºÏʹÓÃ
group by ÓÐÒ»¸öÔÔò,¾ÍÊÇ select ºóÃæµÄËùÓÐÁÐÖÐ,ûÓÐʹÓþۺϺ¯ÊýµÄÁÐ,±ØÐë³öÏÖÔÚ group by ºóÃæ£¨ÖØÒª£©
ÀýÈç,ÓÐÈçÏÂÊý¾Ý¿â±í£º
A B
1 abc
1 bcd
1 asdfg
Èç¹ûÓÐÈçϲéѯÓï¾ä£¨¸ÃÓï¾äÊÇ´íÎóµÄ£¬ÔÒò¼ûÇ°ÃæµÄÔÔò£©
select A,B from table group by A
¸Ã²éѯÓï¾äµÄÒâͼÊÇÏëµÃµ½ÈçϽá¹û(µ±È»Ö»ÊÇÒ»ÏàÇéÔ¸)
A B
abc
1 bcd
asdfg
ÓÒ±ß3ÌõÈçºÎ±ä³ÉÒ»Ìõ,ËùÒÔÐèÒªÓõ½¾ÛºÏº¯Êý£¬ÈçÏÂ(ÏÂÃæÊÇÕýÈ·µÄд·¨):
select A,count(B) as ÊýÁ¿ from table group by A
ÕâÑùµÄ½á¹û¾ÍÊÇ
A ÊýÁ¿
1 3
2. Having
where ×Ó¾äµÄ×÷ÓÃÊÇÔÚ¶Ô²éѯ½á¹û½øÐзÖ×éǰ£¬½«²»·ûºÏwhereÌõ¼þµÄÐÐÈ¥µô£¬¼´ÔÚ·Ö×é֮ǰ¹ýÂËÊý¾Ý£¬Ìõ¼þÖв»Äܰüº¬¾Û×麯Êý£¬Ê¹ÓÃwhereÌõ¼þÏÔÊ¾ÌØ¶¨µÄÐС£
having ×Ó¾äµÄ×÷ÓÃÊÇɸѡÂú×ãÌõ¼þµÄ×飬¼´ÔÚ·Ö×éÖ®ºó¹ýÂËÊý¾Ý£¬Ìõ¼þÖ ......
ÉùÃ÷×Ö¶ÎÓ³Éä
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FiledRef
{
String fieldName();
}
ÉùÃ÷±íÓ³Éä
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TableRef
{
String tableName();
}
²âÊÔÀà
@TableRef(tableName = "MobDevice")
public class ReflectTest
{
@FiledRef(fieldName = "ID")
private Long id;
@FiledRef(fieldName = "DEVICE_NAME")
private String name;
@FiledRef(fieldName = "DEVICE_CODE")
private String code;
public ReflectTest(Long id, String name, String code)
{
this.id = id;
this.name = name;
this.code = code;
}
&nbs ......
ÉùÃ÷×Ö¶ÎÓ³Éä
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FiledRef
{
String fieldName();
}
ÉùÃ÷±íÓ³Éä
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TableRef
{
String tableName();
}
²âÊÔÀà
@TableRef(tableName = "MobDevice")
public class ReflectTest
{
@FiledRef(fieldName = "ID")
private Long id;
@FiledRef(fieldName = "DEVICE_NAME")
private String name;
@FiledRef(fieldName = "DEVICE_CODE")
private String code;
public ReflectTest(Long id, String name, String code)
{
this.id = id;
this.name = name;
this.code = code;
}
&nbs ......