SQL Server Association and some functions

Source: Internet
Author: User

How to copy a table using t_ SQL statements

I have a database datahr and demo in SQL Server 2000. Their structures are the same. Other tables are named gbitem. now I want to copy all the contents of the Table Name: gbitem of the demo database to the table name: gbitem OF THE datahr database. Could you tell me how to write this T-SQL statement? Thank you!

If the target table already exists:
Insert into datahr. DBO. gbitem
Select * from demo. DBO. gbitem
If the target table does not exist:
Select * into datahr. DBO. gbitem
From demo. DBO. gbitem

-- Backup database ofsys
Backup database ofsys to disk = 'e:/ofsys'
Restore
Restore database test from disk = 'C: \ Test'

Exquisite SQL statements
Exquisite SQL statements
Note: copy a table (only copy structure, source table name: a new table name: B)
SQL: Select * into B from a where 1 <> 1
Description: copy a table (copy data, source table name: A target table name: B)

SQL: insert into B (a, B, c) Select D, E, F from B;

Note: DisplayArticle, Submitted and last reply time

SQL: select a. Title, A. username, B. adddate from Table A, (select max (adddate) adddate from table where table. Title = A. Title) B

Description: External join query (table name 1: Table A name 2: B)

SQL: select a. a, a. B, A. C, B. C, B. D, B. F from a left out join B on A. A = B. C

Note: Five minutes in advance of the schedule

SQL: Select * from Schedule where datediff ('minute ', F Start Time, getdate ()> 5

Note: Two associated tables are used to delete information that is already unavailable to the primary table in the secondary table.

SQL:

Delete from info where not exists (select * From infobz where info. INFID = infobz. INFID)

Note :--

SQL:

Select a. Num, A. Name, B. upd_date, B. prev_upd_date

From Table1,

(Select X. Num, X. upd_date, Y. upd_date prev_upd_date

From (select num, upd_date, inbound_qty, stock_onhand

From Table2

Where to_char (upd_date, 'yyyy/mm') = to_char (sysdate, 'yyyy/mm') X,

(Select num, upd_date, stock_onhand

From Table2

Where to_char (upd_date, 'yyyy/mm') =

To_char (to_date (to_char (sysdate, 'yyyy/mm') | '/01', 'yyyy/MM/dd')-1, 'yyyy/mm ')) y,

Where X. num = Y. Num (+)

And X. inbound_qty + nvl (Y. stock_onhand, 0) <> X. stock_onhand) B

Where a. num = B. Num

Note :--

SQL:

Select * From studentinfo where not exists (select * from student where studentinfo. id = student. ID) and Department name = '"& strdepartmentname &"' and major name = '"& strinclusionname &" 'order by gender, student source, total score of the College Entrance Examination

Note:

Go to the telephone fee statistics of each unit from the database for one year (two sources of the fixed telephone fee congratulation fertilizer List)

SQL:

Select a. userper, A. Tel, A. standalone, to_char (A. telfeedate, 'yyyy') as telyear,

Sum (decode (to_char (A. telfeedate, 'mm'), '01', A. factration) as Jan,

Sum (decode (to_char (A. telfeedate, 'mm'), '02', A. factration) as Fri,

Sum (decode (to_char (A. telfeedate, 'mm'), '03', A. factration) as MAR,

Sum (decode (to_char (A. telfeedate, 'mm'), '04 ', A. factration) as Apr,

Sum (decode (to_char (A. telfeedate, 'mm'), '05 ', A. factration) as may,

Sum (decode (to_char (A. telfeedate, 'mm'), '06', A. factration) as Jue,

Sum (decode (to_char (A. telfeedate, 'mm'), '07 ', A. factration) as Jul,

Sum (decode (to_char (A. telfeedate, 'mm'), '08 ', A. factration) as Agu,

Sum (decode (to_char (A. telfeedate, 'mm'), '09', A. factration) as SEP,

Sum (decode (to_char (A. telfeedate, 'mm'), '10', A. factration) as Oct,

Sum (decode (to_char (A. telfeedate, 'mm'), '11', A. factration) as Nov,

Sum (decode (to_char (A. telfeedate, 'mm'), '12', A. factration) as Dec

From (select a. userper, A. Tel, A. standalone, B. telfeedate, B. factration

From telfeestand A, telstmb

Where a. Tel = B. telfax)

Group by A. userper, A. Tel, A. standalone, to_char (A. telfeedate, 'yyyy ')

Notes: Four-table join query problems:

SQL: Select * from a left inner join B on. A = B. B right inner join C on. A = C. C inner join D on. A = D. d Where .....

Note: Obtain the smallest unused ID number in the table.

SQL:

Select (case when exists (select * From handle B where B. handleid = 1) Then min (handleid) + 1 else 1 end) as handleid

From handle

Where not handleid in (select a. handleid-1 from handle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.