易截截图软件、单文件、免安装、纯绿色、仅160KB

介绍SQL Server 2005的CROSS Apply

Cross Apply使表可以和表值函数结果进行join, 这样表值函数的参数就可以使用一个结果集,而不是一个标量值,下面是book online的原文,有例子,有解释。
The APPLY operator allows you to invoke a table-valued function for each row returned by an outer table expression of a query. The table-valued function acts as the right input and the outer table expression acts as the left input. The right input is evaluated for each row from the left input and the rows produced are combined for the final output. The list of columns produced by the APPLY operator is the set of columns in the left input followed by the list of columns returned by the right input.
There are two forms of APPLY: CROSS APPLY and OUTER APPLY. CROSS APPLY returns only rows from the outer table that produce a result set from the table-valued function. OUTER APPLY returns both rows that produce a result set, and rows that do not, with NULL values in the columns produced by the table-valued function.
As an example, consider the following tables, Employees and Departments:
  
--Create Employees table and insert values
CREATE TABLE Employees
(
  empid   int         NOT NULL,
  mgrid   int         NULL,
  empname varchar(25) NOT NULL,
  salary  money       NOT NULL,
  CONSTRAINT PK_Employees PRIMARY KEY(empid),
)
GO
INSERT INTO Employees VALUES(1 , NULL, 'Nancy'   , $10000.00)
INSERT INTO Employees VALUES(2 , 1   , 'Andrew'  , $5000.00)
INSERT INTO Employees VALUES(3 , 1   , 'Janet'   , $5000.00)
INSERT INTO Employees VALUES(4 , 1   , 'Margaret', $5000.00)
INSERT INTO Employees VALUES(5 , 2   , 'Steven'  , $2500.00)
INSERT INTO Employees VALUES(6 , 2   , 'Michael' , $2500.00)
INSERT INTO Employees VALUES(7 , 3   , 'Robert'  , $2500.00)


相关文档:

学习SQL语句之SQL语句大全

 --语 句 功 能  --数据操作  SELECT --从数据库表中检索数据行和列  INSERT --向数据库表添加新数据行  DELETE --从数据库表中删除数据行  UPDATE --更新数据库表中的数据  --数据定义  CREATE TABLE --创建一个数据库表  DROP TABLE --从数据库中删除表  ALTER TABLE --修改数据库表结构  CREATE VI ......

SQL 大全 心雨之家

1.按姓氏笔画排序:
Select * from TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as
2.数据库加密:
select encrypt('原始密码')
select pwdencrypt('原始密码')
select pwdcompare('原始密码','加密后密码') = 1--相同;否则不相同 encrypt('原始密码')
select pwdencrypt('原始密码')
select pw ......

SQL随机查询

SQL Server:
Select TOP N * from TABLE Order By NewID()  
Select TOP N * from TABLE Order By NewID()
NewID()函数将创建一个 uniqueidentifier 类型的唯一值。上面的语句实现效果是从Table中随机读取N条记录。
Access:
Select TOP N *&n ......

动态SQL(Dynamic SQL)

----start
    动态SQL是在程序运行时构造的,要执行单条SQL,使用EXECUTE IMMEDATE 语句;当批量执行SQL时,先使用PREPARE 语句构造SQL,然后使用EXECUTE 语句执行。
一:Prepare语句:用来构造批量SQL
语法:
PREPARE <sql-statement> [OUTPUT] INTO <result> [INPUT INTO] <input> ......

SQL中如何用一个表更新另一个表

for ACCESS :
update a, b set a.name=b.name1 where a.id=b.id
for SQL Server:
"update a set a.name=b.name1 from a,b where a.id=b.id"
update   a   set   a.status=b.status  
  from   table1   a,table2   b  
&nbs ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号