Several skills of SQLServer

xiaoxiao2021-03-06  14

1. Restorize the sequence of a field (from 1 to N):

Declare @i int

Set @i = 0

Update table1 set @i = @i 1, Field1 = @i

2. Ranked by grade

Update transcript

Set a. Name =

SELECT Count (*) 1

From transcript B

WHERE A. Total results

)

From transcripts a

3. Query external databases

SELECT A. *

From OpenRowSet ('Microsoft.jet.OleDb.4.0', 'C: / Test.mdb'; 'Admin'; '', Table1) A

4. Query Excel files

SELECT *

From OpenDataSource ('microsoft.jet.oledb.4.0', 'data source = "c: /test.xls"; user ID = admin; password =; extended profits = excel 8.0') ... Sheet1 $

5. Specify the rules in the query

SELECT * from Table1 Order by Field1 Collate Chinese_PRC_BIN

Why specify the sort rules? See:

http://www.delphibbs.com/delphibbs/dispq.asp?lid=1633985

For example, check if there is a specified user in the PUB_USERS table in the database:

SELECT Count (*) from pub_users where [username] = 'admin' and [password] = 'aaa' collate Chinese_prc_bin

The default comparison is not case sensitive. If you do not add collate chinese_prc_bin, the password AAA and AAA are equivalent, which is of course not in conformity. Note that every condition must be specified, the user name in the upper example not case sensitive.

6. A tip of 6.order By

ORDER BY can specify a column without specifying the column name, in the following example, indicating its use (note, the third column does not specify an alias)

Select A.ID, A.Name, (Select Count (*) from Tableb B Where A.Id = B.PID) from Tablea A ORDER BY 3

转载请注明原文地址:https://www.9cbs.com/read-49510.html

New Post(0)