SQL SERVER 2005 基本查询(连接查询)
use AdventureWorks
GO
SELECT c.LastName from Person.Contact c;
SELECT * from HumanResources.Employee e
INNER JOIN HumanResources.Employee m
ON e.ManagerID = m.EmployeeID; n
SELECT ProductID,Name,ProductNumber,ReorderPoint
from Production.Product
where ProductID in( select ProductID from Production.Product where ProductID in (1,2,3) )
if EXISTS( select ProductID from Production.Product where ProductID in (1,2,3) )
PRINT 'TRUE'
ELSE
PRINT 'FALSE'
SELECT e.EmployeeID,ce.FirstName,ce.LastName
from HumanResources.Employee e
INNER JOIN HumanResources.Employee m
ON e.ManagerID = m.EmployeeID
INNER JOIN Person.Contact ce
ON e.ContactID = ce.ContactID
INNER JOIN Person.Contact cm
ON m.ContactID = cm.ContactID
WHERE cm.FirstName = 'Jo' and cm.LastName = 'Brown';
Select SalesOrderID,SUM(OrderQty) as sum
from Sales.SalesOrderDetail
where SalesOrderID between 43684 and 43686
group by SalesOrderID
having SUM(OrderQty) > 5
create table UserInfor
(
UserID int not null,
FirstName nvarchar(50),
LastName nvarchar(50)
)
GO
ALTER TABLE UserInfor
add primary key (UserID)
INSERT INTO UserInfor(UserID,FirstName,LastName)
SELECT e.EmployeeID,ce.FirstName,ce.LastName
from HumanResources.Employee e
INNER JOIN HumanResources.Employee m
ON e.ManagerID = m.EmployeeID
INNER JOIN Person.Contact ce
ON e.ContactID = ce.ContactID
INNER JOIN Person.Contact cm
ON m.ContactID = cm.ContactID
WHERE cm.FirstName = 'Jo' and cm.LastName = 'Brown';
declare @MyTable table
(
SalesOrderID int,
CustomerID int
)
INSERT INTO @MyTable(SalesOrderID,CustomerID)
select SalesOrderID,CustomerID
from AdventureWorks.Sales.SalesOrderHeader
where SalesOrderId between 50222 and 50225
select * from @MyTable
create table Shippers
(
ShipperID int identity not null primary key,
ShipperName varchar(30) not null,
Address varchar(20) not null,
相关文档:
(18)用EXISTS替换DISTINCT:
当提交一个包含一对多表信息(比如部门表和雇员表)的查询时,避免在SELECT子句中使用DISTINCT。一般可以考虑用EXIST替换, EXISTS 使查询更为迅速,因为RDBMS核心模块将在子查询的条件一旦满足后,立刻返回结果。例子:
(低效):
SELECT DISTINCT DEPT_NO,DEPT_NAME  ......
在T-SQL中监视进程
DBA更愿意使用T-SQL的原因是可以比“活动监视器”更加灵活地获得信息。
1. sp_who和sp_who2
存储过程sp_who也返回当前连接数据库实例,与“活动监视器”非常类似。然而,用户可能发现自己更愿意使用未公开说明的sp_who2存储过程,因为它提供 ......
1.如何创建数据库
CREATE DATABASE student
2.如何删除数据库
DROP DATABASE student
3.如何备份数据库到磁盘文件
BACKUP DATABASE student to disk=´c:S4.bak´
4.如何从磁盘文件还原数据库
RESTORE DATABASE studnet from DISK = ´c:S4.bak´
5.怎样创建表?
CREATE TABLE Students (
&n ......
--测试数据
create table table1(AID int,NAME nvarchar(20))
create table table2 (BID int,NUMBER nvarchar(20))
insert into table1 select 1,'Tom' union all
select 2,'Jim'
insert into table2 select 1,20 union all
select 1,30
--函数
create function F_Str(@ID int)
returns nvarchar(100)
as
begin
......
SQL基本语句
一. SQL的四条最基本的数据操作语句为Insert,Select,Update和Delete。
二.首先我们使用CREATE TABLE语句来创建一个表。DDL语句对数据库对象如表、列和视进行定义。它们并不对表中的行进行处理,这是因为DDL语句并不处理数据库中实际的数据。这些工作由另一 ......