A SQL Server practical tips

1, Description: Copy table (only replicated structure, source table names: a new table name: b) (Access available)
Method a: SELECT INTO A from B WHERE 1 <> 1 (only for SQlServer)
Method Two: select top 0
INTO A from B
2, Description: copy table (copy the data source table name: a target table name: b) (Access available)
INSERT INTO B (A, B, C) SELECT D, E, F from A;

select * into b from a

3, description: copying between cross database table (specific data using an absolute path) (Access available)
INSERT INTO B (A, B, C) SELECT D, E, F from A in 'specific database' where conditions
Examples: .from a in ' "& Server.MapPath ( ". ") &" \ data.mdb "&"' where ..

4, description: subqueries (table 1: a table 2: B)
SELECT A, B, C from A WHERE A the IN (SELECT D from B) or: select a, b, c from a where a IN (1 2, 3)

5, description: displaying articles, the author and the last response time
select a.title, a.username, b.adddate from table a, (select max (adddate) adddate from table where table.title = a.title) b

6, description: outer join query (table 1: a table 2: B)
SELECT AA, ab &, AC, BC, BD, BF the LEFT OUT from the JOIN A AA B = the ON BC

7, Description: Online view query (table name. 1: A)
SELECT * from (the SELECT A, B, C A the FROM) TA WHERE T>. 1;

8, description: between usage, including the boundary values BETWEEN limit query data range, not between not include
SELECT * from table1 WHERE Time BETWEEN TIME1 and TIME2
SELECT A, B, C, from table1 WHERE A Not BETWEEN value 1 and the value 2

9, description: in the use of
select * from table1 where a [not ] in ( ' value 1', 'value 2', '4 value', 'value 6')

10, Description: Two related tables, delete the main table has no information on side tables
delete from table1 where not exists (select * from table2 where table1.field1 = table2.field1)

11、说明:四表联查问题:
select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....

12, Description: The schedule five minutes in advance to remind
SQL: select * from schedule where datediff ( 'minute', f start time, getdate ())> 5

13, description: a sql statement to get the database tab
select top 10 b * from (select top 20 primary key field, sort field from table order by sort field desc) a, table b where b primary key field = a primary key field... order by a sort field.
realization:
About database page:

declare @start int,@end int

@sql nvarchar(600)

set @sql=’select top’+str(@end-@start+1)+’+from T where rid not in(select top’+str(@str-1)+’Rid from T where Rid>-1)’

exec sp_executesql @sql
Note: not with a variable, so the only way of special treatment in practical applications directly after the top. Rid is an identity column, if there are specific top field, this is very good. Because inconsistent avoid the top field if logical index, the query results in the actual table (data in the logical index and possible inconsistencies in the data table, while the index in the first query, if the query index)

14, description: the first 10 rows
10 * form table1 where range select top

15, Description: From 10 to 15 of the recording
SELECT Top. 5 from (SELECT Top 15 from Table ID Order by ASC) table_ Alias order by id desc

select from (select top 5 from (select top 15 * from table order by id asc) table_别名order by id desc) table_别名order by id asc

16, Description: Remove 10 random data
select top 10 * from tablename order by newid ()

17, Description: picked randomly
select newid ()

Generating a plurality of Guid:

--CREATE TABLE #GuidList (Id int,Gid uniqueidentifier)

-- GO

- execution cycle times

--INSERT INTO #GuidList(Gid) SELECT newid()

--GO 100

--select * from #GuidList

18, search for records:
begin to N records
Select Top N * From Table

M to N records (to have the main index ID)
the Select the MN * Top the From Table Where ID in (Select Top M ID From Table) Order by ID Desc

N to end recording
Select Top N * From Table Order by ID Desc
Case
example 1: a table has more than ten thousand records, the first field RecID table is self-growth fields, write a SQL statement, find the first table 31 to the 40th record.

select top 10 recid from A where recid not in(select top 30 recid from A)

Analysis: If you write will have some problems if there is a logical recid index in the table.

select top 10 recid from A where ...... Find the index is, while the back select top 30 recid from A in the lookup table data, so due to the sequential index and possible inconsistencies in the data table, thus leading to the query the original data is not to be obtained.

solution

1, with the order by select top 30 recid from A order by ricid If the field is not self-growth, will be a problem

2, in that a subquery also add conditions: select top 30 recid from A where recid> -1

Example 2: Query the table to the last record, does not know how much data, the structure of this table and the table total.
set @s = 'select top 1 * from T where pid not in (select top' + str (@ count-1) + 'pid from T)'

print @s exec sp_executesql @s

19, Description: select a maximum recording all the information corresponding to the same b-value data in each group (like this forum for a monthly usage can list, monthly selling product analysis, performance ranking by subject, and the like.)
SELECT A, B, C from TA TableName WHERE A = (SELECT max (A) from TB TableName WHERE tb.b = ta.b)

20, description: listed type, vender, pcs field to field type arrangement, case can easily achieve multiple choice, select the similar case.
select type, sum (case vender when 'A' then pcs else 0 end), sum (case vender when 'C' then pcs else 0 end), sum (case vender when 'B' then pcs else 0 end) FROM tablename group by type
display results:
type Vender PCS
PC A 1
computer B 1
disc B 2
disc A 2
Mobile B 3
Mobile C 3

Guess you like

Origin blog.51cto.com/14630964/2476394